2015 - Day 17 (No Such Thing as Too Much)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python3

#Advent of Code
#2015 Day 17, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import combinations

def solve(puzzle_input, target=150):
    seen = 0

    for i in range(1, len(puzzle_input)+1):
        for arr in combinations(puzzle_input, i):
            if sum(arr) == target:
                seen += 1

    return seen

def main():
    puzzle_input = util.read.as_int_list("\n")

    combs = solve(puzzle_input)

    print("The number of combinations of containers that fit 150 litres of eggnog is " + str(combs) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([20, 15, 10, 5, 5], 25), 4)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python3

#Advent of Code
#2015 Day 17, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
from itertools import combinations

def solve(puzzle_input, target=150):
    seen = 0
    minimum = len(puzzle_input)+1

    for i in range(1, len(puzzle_input)+1):
        for arr in combinations(puzzle_input, i):
            if sum(arr) == target:
                if len(arr) == minimum:
                    seen += 1
                elif len(arr) < minimum:
                    minimum = len(arr)
                    seen = 1

    return seen

def main():
    puzzle_input = util.read.as_int_list("\n")

    ways = solve(puzzle_input)

    print("The number of different ways to fill the minimum number of containers that can fit 150 litres of eggnog is " + str(ways) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        return self.assertEqual(solve([20, 15, 10, 5, 5], 25), 3)

if __name__ == "__main__":
    run(main)
Return to solution index