-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.py
50 lines (34 loc) · 836 Bytes
/
day2.py
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
def calculate(values):
i = 0
while True:
op = values[i + 0]
if op == 99:
break
in1 = values[values[i + 1]]
in2 = values[values[i + 2]]
if op == 1:
values[values[i + 3]] = in1 + in2
elif op == 2:
values[values[i + 3]] = in1 * in2
else:
print(f'invalid opcode {op}')
return []
i += 4
return values
def calculate2(values, limit):
for noun in range(0, 100):
for verb in range(0, 100):
part2 = values.copy()
part2[1] = noun
part2[2] = verb
if calculate(part2)[0] == limit:
return 100 * noun + verb
return 0
if __name__ == '__main__':
with open('input/day2.txt') as stream:
values = [int(p) for p in stream.read().split(',')]
part1 = values.copy()
part1[1] = 12
part1[2] = 2
print(f'Part1: {calculate(part1)[0]}')
print(f'Part2: {calculate2(values, 19690720)}')