-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.p
136 lines (108 loc) · 6.12 KB
/
day9.p
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
--- Day 9: Marble Mania ---
You talk to the Elves while you wait for your navigation system to initialize. To pass the time, they introduce you to their favorite marble game.
The Elves play this game by taking turns arranging the marbles in a circle according to very particular rules. The marbles are numbered starting with 0 and increasing by 1 until every marble has a number.
First, the marble numbered 0 is placed in the circle. At this point, while it contains only a single marble, it is still a circle: the marble is both clockwise from itself and counter-clockwise from itself. This marble is designated the current marble.
Then, each Elf takes a turn placing the lowest-numbered remaining marble into the circle between the marbles that are 1 and 2 marbles clockwise of the current marble. (When the circle is large enough, this means that there is one marble between the marble that was just placed and the current marble.) The marble that was just placed then becomes the current marble.
However, if the marble that is about to be placed has a number which is a multiple of 23, something entirely different happens. First, the current player keeps the marble they would have placed, adding it to their score. In addition, the marble 7 marbles counter-clockwise from the current marble is removed from the circle and also added to the current player's score. The marble located immediately clockwise of the marble that was removed becomes the new current marble.
For example, suppose there are 9 players. After the marble with value 0 is placed in the middle, each player (shown in square brackets) takes a turn. The result of each of those turns would produce circles of marbles like this, where clockwise is to the right and the resulting current marble is in parentheses:
[-] (0)
[1] 0 (1)
[2] 0 (2) 1
[3] 0 2 1 (3)
[4] 0 (4) 2 1 3
[5] 0 4 2 (5) 1 3
[6] 0 4 2 5 1 (6) 3
[7] 0 4 2 5 1 6 3 (7)
[8] 0 (8) 4 2 5 1 6 3 7
[9] 0 8 4 (9) 2 5 1 6 3 7
[1] 0 8 4 9 2(10) 5 1 6 3 7
[2] 0 8 4 9 2 10 5(11) 1 6 3 7
[3] 0 8 4 9 2 10 5 11 1(12) 6 3 7
[4] 0 8 4 9 2 10 5 11 1 12 6(13) 3 7
[5] 0 8 4 9 2 10 5 11 1 12 6 13 3(14) 7
[6] 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7(15)
[7] 0(16) 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
[8] 0 16 8(17) 4 9 2 10 5 11 1 12 6 13 3 14 7 15
[9] 0 16 8 17 4(18) 9 2 10 5 11 1 12 6 13 3 14 7 15
[1] 0 16 8 17 4 18 9(19) 2 10 5 11 1 12 6 13 3 14 7 15
[2] 0 16 8 17 4 18 9 19 2(20)10 5 11 1 12 6 13 3 14 7 15
[3] 0 16 8 17 4 18 9 19 2 20 10(21) 5 11 1 12 6 13 3 14 7 15
[4] 0 16 8 17 4 18 9 19 2 20 10 21 5(22)11 1 12 6 13 3 14 7 15
[5] 0 16 8 17 4 18(19) 2 20 10 21 5 22 11 1 12 6 13 3 14 7 15
[6] 0 16 8 17 4 18 19 2(24)20 10 21 5 22 11 1 12 6 13 3 14 7 15
[7] 0 16 8 17 4 18 19 2 24 20(25)10 21 5 22 11 1 12 6 13 3 14 7 15
The goal is to be the player with the highest score after the last marble is used up. Assuming the example above ends after the marble numbered 25, the winning score is 23+9=32 (because player 5 kept marble 23 and removed marble 9, while no other player got any points in this very short example game).
Here are a few more examples:
10 players; last marble is worth 1618 points: high score is 8317
13 players; last marble is worth 7999 points: high score is 146373
17 players; last marble is worth 1104 points: high score is 2764
21 players; last marble is worth 6111 points: high score is 54718
30 players; last marble is worth 5807 points: high score is 37305
What is the winning Elf's score?
*/
DEFINE VARIABLE i AS INTEGER NO-UNDO.
DEFINE VARIABLE iCurrentMarble AS INTEGER NO-UNDO.
DEFINE VARIABLE iCurrentMarblePos AS INTEGER NO-UNDO.
DEFINE VARIABLE iNbMarblesInCircle AS INTEGER NO-UNDO.
DEFINE VARIABLE lcCircle AS LONGCHAR NO-UNDO.
DEFINE TEMP-TABLE ttPlayer NO-UNDO
FIELD id AS INTEGER
FIELD iScore AS INTEGER
INDEX ix IS PRIMARY UNIQUE id
INDEX ip iScore DESCENDING.
/* 9 players; last marble is worth 25 points */
/* &GLOBAL-DEFINE xiPlayers 9 */
/* &GLOBAL-DEFINE xiMarbles 25 */
/* My input: */
/* 473 players; last marble is worth 70904 points */
&GLOBAL-DEFINE xiPlayers 473
&GLOBAL-DEFINE xiMarbles 70904
ETIME(YES).
ASSIGN
iCurrentMarble = 0
lcCircle = "0"
iCurrentMarblePos = 0
iNbMarblesInCircle = 1
.
DO i = 1 TO {&xiPlayers}:
CREATE ttPlayer.
ASSIGN ttPlayer.id = i.
END.
blk:
DO WHILE TRUE:
FOR EACH ttPlayer BY ttPlayer.id:
iCurrentMarble = iCurrentMarble + 1.
IF iCurrentMarble MODULO 23 = 0 THEN DO:
ttPlayer.iScore = ttPlayer.iScore + iCurrentMarble.
/* find marble 7 marbles counter-clockwise */
iCurrentMarblePos = (iCurrentMarblePos - 7) MODULO iNbMarblesInCircle.
/* add it to the player's score */
ttPlayer.iScore = ttPlayer.iScore + INTEGER(ENTRY(iCurrentMarblePos + 1, lcCircle)).
/* remove it from the circle */
ENTRY(iCurrentMarblePos + 1, lcCircle) = "".
lcCircle = REPLACE(lcCircle, ",,", ",").
iNbMarblesInCircle = iNbMarblesInCircle - 1.
END.
ELSE DO:
iCurrentMarblePos = (iCurrentMarblePos + 1) MODULO iNbMarblesInCircle + 1.
/* insert the marble into the circle */
IF NUM-ENTRIES(lcCircle) < iCurrentMarblePos + 1 THEN
lcCircle = lcCircle + "," + STRING(iCurrentMarble).
ELSE
ENTRY(iCurrentMarblePos + 1, lcCircle) = STRING(iCurrentMarble) + "," + ENTRY(iCurrentMarblePos + 1, lcCircle).
iNbMarblesInCircle = iNbMarblesInCircle + 1.
END.
IF iCurrentMarble >= {&xiMarbles} THEN
LEAVE blk.
END.
END.
FOR EACH ttPlayer BY ttPlayer.iScore DESCENDING:
LEAVE.
END.
MESSAGE ETIME SKIP
/* STRING(lcCircle) SKIP */
ttPlayer.iScore
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* 70732 */
/* 371284 */