Finished day10 in python.

This commit is contained in:
peacememories 2022-12-18 01:01:36 +01:00
parent 01909909be
commit c46b4f6f40

36
python/day10.py Executable file
View file

@ -0,0 +1,36 @@
#!/usr/bin/env python3
def run_process(input):
x = 1
for line in input.split("\n"):
match line.split(" "):
case ["noop"]:
yield x
case ["addx", addNum]:
yield x
yield x
x += int(addNum)
case _:
raise ValueError(f"Could not parse command: {line}")
with open("inputs/day10.txt", "r") as f:
file_content = f.read().strip()
total_strength = 0
display = ""
for id, x in enumerate(run_process(file_content)):
if id+1 in [20, 60, 100, 140, 180, 220]:
total_strength += x*(id+1)
screen_id = id % 40
if abs(screen_id - x) < 2:
display += "#"
else:
display += "."
if screen_id == 39:
display += "\n"
print(total_strength)
print(display)