-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.py
53 lines (40 loc) · 1.21 KB
/
10.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
from typing import List
import runner
from pathlib import Path
import numpy as np
from collections import Counter
from anytree import Node
def part1(data):
ratings = list(map(int, data.splitlines()))
ratings = [0] + sorted(ratings)
ratings.append(ratings[-1] + 3)
diffs = np.diff(np.array(ratings))
cnt = Counter(diffs)
print(cnt)
return cnt[1] * cnt[3]
def valid_sequence(seq):
diffs = np.diff(seq)
cnt = Counter(diffs)
too_large = [diff for diff in cnt.keys() if diff > 3]
return len(too_large) == 0
def attach(node: Node, ratings: List[int]):
children = []
for i, rating in enumerate(ratings):
diff = rating - node.name
if diff <= 3:
child = Node(rating)
children.append(child)
attach(child, ratings[i:])
else:
break
node.children = children
def part2(data):
ratings = list(map(int, data.splitlines()))
ratings = sorted(ratings)
final = ratings[-1] + 3
root = Node(0)
attach(root, ratings)
valid_leaves = [leaf for leaf in root.leaves if (final - leaf) <= 3]
print(valid_leaves)
return len(valid_leaves)
runner.run(day=int(Path(__file__).stem))