-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05.py
54 lines (48 loc) · 1.81 KB
/
05.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
51
52
53
54
from aocd import get_data
from copy import deepcopy
program = list(map(int, get_data(day=5, year=2019).split(",")))
def run_program(input_value: int, program):
program_output = pointer = 0
while program[pointer] != 99 and not program_output:
instruction = program[pointer] % 100
param1 = pointer + 1 if program[pointer] // 100 % 10 else program[pointer + 1]
param2 = pointer + 2 if program[pointer] // 1000 else program[pointer + 2]
param3 = program[pointer + 3]
if instruction == 1:
program[param3] = program[param1] + program[param2]
pointer += 4
elif instruction == 2:
program[param3] = program[param1] * program[param2]
pointer += 4
elif instruction == 3:
program[param1] = input_value
pointer += 2
elif instruction == 4:
program_output = program[param1]
pointer += 2
elif instruction == 5:
if program[param1]:
pointer = program[param2]
else:
pointer += 3
elif instruction == 6:
if not program[param1]:
pointer = program[param2]
else:
pointer += 3
elif instruction == 7:
if program[param1] < program[param2]:
program[param3] = 1
else:
program[param3] = 0
pointer += 4
elif instruction == 8:
if program[param1] == program[param2]:
program[param3] = 1
else:
program[param3] = 0
pointer += 4
return program_output
print("2019 Day 05")
print("\tPart 1:", run_program(1, deepcopy(program)))
print("\tPart 2:", run_program(5, deepcopy(program)))