advent-of-code/2020/day11/day11.py

34 lines
896 B
Python
Raw Normal View History

2024-12-03 14:27:19 +00:00
#!/usr/bin/env python3
2024-12-03 15:21:15 +00:00
from rules import part1_rules, part2_rules
2024-12-03 14:27:19 +00:00
def main(grid, rules):
generation = 0
while True:
changes, next_grid = step(grid, rules)
generation += 1
grid = next_grid
assert generation < 1000
2024-12-03 15:21:15 +00:00
if generation % 10 == 0:
print(f"Generation {generation}, changes: {changes}")
2024-12-03 14:27:19 +00:00
if changes == 0:
return next_grid.count("#")
def step(grid, rules):
changes = 0
next_grid = grid[:]
for index, cell in enumerate(grid):
try:
changes += rules[cell](index, grid, next_grid)
except KeyError:
pass
return changes, next_grid
if __name__ == "__main__":
with open("input.txt") as infile:
grid = list("".join(infile.read().splitlines()))
2024-12-03 15:21:15 +00:00
print("Part 1 ", main(grid, rules=part1_rules))
print("Part 2 ", main(grid, rules=part2_rules))