Skip to content

Commit bc829d2

Browse files
author
patrick.fischer
committed
d2p2 succession
1 parent 2b6dcbe commit bc829d2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

day2p2.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
Stepping forward 4 more positions arrives at opcode 99, halting the program.
3+
4+
Here are the initial and final states of a few more small programs:
5+
6+
1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
7+
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
8+
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
9+
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
10+
'''
11+
from typing import List
12+
import numpy as np
13+
14+
15+
def compute(arg: List[int]) -> List[int]:
16+
nInstr = (len(arg)//4) if len(arg) % 4 == 0 else (len(arg)//4+1)
17+
for k in range(nInstr):
18+
idx = k*4
19+
opcode = arg[idx]
20+
if opcode == 99:
21+
break
22+
val1idx = arg[idx+1]
23+
val2idx = arg[idx+2]
24+
resIdx = arg[idx+3]
25+
if opcode == 1:
26+
arg[resIdx] = arg[val1idx] + arg[val2idx]
27+
elif opcode == 2:
28+
arg[resIdx] = arg[val1idx] * arg[val2idx]
29+
else:
30+
break
31+
# raise Exception('no valid opcode')
32+
return arg
33+
34+
35+
# assert np.array_equal(compute([1, 0, 0, 0, 99]), [2, 0, 0, 0, 99])
36+
# assert np.array_equal(compute([2, 3, 0, 3, 99]), [2, 3, 0, 6, 99])
37+
# assert np.array_equal(compute([2,4,4,5,99,0]), [2,4,4,5,99,9801])
38+
# assert np.array_equal(compute([1,1,1,4,99,5,6,0,99]), [30,1,1,4,2,5,6,0,99])
39+
40+
with open('day02-Input.txt') as f:
41+
instr = f.readline().strip().split(',')
42+
for noun in range(99):
43+
for verb in range(99):
44+
inval = list(map(int, instr))
45+
inval[1] = noun
46+
inval[2] = verb
47+
# print(inval)
48+
res = compute(inval)
49+
if res[0] == 19690720:
50+
print(noun, verb, res[0])
51+
print(100*noun + verb)
52+
break

0 commit comments

Comments
 (0)