-
Notifications
You must be signed in to change notification settings - Fork 0
/
day18.p
277 lines (241 loc) · 7.83 KB
/
day18.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
--- Day 18: Settlers of The North Pole ---
On the outskirts of the North Pole base construction project, many Elves are collecting lumber.
The lumber collection area is 50 acres by 50 acres; each acre can be either open ground (.), trees (|), or a lumberyard (#). You take a scan of the area (your puzzle input).
Strange magic is at work here: each minute, the landscape looks entirely different. In exactly one minute, an open acre can fill with trees, a wooded acre can be converted to a lumberyard, or a lumberyard can be cleared to open ground (the lumber having been sent to other projects).
The change to each acre is based entirely on the contents of that acre as well as the number of open, wooded, or lumberyard acres adjacent to it at the start of each minute. Here, "adjacent" means any of the eight acres surrounding that acre. (Acres on the edges of the lumber collection area might have fewer than eight adjacent acres; the missing acres aren't counted.)
In particular:
An open acre will become filled with trees if three or more adjacent acres contained trees. Otherwise, nothing happens.
An acre filled with trees will become a lumberyard if three or more adjacent acres were lumberyards. Otherwise, nothing happens.
An acre containing a lumberyard will remain a lumberyard if it was adjacent to at least one other lumberyard and at least one acre containing trees. Otherwise, it becomes open.
These changes happen across all acres simultaneously, each of them using the state of all acres at the beginning of the minute and changing to their new form by the end of that same minute. Changes that happen during the minute don't affect each other.
For example, suppose the lumber collection area is instead only 10 by 10 acres with this initial configuration:
Initial state:
.#.#...|#.
.....#|##|
.|..|...#.
..|#.....#
#.#|||#|#|
...#.||...
.|....|...
||...#|.#|
|.||||..|.
...#.|..|.
After 1 minute:
.......##.
......|###
.|..|...#.
..|#||...#
..##||.|#|
...#||||..
||...|||..
|||||.||.|
||||||||||
....||..|.
After 2 minutes:
.......#..
......|#..
.|.|||....
..##|||..#
..###|||#|
...#|||||.
|||||||||.
||||||||||
||||||||||
.|||||||||
After 3 minutes:
.......#..
....|||#..
.|.||||...
..###|||.#
...##|||#|
.||##|||||
||||||||||
||||||||||
||||||||||
||||||||||
After 4 minutes:
.....|.#..
...||||#..
.|.#||||..
..###||||#
...###||#|
|||##|||||
||||||||||
||||||||||
||||||||||
||||||||||
After 5 minutes:
....|||#..
...||||#..
.|.##||||.
..####|||#
.|.###||#|
|||###||||
||||||||||
||||||||||
||||||||||
||||||||||
After 6 minutes:
...||||#..
...||||#..
.|.###|||.
..#.##|||#
|||#.##|#|
|||###||||
||||#|||||
||||||||||
||||||||||
||||||||||
After 7 minutes:
...||||#..
..||#|##..
.|.####||.
||#..##||#
||##.##|#|
|||####|||
|||###||||
||||||||||
||||||||||
||||||||||
After 8 minutes:
..||||##..
..|#####..
|||#####|.
||#...##|#
||##..###|
||##.###||
|||####|||
||||#|||||
||||||||||
||||||||||
After 9 minutes:
..||###...
.||#####..
||##...##.
||#....###
|##....##|
||##..###|
||######||
|||###||||
||||||||||
||||||||||
After 10 minutes:
.||##.....
||###.....
||##......
|##.....##
|##.....##
|##....##|
||##.####|
||#####|||
||||#|||||
||||||||||
After 10 minutes, there are 37 wooded acres and 31 lumberyards. Multiplying the number of wooded acres by the number of lumberyards gives the total resource value after ten minutes: 37 * 31 = 1147.
What will the total resource value of the lumber collection area be after 10 minutes?
*/
&GLOBAL-DEFINE xiMinutes 10
&GLOBAL-DEFINE xcOpen "."
&GLOBAL-DEFINE xcTrees "|"
&GLOBAL-DEFINE xcLumberyard "#"
DEFINE VARIABLE cAcre AS CHARACTER NO-UNDO.
DEFINE VARIABLE cLine AS CHARACTER NO-UNDO.
DEFINE VARIABLE iAcre AS INTEGER NO-UNDO.
DEFINE VARIABLE iLine AS INTEGER NO-UNDO.
DEFINE VARIABLE iLumberyards AS INTEGER NO-UNDO.
DEFINE VARIABLE iMinute AS INTEGER NO-UNDO.
DEFINE VARIABLE iTrees AS INTEGER NO-UNDO.
DEFINE VARIABLE iWidth AS INTEGER NO-UNDO.
DEFINE TEMP-TABLE ttLumber NO-UNDO
FIELD iLine AS INTEGER
FIELD lActive AS LOGICAL
FIELD cLumber AS CHARACTER
INDEX ix IS PRIMARY iLine lActive.
DEFINE BUFFER bttLumber FOR ttLumber.
DEFINE BUFFER pttLumber FOR ttLumber.
DEFINE BUFFER nttLumber FOR ttLumber.
FUNCTION getAdjacent RETURNS INTEGER ( piAcre AS INTEGER, pcType AS CHARACTER ):
DEFINE VARIABLE iAcre AS INTEGER NO-UNDO.
DEFINE VARIABLE iCount AS INTEGER NO-UNDO.
IF AVAILABLE pttLumber THEN DO iAcre = MAXIMUM(1, piAcre - 1) TO MINIMUM(iWidth, piAcre + 1):
IF SUBSTRING(pttLumber.cLumber, iAcre, 1) = pcType THEN
iCount = iCount + 1.
END.
DO iAcre = MAXIMUM(1, piAcre - 1) TO MINIMUM(iWidth, piAcre + 1):
IF iAcre = piAcre THEN NEXT.
IF SUBSTRING(ttLumber.cLumber, iAcre, 1) = pcType THEN
iCount = iCount + 1.
END.
IF AVAILABLE nttLumber THEN DO iAcre = MAXIMUM(1, piAcre - 1) TO MINIMUM(iWidth, piAcre + 1):
IF SUBSTRING(nttLumber.cLumber, iAcre, 1) = pcType THEN
iCount = iCount + 1.
END.
RETURN iCount.
END FUNCTION.
ETIME(YES).
INPUT FROM C:\User\JCCARDOT\Perso\Travail\aoc\aoc2018\day18.txt.
REPEAT:
iLine = iLine + 1.
CREATE ttLumber.
ASSIGN ttLumber.iLine = iLine
ttLumber.lActive = YES.
IMPORT UNFORMATTED ttLumber.cLumber.
IF iWidth = 0 THEN
iWidth = LENGTH(ttLumber.cLumber).
CREATE ttLumber.
ASSIGN ttLumber.iLine = iLine
ttLumber.lActive = NO.
END.
DELETE ttLumber.
INPUT CLOSE.
RUN drawLumber.
DO iMinute = 1 TO {&xiMinutes}:
FOR EACH ttLumber WHERE ttLumber.iLine > 0 AND ttLumber.lActive = YES:
FIND pttLumber WHERE pttLumber.iLine = ttLumber.iLine - 1 AND pttLumber.lActive = YES NO-ERROR.
FIND nttLumber WHERE nttLumber.iLine = ttLumber.iLine + 1 AND nttLumber.lActive = YES NO-ERROR.
FIND bttLumber WHERE bttLumber.iLine = ttLumber.iLine AND bttLumber.lActive = NO.
bttLumber.cLumber = ttLumber.cLumber.
DO iAcre = iWidth TO 1 BY -1:
cAcre = SUBSTRING(ttLumber.cLumber, iAcre, 1).
CASE cAcre:
WHEN {&xcOpen} THEN IF getAdjacent(iAcre, {&xcTrees}) >= 3 THEN
SUBSTRING(bttLumber.cLumber, iAcre, 1) = {&xcTrees}.
WHEN {&xcTrees} THEN IF getAdjacent(iAcre, {&xcLumberyard}) >= 3 THEN
SUBSTRING(bttLumber.cLumber, iAcre, 1) = {&xcLumberyard}.
WHEN {&xcLumberyard} THEN IF getAdjacent(iAcre, {&xcLumberyard}) = 0
OR getAdjacent(iAcre, {&xcTrees}) = 0 THEN
SUBSTRING(bttLumber.cLumber, iAcre, 1) = {&xcOpen}.
END CASE.
END.
END.
REPEAT PRESELECT EACH ttLumber:
FIND NEXT ttLumber.
ttLumber.lActive = NOT ttLumber.lActive.
END.
RUN drawLumber.
END.
FOR EACH ttLumber WHERE ttLumber.iLine > 0 AND ttLumber.lActive = YES:
DO iAcre = iWidth TO 1 BY -1:
cAcre = SUBSTRING(ttLumber.cLumber, iAcre, 1).
IF cAcre = {&xcTrees} THEN
iTrees = iTrees + 1.
ELSE IF cAcre = {&xcLumberyard} THEN
iLumberyards = iLumberyards + 1.
END.
END.
MESSAGE ETIME SKIP
iTrees iLumberyards SKIP
iTrees * iLumberyards
VIEW-AS ALERT-BOX INFO BUTTONS OK.
/* 1201 */
/* 837 605 */
/* 506385 */
PROCEDURE drawLumber:
OUTPUT TO c:/temp/lumber.txt APPEND.
PUT UNFORMATTED "Minute: " iMinute SKIP.
FOR EACH ttLumber WHERE ttLumber.iLine > 0 AND ttLumber.lActive = YES:
PUT UNFORMATTED ttLumber.cLumber SKIP.
END.
PUT UNFORMATTED FILL("=", iWidth) SKIP(1).
OUTPUT CLOSE.
END PROCEDURE.