-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_a.py
executable file
·72 lines (57 loc) · 1.91 KB
/
part_a.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
import utils
class Challenge(utils.BaseChallenge):
def solve(self, _input, debug=False):
"""
>>> Challenge().default_solve()
2890696
"""
return get_program_result(_input, {1: 12, 2: 2})
def get_program_result(program_text, substitutions=None):
if substitutions:
program = parse_program(program_text)
for position, substitution in substitutions.items():
program[position] = substitution
program_text = serialise_program(program)
result_text = run_program(program_text)
result = parse_program(result_text)
return result[0]
def run_program(program_text):
"""
>>> run_program("1,9,10,3,2,3,11,0,99,30,40,50")
'3500,9,10,70,2,3,11,0,99,30,40,50'
>>> run_program("1,0,0,0,99")
'2,0,0,0,99'
>>> run_program("2,3,0,3,99")
'2,3,0,6,99'
>>> run_program("2,4,4,5,99,0")
'2,4,4,5,99,9801'
>>> run_program("1,1,1,4,99,5,6,0,99")
'30,1,1,4,2,5,6,0,99'
"""
program = parse_program(program_text)
program_counter = 0
while True:
op_code = program[program_counter]
if op_code == 99:
break
if op_code not in (1, 2):
raise Exception(f"Unknown op code {op_code}")
pointer_1, pointer_2, pointer_3 = \
program[program_counter + 1:program_counter + 4]
program_counter += 4
value_1, value_2 = program[pointer_1], program[pointer_2]
if op_code == 1:
result = value_1 + value_2
elif op_code == 2:
result = value_1 * value_2
else:
raise Exception(f"Unknown op code {op_code}")
program[pointer_3] = result
return serialise_program(program)
def parse_program(program_text):
return list(map(int, program_text.split(",")))
def serialise_program(program):
return ",".join(map(str, program))
Challenge.main()
challenge = Challenge()