Solve 2015:15 "Science for Hungry People"
This commit is contained in:
parent
cfd2c0f079
commit
fb5a1b9381
2 changed files with 80 additions and 0 deletions
50
2015-python/solutions/day_15.py
Normal file
50
2015-python/solutions/day_15.py
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
from itertools import permutations
|
||||||
|
from math import prod
|
||||||
|
from re import compile, findall
|
||||||
|
from solutions import BaseSolution
|
||||||
|
|
||||||
|
|
||||||
|
class Solution(BaseSolution):
|
||||||
|
input_file = "15.txt"
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "Day 15: Science for Hungry People"
|
||||||
|
|
||||||
|
def parse_input(self, data):
|
||||||
|
digits = compile(r"-?\d+")
|
||||||
|
return [
|
||||||
|
tuple(map(int, findall(digits, line))) for line in data.strip().splitlines()
|
||||||
|
]
|
||||||
|
|
||||||
|
def solve(self, puzzle_input):
|
||||||
|
scores = []
|
||||||
|
factors = list(zip(*puzzle_input))[:-1]
|
||||||
|
distributions = filter(
|
||||||
|
lambda x: sum(x) == 100, permutations(range(1, 100), len(factors[0]))
|
||||||
|
)
|
||||||
|
for distribution in distributions:
|
||||||
|
total = [sum(map(prod, zip(f, distribution))) for f in factors]
|
||||||
|
if all(map(lambda x: x > 0, total)):
|
||||||
|
scores.append(prod(total))
|
||||||
|
return max(scores)
|
||||||
|
|
||||||
|
def solve_again(self, puzzle_input):
|
||||||
|
scores = []
|
||||||
|
factors = list(zip(*puzzle_input))
|
||||||
|
props = factors[:-1]
|
||||||
|
calories = factors[-1]
|
||||||
|
distributions = filter(
|
||||||
|
lambda x: sum(x) == 100, permutations(range(1, 100), len(factors[0]))
|
||||||
|
)
|
||||||
|
for distribution in distributions:
|
||||||
|
calcount = sum(map(prod, zip(calories, distribution)))
|
||||||
|
if calcount == 500:
|
||||||
|
total = [sum(map(prod, zip(f, distribution))) for f in props]
|
||||||
|
if all(map(lambda x: x > 0, total)):
|
||||||
|
scores.append(prod(total))
|
||||||
|
return max(scores)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
solution = Solution()
|
||||||
|
solution.show_results()
|
||||||
30
2015-python/tests/test_day_15.py
Normal file
30
2015-python/tests/test_day_15.py
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from solutions.day_15 import Solution
|
||||||
|
|
||||||
|
|
||||||
|
class Day15TestCase(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.solution = Solution()
|
||||||
|
self.puzzle_input = self.solution.parse_input(
|
||||||
|
"""
|
||||||
|
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
|
||||||
|
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_parse_puzzle_input(self):
|
||||||
|
data = """
|
||||||
|
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
|
||||||
|
"""
|
||||||
|
assert self.solution.parse_input(data) == [(-1, -2, 6, 3, 8)]
|
||||||
|
|
||||||
|
def test_solve_first_part(self):
|
||||||
|
assert self.solution.solve(self.puzzle_input) == 62842880
|
||||||
|
|
||||||
|
def test_solve_second_part(self):
|
||||||
|
assert self.solution.solve_again(self.puzzle_input) == 57600000
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Loading…
Add table
Reference in a new issue