mirror of
https://github.com/thib8956/advent-of-code.git
synced 2025-08-24 00:11:57 +00:00
chore: create new project structure and aoc.py runner script
This commit is contained in:
34
adventofcode/2020/day11/day11.py
Normal file
34
adventofcode/2020/day11/day11.py
Normal file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3
|
||||
from rules import part1_rules, part2_rules
|
||||
|
||||
|
||||
def main(grid, rules):
|
||||
generation = 0
|
||||
while True:
|
||||
changes, next_grid = step(grid, rules)
|
||||
generation += 1
|
||||
grid = next_grid
|
||||
assert generation < 1000
|
||||
if generation % 10 == 0:
|
||||
print(f"Generation {generation}, changes: {changes}")
|
||||
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__":
|
||||
import sys
|
||||
with open(sys.argv[1]) as infile:
|
||||
grid = list("".join(infile.read().splitlines()))
|
||||
print("Part 1 ", main(grid, rules=part1_rules))
|
||||
print("Part 2 ", main(grid, rules=part2_rules))
|
Reference in New Issue
Block a user