-
Notifications
You must be signed in to change notification settings - Fork 0
/
day20.py
57 lines (45 loc) · 1.33 KB
/
day20.py
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
46
47
48
49
50
51
52
53
54
55
56
57
import math
from itertools import count
# TIL:
# python has the usual % operator
# keyword args w/ defaults make it super easy to write flexible functions
BEGIN = 700000
def factors(n, cutoff=None):
factors = []
for factor in range(1, math.ceil(math.sqrt(n))):
d, m = divmod(n, factor)
if m == 0:
factors.append(factor)
factors.append(math.trunc(d))
if cutoff:
factors = filter(lambda x: x >= cutoff, factors)
return factors
def part1(tgt):
max = 0
for hnum in count(BEGIN):
facts = factors(hnum)
presents = sum(facts) * 10
# if presents > max:
# max = presents
# print(f"currently at max={max}")
if presents >= tgt:
return hnum
def part2(tgt):
# get the factors, discard factors where factor * 50 < num
max = 0
for hnum in count(BEGIN):
facts = factors(hnum, cutoff=hnum // 50)
presents = sum(facts) * 11
# if presents > max:
# max = presents
# print(f"currently at max={max} for {hnum} facts={facts}")
if presents >= tgt:
return hnum
def main():
print(factors(20))
print(part1(150))
print(part1(33100000)) # 776160
# print(part2(10000))
print(part2(33100000)) # 786240
if __name__ == "__main__":
main()