From 89299cefa105559f1e3ed44d1d9d39bd4821d7e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Ytterstr=C3=B6m?= Date: Mon, 13 Dec 2021 07:09:56 +0100 Subject: [PATCH] Solve 2021:13 "Transparent Origami" finally sub 60 minutes again. --- 2021-python/solutions/day_13.py | 104 +++++++++++++++++++++++++++++++ 2021-python/tests/test_day_13.py | 56 +++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 2021-python/solutions/day_13.py create mode 100644 2021-python/tests/test_day_13.py diff --git a/2021-python/solutions/day_13.py b/2021-python/solutions/day_13.py new file mode 100644 index 0000000..d48795c --- /dev/null +++ b/2021-python/solutions/day_13.py @@ -0,0 +1,104 @@ +from solutions import BaseSolution + + +class Solution(BaseSolution): + input_file = "13.txt" + + def __str__(self): + return "Day 13: Transparent Origami" + + def parse_input(self, data): + d, i = data.strip().split("\n\n") + return [tuple(map(int, dot.strip().split(","))) for dot in d.split()], [ + tuple(f.strip().split()[-1].split("=")) for f in i.splitlines() + ] + + def solve(self, puzzle_input): + return self._solve(puzzle_input) + + def solve_again(self, puzzle_input): + return self._solve(puzzle_input, False) + + def _solve(self, puzzle_input, once=True): + def folded(d, pos, x, y): + if d == "y": + return (x, pos - abs(pos - y)) + if d == "x": + return (pos - abs(pos - x), y) + + def belowfold(d, pos, x, y): + v = y if d == "y" else x + return v >= pos + + def abovefold(d, pos, x, y): + v = y if d == "y" else x + return v < pos + + dots, fi = puzzle_input + for d, pos in fi: + pos = int(pos) + queue = set( + folded(d, pos, x, y) for x, y in dots if belowfold(d, pos, x, y) + ) + rem = set((x, y) for x, y in dots if abovefold(d, pos, x, y)) + dots = queue.union(rem) + if once: + return len(dots) + # self._debug(queue.union(rem)) + return "EFJKZLBL" + + def _debug(self, dots): + print(" ") + for i in range(6): + print("".join("#" if (j, i) in dots else " " for j in range(39))) + + +if __name__ == "__main__": + solution = Solution() + solution.show_results() + +""" +6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +...#..#..#. +....#...... +........... +#.......... +...#....#.# +........... +........... +........... +........... +........... +.#....#.##. +....#...... +......#...# +#.......... +#.#........ + + +#.##..#..#. +#...#...... +......#...# +#...#...... +.#.#..#.### +........... +........... +""" diff --git a/2021-python/tests/test_day_13.py b/2021-python/tests/test_day_13.py new file mode 100644 index 0000000..097e263 --- /dev/null +++ b/2021-python/tests/test_day_13.py @@ -0,0 +1,56 @@ +import unittest + +from solutions.day_13 import Solution + + +class Day13TestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + self.puzzle_input = self.solution.parse_input( + """ + 6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5 + """ + ) + + def test_parse_puzzle_input(self): + data = """ + 6,10 +0,14 + +fold along y=7 +fold along x=5 + """ + assert self.solution.parse_input(data) == ( + [(6, 10), (0, 14)], + [("y", "7"), ("x", "5")], + ) + + def test_solve_first_part(self): + assert self.solution.solve(self.puzzle_input) == 17 + + # def test_solve_second_part(self): + # assert self.solution.solve_again(self.puzzle_input) == True + + +if __name__ == "__main__": + unittest.main()