From 97e4d5284c810de7ae00f16f4752a414fa434799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Rasmussen?= Date: Fri, 11 Dec 2020 10:16:16 +0100 Subject: [PATCH] Day 11 - Take two --- 2020/11/main.py | 80 +++++++++++++++++++++++++++++++++++++++++++--- 2020/11/test-2.txt | 9 ++++++ 2020/11/test-3.txt | 7 ++++ 2020/11/test-4.txt | 3 ++ 4 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 2020/11/test-2.txt create mode 100644 2020/11/test-3.txt create mode 100644 2020/11/test-4.txt diff --git a/2020/11/main.py b/2020/11/main.py index b410cbb..7c632ba 100644 --- a/2020/11/main.py +++ b/2020/11/main.py @@ -29,6 +29,25 @@ class PuzzleSolver: return n + def occupied_in_sight(self, ix, iy): + directions = [( 1, -1), ( 1, 0), ( 1, 1), ( 0, 1), (-1, 1), (-1, 0), (-1, -1), ( 0, -1)] + n = 0 + for dx, dy in directions: + x, y = ix, iy + x += dx + y += dy + while 0 <= y < self.height and 0 <= x < self.width: + s = self.get(x, y) + if s == "#": + n += 1 + break + if s == "L": + break + x += dx + y += dy + + return n + def get(self, x, y): return self.data[y][x] @@ -50,11 +69,43 @@ class PuzzleSolver: return ret + def mutate_in_sight(self): + ret = copy.deepcopy(self.data) + for y in range(self.height): + for x in range(self.width): + s = self.get(x, y) + if s == ".": + continue + + n = self.occupied_in_sight(x, y) + if s == "L": + if n == 0: + ret[y][x] = "#" + elif s == "#": + if n >= 5: + ret[y][x] = "L" + + return ret + def print(self): for y in range(self.height): print("".join(self.data[y])) print() + def solve_in_sight(self): + stable = False + rounds = 0 + self.print() + while not stable: + x = self.mutate_in_sight() + stable = x == self.data + self.data = x + rounds += 1 + + self.print() + + return rounds + def solve(self): stable = False rounds = 0 @@ -73,12 +124,33 @@ class PuzzleSolver: return sum([x.count("#") for x in self.data]) +#ps = PuzzleSolver("test.txt") +#rounds = ps.solve() +#cs = PuzzleSolver("test-1.txt") +#assert ps.data == cs.data +# +#ps = PuzzleSolver("data.txt") +#rounds = ps.solve() +#n = ps.count_occupied() +#print(f"There are {n} occupied seats.") + +ps = PuzzleSolver("test-2.txt") +assert ps.get(3, 4) == "L" +assert ps.occupied_in_sight(3, 4) == 8 + +ps = PuzzleSolver("test-3.txt") +assert ps.get(3, 3) == "L" +assert ps.occupied_in_sight(3, 3) == 0 + +ps = PuzzleSolver("test-4.txt") +assert ps.get(1, 1) == "L" +assert ps.occupied_in_sight(1, 1) == 0 + ps = PuzzleSolver("test.txt") -rounds = ps.solve() -cs = PuzzleSolver("test-1.txt") -assert ps.data == cs.data +ps.solve_in_sight() +assert ps.count_occupied() == 26 ps = PuzzleSolver("data.txt") -rounds = ps.solve() +ps.solve_in_sight() n = ps.count_occupied() print(f"There are {n} occupied seats.") diff --git a/2020/11/test-2.txt b/2020/11/test-2.txt new file mode 100644 index 0000000..a486e35 --- /dev/null +++ b/2020/11/test-2.txt @@ -0,0 +1,9 @@ +.......#. +...#..... +.#....... +......... +..#L....# +....#.... +......... +#........ +...#..... diff --git a/2020/11/test-3.txt b/2020/11/test-3.txt new file mode 100644 index 0000000..68e6543 --- /dev/null +++ b/2020/11/test-3.txt @@ -0,0 +1,7 @@ +.##.##. +#.#.#.# +##...## +...L... +##...## +#.#.#.# +.##.##. diff --git a/2020/11/test-4.txt b/2020/11/test-4.txt new file mode 100644 index 0000000..bb0b813 --- /dev/null +++ b/2020/11/test-4.txt @@ -0,0 +1,3 @@ +............. +.L.L.#.#.#.#. +.............