-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday05.py
executable file
·147 lines (128 loc) · 5.05 KB
/
day05.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
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
#!/usr/bin/env python
# DEBUG = True
DEBUG = False
class OP:
ADD = 1
MULT = 2
SAVE = 3
WRITE = 4
JUMP_IF_TRUE = 5
JUMP_IF_FALSE = 6
LESS_THAN = 7
EQUALS = 8
STOP = 99
class MODE:
POSITION = 0
IMMEDIATE = 1
def digit_from_right(x, n):
return x // (10 ** n) % 10
class Computer:
def __init__(self, memory, inputs):
self.memory = memory.copy()
self.inputs = inputs.copy()
self.outputs = []
self.pc = 0
def direct(self, n):
""" Get the direct value of the memory address of the Nth arg, or PC + N"""
return self.memory[self.pc + n]
def lookup(self, n):
""" Get the dereferenced value of the Nth arg, after checking the Nth mode
of the current instruction. """
instruction = self.memory[self.pc]
# If instruction is 105, and n=1, mode is the "1", or the 2nd digit
# from right 0 indexed (3rd when counting naturally)
mode = digit_from_right(instruction, n + 1)
if mode == MODE.POSITION:
return self.memory[self.direct(n)]
if mode == MODE.IMMEDIATE:
return self.direct(n)
raise Exception("Unknown mode")
def info(self, string):
if DEBUG:
print(string)
def execute(self):
while True:
instruction = self.memory[self.pc] % 100
if instruction == OP.ADD:
self.memory[self.direct(3)] = self.lookup(1) + self.lookup(2)
self.info(
f" -> ADD program[{self.direct(3)}] = {self.lookup(1)} + {self.lookup(2)} = {self.lookup(1) + self.lookup(2)}"
)
self.pc += 4
elif instruction == OP.MULT:
self.memory[self.direct(3)] = self.lookup(1) * self.lookup(2)
self.info(
f" -> ADD program[{self.direct(3)}] = {self.lookup(1)} * {self.lookup(2)} = {self.lookup(1) * self.lookup(2)}"
)
self.pc += 4
elif instruction == OP.SAVE:
this_input = self.inputs.pop(0)
self.memory[self.direct(1)] = this_input
self.info(
f" -> SAVE program[{self.direct(1)}] = INPUT = {this_input}"
)
self.pc += 2
elif instruction == OP.WRITE:
self.outputs.append(self.lookup(1))
self.info(f" -> WRITE {self.lookup(1)} = OUTPUT")
self.pc += 2
elif instruction == OP.JUMP_IF_TRUE:
if self.lookup(1) != 0:
self.info(
f" -> JUMP_IF_TRUE [{self.lookup(1)}] != 0, setting i = [{self.lookup(2)}]"
)
self.pc = self.lookup(2)
else:
self.info(
f" -> JUMP_IF_TRUE [{self.lookup(1)}] == 0, doing normal i+= 3"
)
self.pc += 3
elif instruction == OP.JUMP_IF_FALSE:
if self.lookup(1) == 0:
self.info(
f" -> JUMP_IF_FALSE [{self.lookup(1)}] == 0, setting i = [{self.lookup(2)}]"
)
self.pc = self.lookup(2)
else:
self.info(
f" -> JUMP_IF_FALSE [{self.lookup(1)}] != 0, doing normal i+= 3"
)
self.pc += 3
elif instruction == OP.LESS_THAN:
if self.lookup(1) < self.lookup(2):
self.info(
f" -> LESS_THAN [{self.lookup(1)}] < [{self.lookup(2)}], setting program[{self.direct(3)}] = 1"
)
self.memory[self.direct(3)] = 1
else:
self.info(
f" -> LESS_THAN [{self.lookup(1)}] not < [{self.lookup(2)}], setting program[{self.direct(3)}] = 0"
)
self.memory[self.direct(3)] = 0
self.pc += 4
elif instruction == OP.EQUALS:
if self.lookup(1) == self.lookup(2):
self.info(
f" -> EQUALS [{self.lookup(1)}] == [{self.lookup(2)}], setting program[{self.direct(3)}] = 1"
)
self.memory[self.direct(3)] = 1
else:
self.info(
f" -> EQUALS [{self.lookup(1)}] != [{self.lookup(2)}], setting program[{self.direct(3)}] = 0"
)
self.memory[self.direct(3)] = 0
self.pc += 4
elif instruction == OP.STOP:
break
def parse(filename):
return [int(num) for num in open(filename).readline().strip().split(",")]
def solve1(program_in, inputs):
c = Computer(program_in, inputs)
c.execute()
return c.outputs
if __name__ == "__main__":
file_data = parse("../input.txt")
print("Part1: ")
print(solve1(file_data, [1]))
print("Part2: ")
print(solve1(file_data, [5]))