#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 11 and 99 inclusive at each end. At the start of the game, there are 44 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 11 if necessary. Start looking at Row 11 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 11 to Row 44. It is followed by a string of no more than 5050 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 44 rows having numbers 5, 9, 2, 3.

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

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 22 as 91; the tile 64 is placed on Row 11; the 57 is placed on Row 33 as 75; and the 18 is placed on Row 22. The final outcome is shown below:

The sum of the single-digit numbers on the tiles in the discard pile is 3+4+4+5+3+2=213 + 4 + 4 + 5 + 3 + 2 = 21.