Add day 10 solution
This commit is contained in:
parent
407ff5c036
commit
69fe5ef070
42
10/cpu.py
Normal file
42
10/cpu.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
|
||||||
|
import fileinput
|
||||||
|
|
||||||
|
class CPU:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.reg_x = 1
|
||||||
|
self.cycle = 0
|
||||||
|
|
||||||
|
def draw(self):
|
||||||
|
pos = self.cycle - 1
|
||||||
|
if pos % 40 == 0 and pos != 0:
|
||||||
|
print('')
|
||||||
|
print("#" if abs((pos % 40) - self.reg_x) <= 1 else ".", end='')
|
||||||
|
if self.cycle == 240:
|
||||||
|
print("")
|
||||||
|
|
||||||
|
def exec(self, instructions, breaks=[]):
|
||||||
|
for instruction in instructions:
|
||||||
|
# print(f"Start cycle {self.cycle:3d}: begin executing {instruction}")
|
||||||
|
if instruction == "noop":
|
||||||
|
self.cycle += 1
|
||||||
|
if self.cycle in breaks:
|
||||||
|
yield self.cycle*self.reg_x
|
||||||
|
self.draw()
|
||||||
|
elif instruction.startswith("addx"):
|
||||||
|
self.cycle += 1
|
||||||
|
if self.cycle in breaks:
|
||||||
|
yield self.cycle*self.reg_x
|
||||||
|
self.draw()
|
||||||
|
self.cycle += 1
|
||||||
|
if self.cycle in breaks:
|
||||||
|
yield self.cycle*self.reg_x
|
||||||
|
self.draw()
|
||||||
|
self.reg_x += int(instruction.split(' ')[1])
|
||||||
|
else:
|
||||||
|
print("Unknown instruction")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cpu = CPU()
|
||||||
|
print(f"Solution A: {sum(cpu.exec(filter(lambda x: x != '', map(lambda x: x.strip(), fileinput.input())), [20, 60, 100, 140, 180, 220]))}")
|
||||||
|
|
146
10/example
Normal file
146
10/example
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
addx 15
|
||||||
|
addx -11
|
||||||
|
addx 6
|
||||||
|
addx -3
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -8
|
||||||
|
addx 13
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -35
|
||||||
|
addx 1
|
||||||
|
addx 24
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 16
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 21
|
||||||
|
addx -15
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -3
|
||||||
|
addx 9
|
||||||
|
addx 1
|
||||||
|
addx -3
|
||||||
|
addx 8
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -36
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 6
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx 13
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx -33
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 8
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -9
|
||||||
|
addx 1
|
||||||
|
addx 1
|
||||||
|
addx -3
|
||||||
|
addx 11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
addx 26
|
||||||
|
addx -30
|
||||||
|
addx 12
|
||||||
|
addx -1
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -9
|
||||||
|
addx 18
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 9
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 2
|
||||||
|
addx -37
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 15
|
||||||
|
addx -21
|
||||||
|
addx 22
|
||||||
|
addx -6
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 20
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx -6
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
Loading…
Reference in New Issue
Block a user