2015 - Day 20 (Infinite Elves and Infinite Houses)

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
36
37
38
39
#!/usr/bin/env python3

#Advent of Code
#2015 Day 20, 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

def solve(puzzle_input):
    def factors(n):
        pairs = [(f, int(n/f)) for f in range(1, int(n**0.5 + 1)) if n % f == 0]
        unique = set()
        for pair in pairs:
            unique.add(pair[0])
            unique.add(pair[1])
        return list(unique)

    i = 1
    while sum(factors(i)) * 10 < puzzle_input:
        i += 1
    return i

def main():
    puzzle_input = util.read.as_int()

    house = solve(puzzle_input)

    print("The lowest house number that gets at least as many presents as the puzzle input is " + str(house) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

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
41
#!/usr/bin/env python3

#Advent of Code
#2015 Day 20, 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

def solve(puzzle_input):
    def factors(n):
        pairs = [(f, int(n/f)) for f in range(1, int(n**0.5 + 1)) if n % f == 0]
        unique = set()
        for pair in pairs:
            if n <= pair[0] * 50:
                unique.add(pair[0])
            if n <= pair[1] * 50:
                unique.add(pair[1])
        return list(unique)

    i = 1
    while sum(factors(i)) * 11 < puzzle_input:
        i += 1
    return i

def main():
    puzzle_input = util.read.as_int()

    house = solve(puzzle_input)

    print("The lowest house number that gets at least as many presents as the puzzle input is " + str(house) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

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