-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
67 lines (48 loc) · 1.45 KB
/
day10.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
58
59
60
61
62
63
64
65
66
67
from pathlib import Path
import numpy as np
import itertools
lines = ''
with open('input/' + Path(__file__).stem, "r") as text_file:
lines = text_file.read().splitlines()
adapters = np.array(lines, dtype=int)
device_jol = np.max(adapters) + 3
def part1():
jol = 0
difference = {
1: 0,
2: 0,
3: 0
}
while jol < device_jol:
# Find matching adapter
possible_adapter = np.sort(
adapters[(adapters - jol <= 3) & (adapters - jol > 0)])
# Arrived at device
if(len(possible_adapter) == 0):
difference[3] += 1
break
adapter = possible_adapter[0]
difference[adapter - jol] += 1
jol = adapter
print(difference)
return difference[1] * difference[3]
def part2(jol=0, prev=[]):
current_jol = jol
if(current_jol == device_jol):
return []
# Find matching adapter
possible_adapter = np.sort(
adapters[(adapters - jol <= 3) & (adapters - jol > 0)])
# Arrived at device
# if(len(possible_adapter) == 0):
# return []
return prev + [part2(adapt, [adapt]) for adapt in possible_adapter]
def count_paths(tree):
if(not isinstance(tree, list)):
return int(tree == device_jol - 3)
return np.sum([count_paths(subtree) for subtree in tree])
print(f'Device: {device_jol} joltage')
# print(f'Part 1: {part1()}')
tree = part2()
paths = count_paths(tree)
print(f'Part 2: {paths}')