From b6c9f3f016e39cb392a4b2243e0c0b648b9ddf53 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sat, 10 Dec 2022 16:30:02 +1030 Subject: [PATCH] 2022 Day 10 Python --- 2022/day10.py | 25 +++++++++ 2022/input/10 | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 2022/day10.py create mode 100644 2022/input/10 diff --git a/2022/day10.py b/2022/day10.py new file mode 100644 index 0000000..25a4118 --- /dev/null +++ b/2022/day10.py @@ -0,0 +1,25 @@ +from helpers import * + +def run_machine(l): + x = 1 + cycle = 1 + X = np.ones(len(l)*2, dtype=dtype) + for line in l: + ins = line[:4] + if ins == 'addx': + cycle += 2 + X[cycle:] += int(line[5:]) + elif ins == 'noop': + cycle += 1 + X = X[:cycle+1] + I = np.arange(cycle+1) + sig_strength = sum((X*I)[(I+20)%40 == 0]) + print(f'Part 1: Signal strength sampled at 20, 60, 100... = {sig_strength}') + SCREEN_X = I % 40 + SCREEN = np.zeros_like(X) + SCREEN[abs(SCREEN_X-X-1) <= 1] = 1 + print('Part 2: Display output below') + for i in range(1, cycle, 40): + print(''.join(' ' if c==0 else '#' for c in SCREEN[i:i+40])) + +run_machine(read_day(10).split('\n')) diff --git a/2022/input/10 b/2022/input/10 new file mode 100644 index 0000000..5fab235 --- /dev/null +++ b/2022/input/10 @@ -0,0 +1,140 @@ +noop +addx 25 +addx -5 +addx -14 +addx 4 +noop +addx 2 +addx 3 +noop +noop +noop +noop +addx 3 +addx 5 +addx 2 +noop +noop +addx 5 +noop +noop +noop +addx 1 +addx 2 +addx 5 +addx -40 +addx 5 +noop +addx 26 +addx -20 +addx -3 +addx 2 +noop +addx -4 +addx 9 +addx 5 +addx 2 +addx 11 +addx -10 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +noop +addx -31 +addx 32 +addx -37 +addx 1 +addx 8 +addx 13 +addx -15 +addx 4 +noop +addx 5 +noop +addx 3 +addx -2 +addx 4 +addx 1 +addx 4 +addx -14 +addx 15 +addx 4 +noop +noop +noop +addx 3 +addx 5 +addx -40 +noop +addx 5 +addx 8 +addx -3 +noop +addx 2 +addx 9 +addx -4 +noop +noop +noop +noop +addx 5 +addx -9 +addx 10 +addx 4 +noop +noop +addx 5 +addx -19 +addx 24 +addx -2 +addx 5 +addx -40 +addx 22 +addx -19 +addx 2 +addx 5 +addx 2 +addx 5 +noop +noop +addx -2 +addx 2 +addx 5 +addx 3 +noop +addx 2 +addx 2 +addx 3 +addx -2 +addx 10 +addx -3 +addx 3 +noop +addx -40 +addx 2 +addx 11 +addx -5 +addx -1 +noop +addx 3 +addx 7 +noop +addx -2 +addx 5 +addx 2 +addx 3 +noop +addx 2 +addx 6 +addx -5 +addx 2 +addx -18 +addx 26 +addx -1 +noop +noop +noop +noop