-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02_class.py
72 lines (54 loc) · 1.46 KB
/
day02_class.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
from itertools import count
from operator import add, mul
def pointer_jump(n):
"""Adds a .jump attribute to a function"""
def dec(f):
f.jump = n
return f
class IntCode(list):
def init(self, intcode):
super().__init__(intcode)
def traverse(self):
pointer = 0
operations = {
1: self.add,
2: self.mul,
99: self.stop,
}
while True:
opcode = self[pointer]
operation = operations(opcode)
try:
operation(pointer)
except StopIteration:
return
pointer += operation.jump
@pointer_jump(1)
def stop(self, pos):
raise StopIteration
@pointer_jump(4)
def add(self, pos):
posx, posy, pos_result = self[pos+1:pos+4]
self[pos_result] = self[posx] + self[posy]
@pointer_jump(4)
def mul(self, pos):
posx, posy, pos_result = self[pos+1:pos+4]
self[pos_result] = self[posx] * self[posy]
target = 19690720
with open('day2.in') as input_file:
intcode_ = [int(n) for n in input_file.read().split(',')]
operations = {1: add, 2: mul}
nv = ((n, v) for n in range(100) for v in range(100))
for noun, verb in nv:
intcode = list(intcode_)
intcode[1:3] = noun, verb
for pos in count(start=0, step=4):
if intcode[pos] == 99:
break
opcode, posx, posy, pos_result = intcode[pos:pos+4]
operation = operations[opcode]
x = intcode[posx]
y = intcode[posy]
intcode[pos_result] = operation(x, y)
if intcode[0] == target:
print('Result =', 100 * noun + verb)