2015 Day 18 Python

This commit is contained in:
Luke Hubmayer-Werner 2023-01-16 19:19:01 +10:30
parent a913b90a62
commit 9ebd9303ad
1 changed files with 40 additions and 0 deletions

40
2015/day18.py Normal file
View File

@ -0,0 +1,40 @@
import numpy as np
with open('day18-input', 'r') as file:
lines = file.read().strip().split('\n')
initial_state = np.array([[1 if c == '#' else 0 for c in line] for line in lines], dtype=np.uint8)
def sim_step(state):
adjacent_on = np.zeros_like(initial_state, dtype=np.uint8)
adjacent_on[:-1,:] += state[1:,:] # Right neighbour
adjacent_on[1:,:] += state[:-1,:] # Left neighbour
adjacent_on[:,:-1] += state[:,1:] # Down
adjacent_on[:,1:] += state[:,:-1] # Up
adjacent_on[:-1,1:] += state[1:,:-1] # Up-right
adjacent_on[1:,1:] += state[:-1,:-1] # Up-left
adjacent_on[:-1,:-1] += state[1:,1:] # Down-right
adjacent_on[1:,:-1] += state[:-1,1:] # Down-left
output = np.zeros_like(state)
output[state>0] = (adjacent_on[state>0] == 2)
output[state>0] |= (adjacent_on[state>0] == 3)
output[state==0] = (adjacent_on[state==0] == 3)
return output
state = initial_state
for i in range(100):
state = sim_step(state)
print(f'Part 1: {state.sum()} lights are on after 100 steps')
def set_corners(state):
state[0,0] = 1
state[-1,0] = 1
state[0,-1] = 1
state[-1,-1] = 1
state = np.copy(initial_state)
set_corners(state)
for i in range(100):
state = sim_step(state)
set_corners(state)
print(f'Part 2: {state.sum()} lights are on after 100 steps with locked corners')