-
Notifications
You must be signed in to change notification settings - Fork 3
/
elixir_day23_test.exs
476 lines (423 loc) · 9.01 KB
/
elixir_day23_test.exs
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
defmodule ElixirDay23Test do
use ExUnit.Case
doctest ElixirDay23
alias ElixirDay23.{Breakout, Coordinator, Computer, ComputerServer, PainterRobot}
## Day 23 specific tests
test "day23_part1and2" do
program = ElixirDay23.parse("../../23/input.txt")
how_many = 50
{:ok, pid} = Coordinator.start(program, how_many, self())
pid
|> IO.inspect(label: "coordinator pid")
receive do
{:answers, p1, p2} ->
assert p1 == 22134
assert p2 == 16084
end
end
## Day 13 specific tests
test "day13_part1" do
got =
ElixirDay23.parse("../../13/input.txt")
|> Breakout.part1()
assert got == 270
end
test "day13_part2" do
got =
ElixirDay23.parse("../../13/input.txt")
|> Breakout.part2()
assert got == 12535
end
## Day 11 specific tests
test "day11_part1" do
got =
ElixirDay23.parse("../../11/input.txt")
|> PainterRobot.new(0)
|> PainterRobot.execute()
|> PainterRobot.count_painted_squares()
assert got == 2539
end
test "day11_part2" do
got =
ElixirDay23.parse("../../11/input.txt")
|> PainterRobot.new(1)
|> PainterRobot.execute()
|> PainterRobot.count_painted_squares()
assert got == 248
end
## Day 9 specific tests
test "day9_testprog1" do
test_prog91 = [109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99]
outputs = Computer.solve(test_prog91, [])
# Quine
assert outputs == test_prog91
end
test "day9_testprog2" do
test_prog92 = [1102, 34_915_192, 34_915_192, 7, 4, 7, 99, 0]
outputs = Computer.solve(test_prog92, [])
assert outputs == [1_219_070_632_396_864]
end
test "day9_testprog3" do
test_prog93 = [104, 1_125_899_906_842_624, 99]
outputs = Computer.solve(test_prog93, [])
assert outputs == [1_125_899_906_842_624]
end
test "day9_part1" do
outputs =
ElixirDay23.parse("../../09/input.txt")
|> Computer.solve([1])
assert outputs == [3_780_860_499]
end
test "day9_part2" do
outputs =
ElixirDay23.parse("../../09/input.txt")
|> Computer.solve([2])
assert outputs == [33343]
end
## Day 7 Specific Tests
test "day7_part1" do
{got_seq, got_val} =
ElixirDay23.parse("../../07/input.txt")
|> ElixirDay23.Amplify.amplify_once_max_seq()
assert got_seq == [2, 0, 3, 1, 4]
assert got_val == 13848
end
test "day7_part2" do
{got_seq, got_val} =
ElixirDay23.parse("../../07/input.txt")
|> ElixirDay23.Amplify.amplify_loop_max_seq()
assert got_seq == [6, 8, 7, 5, 9]
assert got_val == 12_932_154
end
test "amplify_once" do
prog_a_1 = [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]
prog_a_2 = [
3,
23,
3,
24,
1002,
24,
10,
24,
1002,
23,
-1,
23,
101,
5,
23,
23,
1,
24,
23,
23,
4,
23,
99,
0,
0
]
prog_a_3 = [
3,
31,
3,
32,
1002,
32,
10,
32,
1001,
31,
-2,
31,
1007,
31,
0,
33,
1002,
33,
7,
33,
1,
33,
31,
31,
1,
32,
31,
31,
4,
31,
99,
0,
0,
0
]
test_cases = [
{prog_a_1, [4, 3, 2, 1, 0], 43210},
{prog_a_2, [0, 1, 2, 3, 4], 54321},
{prog_a_3, [1, 0, 4, 3, 2], 65210}
]
# Test amplify_once
test_cases
|> Enum.each(fn {prog, phase_seq, want_val} ->
got_val = ElixirDay23.Amplify.amplify_once(prog, phase_seq)
assert got_val == want_val
end)
# Test amplify_once_max_seq
test_cases
|> Enum.each(fn {prog, want_seq, want_val} ->
{got_seq, got_val} = ElixirDay23.Amplify.amplify_once_max_seq(prog)
assert got_seq == want_seq
assert got_val == want_val
end)
end
test "amplify_loop" do
prog_b_1 = [
3,
26,
1001,
26,
-4,
26,
3,
27,
1002,
27,
2,
27,
1,
27,
26,
27,
4,
27,
1001,
28,
-1,
28,
1005,
28,
6,
99,
0,
0,
5
]
prog_b_2 = [
3,
52,
1001,
52,
-5,
52,
3,
53,
1,
52,
56,
54,
1007,
54,
5,
55,
1005,
55,
26,
1001,
54,
-5,
54,
1105,
1,
12,
1,
53,
54,
53,
1008,
54,
0,
55,
1001,
55,
1,
55,
2,
53,
55,
53,
4,
53,
1001,
56,
-1,
56,
1005,
56,
6,
99,
0,
0,
0,
0,
10
]
test_cases = [
{prog_b_1, [9, 8, 7, 6, 5], 139_629_729},
{prog_b_2, [9, 7, 8, 5, 6], 18216}
]
# Test amplify_loop
test_cases
|> Enum.each(fn {prog, phase_seq, want_val} ->
got_val = ElixirDay23.Amplify.amplify_loop(prog, phase_seq)
assert got_val == want_val
end)
# Test amplify_loop_max_seq
test_cases
|> Enum.each(fn {prog, want_seq, want_val} ->
{got_seq, got_val} = ElixirDay23.Amplify.amplify_loop_max_seq(prog)
assert got_seq == want_seq
assert got_val == want_val
end)
end
## GenServer tests
test "day 5 part1 GenServer (Full Input)" do
program = ElixirDay23.parse("../../05/input.txt")
{:ok, pid} = ComputerServer.start(program, [1])
got = ComputerServer.outputs(pid)
want = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5_821_753]
assert got == want
end
test "day 5 part1 GenServer (Pass Input after starting)" do
program = ElixirDay23.parse("../../05/input.txt")
{:ok, pid} = ComputerServer.start(program, [])
assert ComputerServer.outputs(pid) == []
ComputerServer.add_input(pid, 1)
got = ComputerServer.outputs(pid)
want = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5_821_753]
assert got == want
end
test "day5 part2 GenServer (Pop Output)" do
program = ElixirDay23.parse("../../05/input.txt")
{:ok, pid} = ComputerServer.start(program, [5])
old_output_len = length(ComputerServer.outputs(pid))
output = ComputerServer.pop_output(pid)
new_output_len = length(ComputerServer.outputs(pid))
assert new_output_len + 1 == old_output_len
assert output == 11_956_381
end
## Non GenServer Tests
test "pausing on missing input, adding input, and resuming" do
program = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]
c = Computer.new(program, []) |> Computer.execute()
assert c.waiting_for_input == true
c = c |> Computer.add_input(8) |> Computer.execute()
assert c.waiting_for_input == false
assert c.halted == true
assert c.outputs == [1]
end
test "day 5 part1" do
got =
ElixirDay23.parse("../../05/input.txt")
|> Computer.solve([1])
want = [0, 0, 0, 0, 0, 0, 0, 0, 0, 5_821_753]
assert got == want
end
test "day 5 part2" do
got =
ElixirDay23.parse("../../05/input.txt")
|> Computer.solve([5])
want = [11_956_381]
assert got == want
end
test "8_1" do
program = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8]
assert Computer.solve(program, [7]) == [0]
assert Computer.solve(program, [8]) == [1]
assert Computer.solve(program, [9]) == [0]
end
test "8_2" do
program = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8]
assert Computer.solve(program, [7]) == [1]
assert Computer.solve(program, [8]) == [0]
assert Computer.solve(program, [9]) == [0]
end
test "8_3" do
program = [3, 3, 1108, -1, 8, 3, 4, 3, 99]
assert Computer.solve(program, [7]) == [0]
assert Computer.solve(program, [8]) == [1]
assert Computer.solve(program, [9]) == [0]
end
test "8_4" do
program = [3, 3, 1107, -1, 8, 3, 4, 3, 99]
assert Computer.solve(program, [7]) == [1]
assert Computer.solve(program, [8]) == [0]
assert Computer.solve(program, [9]) == [0]
end
test "jump_1" do
program = [3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9]
assert Computer.solve(program, [0]) == [0]
assert Computer.solve(program, [10]) == [1]
end
test "jump_2" do
program = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1]
assert Computer.solve(program, [0]) == [0]
assert Computer.solve(program, [10]) == [1]
end
test "larger" do
program = [
3,
21,
1008,
21,
8,
20,
1005,
20,
22,
107,
8,
21,
20,
1006,
20,
31,
1106,
0,
36,
98,
0,
0,
1002,
21,
125,
20,
4,
20,
1105,
1,
46,
104,
999,
1105,
1,
46,
1101,
1000,
1,
20,
4,
20,
1105,
1,
46,
98,
99
]
assert Computer.solve(program, [2]) == [999]
assert Computer.solve(program, [8]) == [1000]
assert Computer.solve(program, [12]) == [1001]
end
end