Solve 2021:13 "Transparent Origami"
finally sub 60 minutes again.
This commit is contained in:
parent
c0d352cdf1
commit
89299cefa1
2 changed files with 160 additions and 0 deletions
104
2021-python/solutions/day_13.py
Normal file
104
2021-python/solutions/day_13.py
Normal file
|
|
@ -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
|
||||
|
||||
...#..#..#.
|
||||
....#......
|
||||
...........
|
||||
#..........
|
||||
...#....#.#
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
...........
|
||||
.#....#.##.
|
||||
....#......
|
||||
......#...#
|
||||
#..........
|
||||
#.#........
|
||||
|
||||
|
||||
#.##..#..#.
|
||||
#...#......
|
||||
......#...#
|
||||
#...#......
|
||||
.#.#..#.###
|
||||
...........
|
||||
...........
|
||||
"""
|
||||
56
2021-python/tests/test_day_13.py
Normal file
56
2021-python/tests/test_day_13.py
Normal file
|
|
@ -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()
|
||||
Loading…
Add table
Reference in a new issue