Add day 11 solution
This commit is contained in:
80
day-11/day-11.py
Normal file
80
day-11/day-11.py
Normal file
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def part_1(input):
|
||||
result = 0
|
||||
m = {}
|
||||
for y, line in enumerate(input):
|
||||
for x, d in enumerate(line.rstrip()):
|
||||
m[(x, y)] = int(d)
|
||||
for _ in range(100):
|
||||
pending_flash = set()
|
||||
flashed = set()
|
||||
for k in m:
|
||||
m[k] += 1
|
||||
if m[k] > 9:
|
||||
pending_flash.add(k)
|
||||
while len(pending_flash):
|
||||
k = pending_flash.pop()
|
||||
flashed.add(k)
|
||||
result += 1
|
||||
min_x = max(k[0] - 1, 0)
|
||||
max_x = min(k[0] + 2, 10)
|
||||
min_y = max(k[1] - 1, 0)
|
||||
max_y = min(k[1] + 2, 10)
|
||||
for y in range(min_y, max_y):
|
||||
for x in range(min_x, max_x):
|
||||
m[(x, y)] += 1
|
||||
if m[(x, y)] > 9 and (x, y) not in pending_flash and (x, y) not in flashed:
|
||||
pending_flash.add((x, y))
|
||||
for k in flashed:
|
||||
m[k] = 0
|
||||
# for y in range(10):
|
||||
# for x in range(10):
|
||||
# print(m[(x, y)], end='')
|
||||
# print()
|
||||
# print()
|
||||
print("Part 1 result:", result)
|
||||
|
||||
|
||||
def part_2(input):
|
||||
result = 0
|
||||
m = {}
|
||||
for y, line in enumerate(input):
|
||||
for x, d in enumerate(line.rstrip()):
|
||||
m[(x, y)] = int(d)
|
||||
s = 0
|
||||
while not result:
|
||||
s += 1
|
||||
pending_flash = set()
|
||||
flashed = set()
|
||||
for k in m:
|
||||
m[k] += 1
|
||||
if m[k] > 9:
|
||||
pending_flash.add(k)
|
||||
while len(pending_flash):
|
||||
k = pending_flash.pop()
|
||||
flashed.add(k)
|
||||
min_x = max(k[0] - 1, 0)
|
||||
max_x = min(k[0] + 2, 10)
|
||||
min_y = max(k[1] - 1, 0)
|
||||
max_y = min(k[1] + 2, 10)
|
||||
for y in range(min_y, max_y):
|
||||
for x in range(min_x, max_x):
|
||||
m[(x, y)] += 1
|
||||
if m[(x, y)] > 9 and (x, y) not in pending_flash and (x, y) not in flashed:
|
||||
pending_flash.add((x, y))
|
||||
for k in flashed:
|
||||
m[k] = 0
|
||||
if len(flashed) == 100:
|
||||
result = s
|
||||
print("Part 2 result:", result)
|
||||
|
||||
|
||||
input = list()
|
||||
p = Path(__file__).with_name('input.txt')
|
||||
with open(p) as f:
|
||||
input = f.readlines()
|
||||
part_1(input)
|
||||
part_2(input)
|
||||
10
day-11/input.txt
Normal file
10
day-11/input.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
2682551651
|
||||
3223134263
|
||||
5848471412
|
||||
7438334862
|
||||
8731321573
|
||||
6415233574
|
||||
5564726843
|
||||
6683456445
|
||||
8582346112
|
||||
4617588236
|
||||
Reference in New Issue
Block a user