-
Notifications
You must be signed in to change notification settings - Fork 3
/
day13_test.go
471 lines (432 loc) · 10.8 KB
/
day13_test.go
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
// Package main provides ...
package main
import (
"reflect"
"testing"
)
func TestDay13Part1(t *testing.T) {
program := Parse("../../13/input.txt")
got := Day13Part1(program)
want := 270
if want != got {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay13Part2(t *testing.T) {
program := Parse("../../13/input.txt")
got := Day13Part2(program)
want := 12535
if want != got {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay11Part1(t *testing.T) {
program := Parse("../../11/input.txt")
grid1 := PainterRobot(program, 0)
got := NumPaintedSquares(grid1)
want := 2539
if want != got {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay11Part2(t *testing.T) {
program := Parse("../../11/input.txt")
grid2 := PainterRobot(program, 1)
got := NumPaintedSquares(grid2)
want := 248
if want != got {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay9Part1(t *testing.T) {
program := Parse("../../09/input.txt")
got := Solve(program, []int{1})
want := []int{3780860499}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay9Part2(t *testing.T) {
program := Parse("../../09/input.txt")
got := Solve(program, []int{2})
want := []int{33343}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay9TestProgs(t *testing.T) {
quineProg := []int{109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99}
tests := []struct {
name string
prog []int
output []int
}{
{
name: "day 9 test 1",
prog: quineProg,
output: quineProg,
},
{
name: "day 9 test 2",
prog: []int{1102, 34915192, 34915192, 7, 4, 7, 99, 0},
output: []int{1219070632396864},
},
{
name: "day 9 test 3",
prog: []int{104, 1125899906842624, 99},
output: []int{1125899906842624},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := Solve(test.prog, []int{})
want := test.output
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func TestPauseOnMissingInput(t *testing.T) {
// Run this program with no inputs
program := []int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}
inputs := []int{}
c := NewComputer(program, inputs)
c.Execute()
// C should be waiting for input
if !c.WaitingForInput {
t.Errorf("computer not waiting for input")
}
// Add input 8 and execute
c.AddInput(8)
c.Execute()
// Should no longer be waiting for input
if c.WaitingForInput {
t.Errorf("computer waiting for input inappropriately")
}
// Should be halted
if !c.Halted {
t.Errorf("computer not halted")
}
// Outputs should have 1 in them
got := c.Outputs
want := []int{1}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay7Part1(t *testing.T) {
program := Parse("../../07/input.txt")
p1MaxSeq, p1MaxVal := AmplifyOnceMaxSeq(program)
wantMaxSeq := []int{2, 0, 3, 1, 4}
wantMaxVal := 13848
if !reflect.DeepEqual(p1MaxSeq, wantMaxSeq) {
t.Errorf("got %v want %v", p1MaxSeq, wantMaxSeq)
}
if p1MaxVal != wantMaxVal {
t.Errorf("got %v want %v", p1MaxVal, wantMaxVal)
}
}
func TestDay7Part2(t *testing.T) {
program := Parse("../../07/input.txt")
p2MaxSeq, p2MaxVal := AmplifyLoopMaxSeq(program)
wantMaxSeq := []int{6, 8, 7, 5, 9}
wantMaxVal := 12932154
if !reflect.DeepEqual(p2MaxSeq, wantMaxSeq) {
t.Errorf("got %v want %v", p2MaxSeq, wantMaxSeq)
}
if p2MaxVal != wantMaxVal {
t.Errorf("got %v want %v", p2MaxVal, wantMaxVal)
}
}
func TestPopOutput(t *testing.T) {
program := []int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}
inputs := []int{8}
c := NewComputer(program, inputs)
c.Execute()
oldLen := len(c.Outputs)
popped, err := c.PopOutput()
newLen := len(c.Outputs)
if err != nil {
t.Errorf("TestPopOutput: didn't expect an error")
}
if newLen+1 != oldLen {
t.Errorf("Popping didn't remove an output")
}
if popped != 1 {
t.Errorf("Incorrect value popped off")
}
}
func TestAmplifyOnce(t *testing.T) {
progA1 := []int{3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0}
progA2 := []int{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}
progA3 := []int{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}
tests := []struct {
name string
prog []int
phaseSeq []int
wantVal int
}{
{
"A1", progA1, []int{4, 3, 2, 1, 0}, 43210,
},
{
"A2", progA2, []int{0, 1, 2, 3, 4}, 54321,
},
{
"A3", progA3, []int{1, 0, 4, 3, 2}, 65210,
},
}
for _, test := range tests {
// First: Check the correct answer with AmplifyOnce
t.Run(test.name+" T1", func(t *testing.T) {
got := AmplifyOnce(test.prog, test.phaseSeq)
want := test.wantVal
if got != want {
t.Errorf("got %v want %v", got, want)
}
})
// Second: See if we can generate the correct answer with AmplifyOnceMaxSeq
t.Run(test.name+" T1", func(t *testing.T) {
gotSeq, gotVal := AmplifyOnceMaxSeq(test.prog)
if !reflect.DeepEqual(gotSeq, test.phaseSeq) {
t.Errorf("got %v want %v", gotSeq, test.phaseSeq)
}
if gotVal != test.wantVal {
t.Errorf("got %v want %v", gotVal, test.wantVal)
}
})
}
}
func TestAmplifyLoop(t *testing.T) {
progB1 := []int{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}
progB2 := []int{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}
tests := []struct {
name string
prog []int
phaseSeq []int
wantVal int
}{
{
"B1", progB1, []int{9, 8, 7, 6, 5}, 139629729,
},
{
"B2", progB2, []int{9, 7, 8, 5, 6}, 18216,
},
}
for _, test := range tests {
// First: Check the correct answer with AmplifyOnce
t.Run(test.name+" T1", func(t *testing.T) {
got := AmplifyLoop(test.prog, test.phaseSeq)
want := test.wantVal
if got != want {
t.Errorf("got %v want %v", got, want)
}
})
// Second: See if we can generate the correct answer with AmplifyOnceMaxSeq
t.Run(test.name+" T1", func(t *testing.T) {
gotSeq, gotVal := AmplifyLoopMaxSeq(test.prog)
if !reflect.DeepEqual(gotSeq, test.phaseSeq) {
t.Errorf("got %v want %v", gotSeq, test.phaseSeq)
}
if gotVal != test.wantVal {
t.Errorf("got %v want %v", gotVal, test.wantVal)
}
})
}
}
func TestDigitFromRight(t *testing.T) {
tests := []struct {
num int
i int
expected int
}{
{12345, 0, 5},
{12345, 1, 4},
{12345, 2, 3},
{12345, 3, 2},
{12345, 4, 1},
{12345, 5, 0},
{498, 0, 8},
{498, 1, 9},
{498, 2, 4},
{498, 3, 0},
{498, 4, 0},
{498, 5, 0},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := DigitFromRight(test.num, test.i)
want := test.expected
if got != want {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func TestDay5Part1(t *testing.T) {
program := Parse("../../05/input.txt")
got := Solve(program, []int{1})
want := []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 5821753}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
func TestDay5Part2(t *testing.T) {
program := Parse("../../05/input.txt")
got := Solve(program, []int{5})
want := []int{11956381}
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
}
func Test81(t *testing.T) {
program := []int{3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8}
tests := []struct {
inputs []int
want []int
}{
{[]int{7}, []int{0}},
{[]int{8}, []int{1}},
{[]int{9}, []int{0}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func Test82(t *testing.T) {
program := []int{3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8}
tests := []struct {
inputs []int
want []int
}{
{[]int{7}, []int{1}},
{[]int{8}, []int{0}},
{[]int{9}, []int{0}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func Test83(t *testing.T) {
program := []int{3, 3, 1108, -1, 8, 3, 4, 3, 99}
tests := []struct {
inputs []int
want []int
}{
{[]int{7}, []int{0}},
{[]int{8}, []int{1}},
{[]int{9}, []int{0}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func Test84(t *testing.T) {
program := []int{3, 3, 1107, -1, 8, 3, 4, 3, 99}
tests := []struct {
inputs []int
want []int
}{
{[]int{7}, []int{1}},
{[]int{8}, []int{0}},
{[]int{9}, []int{0}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func TestJump1(t *testing.T) {
program := []int{3, 12, 6, 12, 15, 1, 13, 14, 13, 4, 13, 99, -1, 0, 1, 9}
tests := []struct {
inputs []int
want []int
}{
{[]int{0}, []int{0}},
{[]int{10}, []int{1}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func TestJump2(t *testing.T) {
program := []int{3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1}
tests := []struct {
inputs []int
want []int
}{
{[]int{0}, []int{0}},
{[]int{10}, []int{1}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func TestLonger(t *testing.T) {
program := []int{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}
tests := []struct {
inputs []int
want []int
}{
{[]int{2}, []int{999}},
{[]int{8}, []int{1000}},
{[]int{12}, []int{1001}},
}
for _, test := range tests {
t.Run("whatever", func(t *testing.T) {
got := Solve(program, test.inputs)
want := test.want
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v want %v", got, want)
}
})
}
}
func BenchmarkSolve05Part1(b *testing.B) {
program := Parse("../../05/input.txt")
for i := 0; i < b.N; i++ {
Solve(program, []int{1})
}
}
func BenchmarkSolve05Part2(b *testing.B) {
program := Parse("../../05/input.txt")
for i := 0; i < b.N; i++ {
Solve(program, []int{5})
}
}