diff options
author | Prefetch | 2022-12-31 22:21:39 +0100 |
---|---|---|
committer | Prefetch | 2022-12-31 22:21:39 +0100 |
commit | 68615a9ad2c942254135cffb00cf25a84a3b1356 (patch) | |
tree | 1ed3131f673207b2ef0bdaee3ee98bb68d6640ca /20 |
Initial commit
Diffstat (limited to '20')
-rwxr-xr-x | 20/main.py | 45 | ||||
-rwxr-xr-x | 20/test.py | 16 |
2 files changed, 61 insertions, 0 deletions
diff --git a/20/main.py b/20/main.py new file mode 100755 index 0000000..c20cc5c --- /dev/null +++ b/20/main.py @@ -0,0 +1,45 @@ +#!/usr/bin/python + +from math import sqrt + + + +def solve_partn(partn, target): + house = 1 + while True: + # Find all divisors of the house number, because those + # determine which elves brings presents to which house. + # If I were smart, I'd cache the results and use them in + # both parts 1 and 2, but I can't be bothered to do that. + divs = [1, house] + for n in range(2, int(sqrt(house)) + 1): + if house % n == 0: + divs += [n, house / n] + + # Give gifts to each house as described in the puzzle text + gifts = 0 + for d in divs: + if partn == 1: + gifts += 10 * d + else: # partn == 2 + if house / d <= 50: + gifts += 11 * d + + if gifts >= target: + return house + + house += 1 + + + +def main(): + # My personalized puzzle input + target = 34000000 + + print("Part 1 solution:", solve_partn(1, target)) # 786240 for me + print("Part 2 solution:", solve_partn(2, target)) # 831600 for me + + + +if __name__ == "__main__": + main() diff --git a/20/test.py b/20/test.py new file mode 100755 index 0000000..15ac2f9 --- /dev/null +++ b/20/test.py @@ -0,0 +1,16 @@ +#!/usr/bin/python + +import unittest + +import main + + + +class ExamplesPart1(unittest.TestCase): + def test_example1(self): + self.assertEqual(main.solve_partn(1, 150), 8) + + + +if __name__ == "__main__": + unittest.main() |