AdventOfCode/2021/day20.py

32 lines
1.3 KiB
Python
Raw Permalink Normal View History

2021-12-21 00:24:30 +10:30
import numpy as np
2021-12-21 13:42:01 +10:30
from sys import argv
filename = argv[1] if len(argv)>1 else 'day20input'
2021-12-21 00:24:30 +10:30
def unfold(func, arg, n):
2021-12-21 00:24:30 +10:30
for _ in range(n):
arg = func(arg)
yield arg
mapping = {'#':1, '.':0}
2021-12-21 13:42:01 +10:30
with open(filename, 'r') as file:
2021-12-21 00:24:30 +10:30
rawinput = file.read().strip()
cipher_str, _, input_str = rawinput.partition('\n\n')
2021-12-21 13:42:01 +10:30
cipher = np.array([mapping[c] for c in cipher_str], dtype=np.int8)
input_arr = np.array([[mapping[c] for c in string] for string in input_str.split('\n')], dtype=np.int8)
2021-12-21 00:24:30 +10:30
roll_offsets = [(x,y) for y in [1,0,-1] for x in [1,0,-1]] # Note roll is opposite direction to our sampling i.e. (1,1) means (0,0) is old (-1,-1)
def sample(array):
2021-12-21 13:42:01 +10:30
#lookup_values = sum([2**(8-n)*np.roll(array, offset, (1,0)) for n,offset in enumerate(roll_offsets)]) # Note axis 1 is x axis 0 is y.
lookup_values = np.sum([2**(8-n)*np.roll(array, offset, (1,0)) for n,offset in enumerate(roll_offsets)], 0, dtype=np.int16) # Note axis 1 is x axis 0 is y.
2021-12-21 00:24:30 +10:30
return cipher[lookup_values]
def visualize(array):
print('\n'.join(''.join(['.','#'][c] for c in row) for row in array))
simulated_lights = [t.sum() for t in unfold(sample, np.pad(input_arr, 51), 50)]
2021-12-21 00:24:30 +10:30
print('Part 1: Lights after 2 enhancements:', simulated_lights[2-1])
print('Part 2: Lights after 50 enhancements:', simulated_lights[50-1])