#P2100. ACSL 2023-2024 Intermediate Division Contest #1 ACSL Tiles
ACSL 2023-2024 Intermediate Division Contest #1 ACSL Tiles
PROBLEM DESCRIPTION
ACSL Tiles is a one-person game played with rectangular tiles. Each tile has a single-digit number between and inclusive at each end. At the start of the game, there are rows, each with a number. The goal of the game is to build rows by placing a tile at the right end of a 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.
At each turn, try the next tile in your hand to see if it can be added to one of the rows, starting with the row after the one where the last tile was placed, rotating back to Row if necessary. Start looking at Row when the game starts. However, if the last tile placed was a double (i.e., both numbers are the same), another tile must be placed on that row before any other match can be considered. If the tile cannot be placed, add it to the discard pile.
INPUT FORMAT
Input a 4-digit number that gives the initial numbers, from Row to Row . It is followed by a string of no more than 2-digit integers, each separated by a single space. Each 2-digit number represents the two numbers on each tile.
OUTPUT FORMAT
After placing the tiles using the rules above, output the sum of the single-digit numbers on the tiles in the discard pile.
SAMPLE
INPUT #1
5923
56 27 73 34 99 45 32 19 64 57 18
OUTPUT #1
21
INPUT #2
4687
81 72 15 89 36 21 13 67 42 93 48 83 45 47 52 94 62
OUTPUT #2
86
INPUT #3
1932
94 81 13 43 21 31 89 69 18 28 86 88 29 89 92
OUTPUT #3
11
INPUT #4
1957
32 69 87 73 31 88 62
OUTPUT #4
23
INPUT #5
1542
24 44 39 32 92 63 47 76 37 78 38
OUTPUT #5
46
EXPLANATION
Sample #1 Explanation
The game starts with rows having numbers 5, 9, 2, 3.

The tile 56 is placed on Row ; the tile 27 is placed on Row ; the tile 73 is placed on Row as
37 because Row is checked first after a tile is placed on Row ; the tile 34 is placed on the
discard pile; the tile 99 is placed on Row .

The 45 and the 32 tiles are placed in the discard pile because they don’t match the 99 tile; the tile 19 is placed on Row as 91; the tile 64 is placed on Row ; the 57 is placed on Row as 75; and the 18 is placed on Row . The final outcome is shown below:

The sum of the single-digit numbers on the tiles in the discard pile is .