-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10-2.py
executable file
·57 lines (43 loc) · 1.33 KB
/
10-2.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
#!/usr/bin/env python
import numpy
import re
class CPU:
def __init__(self):
self.x = 1
self.cycle = 0
self.crt = numpy.zeros((6, 40), dtype=numpy.bool8)
def advance_timer(self, cycles):
for _ in range(cycles):
beam_row = self.cycle // 40
beam_col = self.cycle % 40
if abs(beam_col - self.x) <= 1:
self.crt[beam_row, beam_col] = 1
self.cycle += 1
def display(self):
s = ''
for row in range(numpy.shape(self.crt)[0]):
for col in range(numpy.shape(self.crt)[1]):
if self.crt[row, col] == 1:
s += "#"
else:
s += "."
s += "\n"
return s
def addx(self, v):
self.advance_timer(2)
self.x += int(v)
def noop(self, _):
self.advance_timer(1)
def __str__(self):
s = f"<{self.__class__.__name__}: X: {self.x}, cycle: {self.cycle}\n"
s += self.display()
s += ">"
return s
instructions = [insn.strip() for insn in open('10.input').readlines()]
instruction_format = re.compile("(?P<inst>addx|noop) ?(?P<oper>-?[0-9]+)?")
cpu = CPU()
for insn in instructions:
m = instruction_format.match(insn)
fun = getattr(cpu, m['inst'])
fun(m['oper'])
print(cpu.display())