Solve 2015:13 "Knights of the Dinner Table"
This commit is contained in:
parent
6a1b231552
commit
e859324030
2 changed files with 96 additions and 0 deletions
50
2015-python/solutions/day_13.py
Normal file
50
2015-python/solutions/day_13.py
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
from collections import defaultdict
|
||||||
|
from itertools import permutations, pairwise
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "13.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 13: Knights of the Dinner Table"
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
lines = defaultdict(int)
|
||||||
|
|
||||||
|
def parseline(line):
|
||||||
|
a, _w, gain_or_lose, value, _h, _u, _b, _s, _n, _t, b = line.split()
|
||||||
|
units = -int(value) if gain_or_lose == "lose" else int(value)
|
||||||
|
return (f"{a}-{b[:-1]}", units)
|
||||||
|
|
||||||
|
for k, v in [parseline(line) for line in data.strip().splitlines()]:
|
||||||
|
lines[k] = v
|
||||||
|
|
||||||
|
return lines
|
||||||
|
|
||||||
|
def _solve(self, people, puzzle_input):
|
||||||
|
def happiness(seats):
|
||||||
|
neighbours = list(pairwise(seats))
|
||||||
|
neighbours.append((seats[0], seats[-1]))
|
||||||
|
return sum(
|
||||||
|
[
|
||||||
|
sum(puzzle_input["-".join([a, b])] for a, b in neighbours),
|
||||||
|
sum(puzzle_input["-".join([b, a])] for a, b in neighbours[::-1]),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
return max(happiness(seats) for seats in permutations(people, len(people)))
|
||||||
|
|
||||||
|
def solve(self, puzzle_input):
|
||||||
|
people = set([ab.split("-")[0] for ab, _v in puzzle_input.items()])
|
||||||
|
return self._solve(people, puzzle_input)
|
||||||
|
|
||||||
|
def solve_again(self, puzzle_input):
|
||||||
|
people = set([ab.split("-")[0] for ab, _v in puzzle_input.items()])
|
||||||
|
people.add("-")
|
||||||
|
return self._solve(people, puzzle_input)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
46
2015-python/tests/test_day_13.py
Normal file
46
2015-python/tests/test_day_13.py
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
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(
|
||||||
|
"""
|
||||||
|
Alice would gain 54 happiness units by sitting next to Bob.
|
||||||
|
Alice would lose 79 happiness units by sitting next to Carol.
|
||||||
|
Alice would lose 2 happiness units by sitting next to David.
|
||||||
|
Bob would gain 83 happiness units by sitting next to Alice.
|
||||||
|
Bob would lose 7 happiness units by sitting next to Carol.
|
||||||
|
Bob would lose 63 happiness units by sitting next to David.
|
||||||
|
Carol would lose 62 happiness units by sitting next to Alice.
|
||||||
|
Carol would gain 60 happiness units by sitting next to Bob.
|
||||||
|
Carol would gain 55 happiness units by sitting next to David.
|
||||||
|
David would gain 46 happiness units by sitting next to Alice.
|
||||||
|
David would lose 7 happiness units by sitting next to Bob.
|
||||||
|
David would gain 41 happiness units by sitting next to Carol.
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_parse_puzzle_input(self):
|
||||||
|
data = """
|
||||||
|
Alice would gain 54 happiness units by sitting next to Bob.
|
||||||
|
Alice would lose 79 happiness units by sitting next to Carol.
|
||||||
|
"""
|
||||||
|
assert self.solution.parse_input(data) == dict(
|
||||||
|
[
|
||||||
|
("Alice-Bob", 54),
|
||||||
|
("Alice-Carol", -79),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_solve_first_part(self):
|
||||||
|
assert self.solution.solve(self.puzzle_input) == 330
|
||||||
|
|
||||||
|
# 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