2022 Day 10 Python

This commit is contained in:
Luke Hubmayer-Werner 2022-12-10 16:30:02 +10:30
parent c676a77fb5
commit b6c9f3f016
2 changed files with 165 additions and 0 deletions

25
2022/day10.py Normal file
View File

@ -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'))

140
2022/input/10 Normal file
View File

@ -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