blob: c20cc5c0969a12c65700ffdb4554983676337476 (
plain)
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
42
43
44
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()
|