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/main.py |
Initial commit
Diffstat (limited to '20/main.py')
-rwxr-xr-x | 20/main.py | 45 |
1 files changed, 45 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() |