-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
38 lines (28 loc) · 865 Bytes
/
main.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
import functools
import os
@functools.lru_cache(maxsize=None)
def num_combinations(ratings, i=0):
if i >= len(ratings) - 1:
return 1
num = 0
for j in range(1, 4):
if i + j <= len(ratings) - 1 and ratings[i + j] - ratings[i] <= 3:
num += num_combinations(ratings, i + j)
return num
def main():
with open(os.path.join(os.path.dirname(__file__), "input.txt")) as f:
ratings = list(map(int, f.read().splitlines()))
ratings.insert(0, 0)
ratings.sort()
one = 0
three = 1
for i in range(len(ratings) - 1):
curr, nxt = ratings[i + 1], ratings[i]
if curr - nxt == 1:
one += 1
elif curr - nxt == 3:
three += 1
print("Part one:", one * three)
print("Part two:", num_combinations(tuple(ratings)))
if __name__ == "__main__":
main()