* Solve 2015:16 "Aunt Sue" * Make 2023:08 future compatible Code used to work with another version of python. * Solve 2015:17 "No such Thing as Too much" * Solve 2015:18 "Like a GIF For Your Yard" Also solve 2015:06 just in case, was just a ref in the end. * Solve 2015:19 "Medicine for Rudolph" * Solve 2015:20 "Infinite Elves and Infinite Houses" * Solve 2023:21 "RPG Simulator 20XX" * Solve 2015:22 "Wizard Simulator 20XX" * Solve 2015:23 "Opening the Turing Lock" * Solve 2015:25 "Let it Snow" Wrote p2rc and rc2p just for academic purposes. Puzzles could be solved anyway. * Solve 2015:24 "Hangs in the Balance" --------- Co-authored-by: Anders Englöf Ytterström <anders@playmaker.ai>
79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
from collections import defaultdict
|
|
from solutions import BaseSolution
|
|
|
|
|
|
N = [
|
|
(-1, -1),
|
|
(-1, 0),
|
|
(-1, 1),
|
|
(0, -1),
|
|
(0, 1),
|
|
(1, -1),
|
|
(1, 0),
|
|
(1, 1),
|
|
]
|
|
|
|
|
|
class Solution(BaseSolution):
|
|
input_file = "18.txt"
|
|
|
|
def __str__(self):
|
|
return "Day 18: Like a GIF For Your Yard"
|
|
|
|
def parse_input(self, data):
|
|
return data.strip()
|
|
|
|
def solve(self, data):
|
|
m = defaultdict(bool)
|
|
rows = data.split()
|
|
h = len(rows)
|
|
w = len(rows[0])
|
|
for r in range(h):
|
|
for c in range(w):
|
|
m[(r, c)] = 1 if rows[r][c] == "#" else 0
|
|
for _ in range(100):
|
|
nm = defaultdict(bool)
|
|
for r in range(h):
|
|
for c in range(w):
|
|
n = sum(m[(r + nr, c + nc)] for nr, nc in N)
|
|
match m[(r, c)]:
|
|
case 1:
|
|
nm[(r, c)] = n in (2, 3)
|
|
case 0:
|
|
nm[(r, c)] = n == 3
|
|
m = nm
|
|
return sum(m.values())
|
|
|
|
def solve_again(self, data):
|
|
m = defaultdict(bool)
|
|
rows = data.split()
|
|
h = len(rows)
|
|
w = len(rows[0])
|
|
for r in range(h):
|
|
for c in range(w):
|
|
m[(r, c)] = 1 if rows[r][c] == "#" else 0
|
|
m[(0, 0)] = True
|
|
m[(99, 0)] = True
|
|
m[(99, 99)] = True
|
|
m[(0, 99)] = True
|
|
for _ in range(100):
|
|
nm = defaultdict(bool)
|
|
for r in range(h):
|
|
for c in range(w):
|
|
if (r, c) in [(0, 0), (99, 0), (99, 99), (0, 99)]:
|
|
nm[(r, c)] = True
|
|
continue
|
|
n = sum(m[(r + nr, c + nc)] for nr, nc in N)
|
|
match m[(r, c)]:
|
|
case 1:
|
|
nm[(r, c)] = n in (2, 3)
|
|
case 0:
|
|
nm[(r, c)] = n == 3
|
|
m = nm
|
|
return sum(m.values())
|
|
return True
|
|
|
|
|
|
if __name__ == "__main__":
|
|
solution = Solution()
|
|
solution.show_results()
|