From 8f5d73b4bfdca89331d71c5279bf2f18dd2372be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Wed, 25 Oct 2023 23:24:51 +0200 Subject: [PATCH] Solve 2015:9 "All in a Single Night" --- 2015-python/solutions/day_09.py | 54 ++++++++++++++++++++++++++++++++ 2015-python/tests/test_day_06.py | 29 ----------------- 2015-python/tests/test_day_09.py | 31 ++++++++++++++++++ 3 files changed, 85 insertions(+), 29 deletions(-) create mode 100644 2015-python/solutions/day_09.py delete mode 100644 2015-python/tests/test_day_06.py create mode 100644 2015-python/tests/test_day_09.py diff --git a/2015-python/solutions/day_09.py b/2015-python/solutions/day_09.py new file mode 100644 index 0000000..28041b8 --- /dev/null +++ b/2015-python/solutions/day_09.py @@ -0,0 +1,54 @@ +from itertools import permutations +from re import findall +from solutions import BaseSolution + + +class Solution(BaseSolution): + input_file = "09.txt" + + def __str__(self): + return "Day 9: All in a Single Night" + + def parse_input(self, data): + def fromtodest(line): + f, t, d = findall(r"(\w+) to (\w+) = (\d+)", line)[0] + return f, t, int(d) + + return [fromtodest(line) for line in data.strip().splitlines()] + + def solve(self, puzzle_input): + return min(self._distances(puzzle_input)) + + def solve_again(self, puzzle_input): + return max(self._distances(puzzle_input)) + + def _distances(self, puzzle_input): + places = set( + list(map(lambda sdd: sdd[0], puzzle_input)) + + list(map(lambda ssd: ssd[1], puzzle_input)) + ) + route_len = len(places) + + return [ + sum( + sum( + map( + lambda ssd: ssd[2], + filter( + lambda ssd: ( + (ssd[0] == route[i] and ssd[1] == route[i + 1]) + or (ssd[0] == route[i + 1] and ssd[1] == route[i]) + ), + puzzle_input, + ), + ) + ) + for i in range(route_len - 1) + ) + for route in permutations(places) + ] + + +if __name__ == "__main__": + solution = Solution() + solution.show_results() diff --git a/2015-python/tests/test_day_06.py b/2015-python/tests/test_day_06.py deleted file mode 100644 index aeb9aef..0000000 --- a/2015-python/tests/test_day_06.py +++ /dev/null @@ -1,29 +0,0 @@ -import unittest - -from solutions.day_06 import Solution - - -class Day06TestCase(unittest.TestCase): - def setUp(self): - self.solution = Solution() - self.puzzle_input = self.solution.parse_input( - """ - - """ - ) - - def test_parse_puzzle_input(self): - data = """ - - """ - assert self.solution.parse_input(data) == "" - - # def test_solve_first_part(self): - # assert self.solution.solve(self.puzzle_input) == True - - # def test_solve_second_part(self): - # assert self.solution.solve_again(self.puzzle_input) == True - - -if __name__ == "__main__": - unittest.main() diff --git a/2015-python/tests/test_day_09.py b/2015-python/tests/test_day_09.py new file mode 100644 index 0000000..df0bc3c --- /dev/null +++ b/2015-python/tests/test_day_09.py @@ -0,0 +1,31 @@ +import unittest + +from solutions.day_09 import Solution + + +class Day09TestCase(unittest.TestCase): + def setUp(self): + self.solution = Solution() + self.puzzle_input = self.solution.parse_input( + """ +London to Dublin = 464 +London to Belfast = 518 +Dublin to Belfast = 141 + """ + ) + + def test_parse_puzzle_input(self): + data = """ + Lnd to Dbn = 123 + """ + assert self.solution.parse_input(data) == [("Lnd", "Dbn", 123)] + + def test_solve_first_part(self): + assert self.solution.solve(self.puzzle_input) == 605 + + def test_solve_second_part(self): + assert self.solution.solve_again(self.puzzle_input) == 982 + + +if __name__ == "__main__": + unittest.main()