#AJ2301. ACSL Tiles(初级)

ACSL Tiles(初级)

Description

ACSL Tiles is a one-person game played with rectangular tiles. Each tile has a single-digit number between 1 and 9 inclusive at each end. At the start of the game, there are 4 rows, each with a number. The goal of the game is to build rows by placing a tile at the right end of the topmost row whose last number matches a number on the tile. Tiles can be re-oriented; thus, the tiles 34 and 43 are the same tile. If a tile cannot be placed on any row, it is placed in the discard pile. When all tiles have been played or discarded, find the sum of the single-digit numbers on all of the tiles in the discard pile.

Input Format

Input a 4-digit integer that gives the initial numbers from Row 1 to Row 4, and a string of no more than fifty 2-digit integers, each separated by a single space. Each 2-digit integer represents the two numbers on each tile, both between 1 and 9, inclusive.

Output Format

After placing the tiles using the rules above, output the sum of the single-digit numbers on all of the tiles in the discard pile.

Explanation

The game starts with 4 rows having numbers 5, 9, 2, 3.

After tile 56 is placed in Row 1, tile 85 is placed in the discard pile. Both 27 and 73 are placed in Row 3 and tile 14 is placed in the discard pile.

The next tile, 34, is placed in Row 3, the topmost row it can go on. And finally, the 26 tile is added to Row 1, with the orientation of 62.

The discard pile contains tiles 85 and 14. The result is the sum of the single-digit numbers on all of the tiles in the discard pile: 8+5+1+4=18.

5923
56 85 27 73 14 34 62
18
8423
74 92 57 93 26 87 14 63 82 54 12
26
1253
51 81 35 84 95 26 59 13 71 35 46 28
31
2694
69 76 41 89 16 78 64 36 12 95 52
22
6479
58 73 92 54 75 35 78 25 81 24 16 95 36 82 14 27 43 13 51
45