summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--10.py50
1 files changed, 23 insertions, 27 deletions
diff --git a/10.py b/10.py
index 27dbde9..e983650 100644
--- a/10.py
+++ b/10.py
@@ -8,35 +8,31 @@ class Cycle:
class Change:
diff: int
-instrs = []
-with open('10.in') as f:
+def instr_gen(f):
for line in f:
match line.rstrip().split():
case ['addx', n]:
- instrs.append(Cycle())
- instrs.append(Cycle())
- instrs.append(Change(int(n)))
+ yield Cycle()
+ yield Cycle()
+ yield Change(int(n))
case ['noop']:
- instrs.append(Cycle())
+ yield Cycle()
-cycle = 0
-sigstr = 0
-X = 1
-screen = [0 for _ in range(240)]
-for instr in instrs:
- match instr:
- case Cycle():
- cycle += 1
- if (cycle - 20) % 40 == 0:
- sigstr += cycle * X
- xpos = (cycle - 1) % 40
- if xpos - 1 <= X <= xpos + 1:
- screen[cycle - 1] = 1
- case Change(diff):
- X += diff
-print(sigstr)
-for y in range(6):
- line = []
- for x in range(40):
- line.append('#' if screen[x + 40 * y] else ' ')
- print(''.join(line))
+with open('10.in') as f:
+ cycle = 0
+ signal_strength = 0
+ X = 1
+ screen = []
+ for instr in instr_gen(f):
+ match instr:
+ case Cycle():
+ cycle += 1
+ if (cycle - 20) % 40 == 0:
+ signal_strength += cycle * X
+ xpos = (cycle - 1) % 40
+ screen.append('#' if xpos - 1 <= X <= xpos + 1 else ' ')
+ if xpos == 39: screen.append('\n')
+ case Change(diff):
+ X += diff
+print(signal_strength)
+print(''.join(screen), end='')