-
Notifications
You must be signed in to change notification settings - Fork 0
/
day10.py
48 lines (40 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
FILEPATH = "data/day10-1_input.txt"
the_adapters = sorted([int(x) for x in open(FILEPATH, 'r')])
def get_adapters(adapter: int, adapters: list) -> tuple:
if not adapters:
return 0, 0
if adapters[0] == adapter + 3:
one_jolt, three_jolts = get_adapters(adapters[0], adapters[1:])
return one_jolt, three_jolts + 1
elif adapters[0] == adapter + 1:
one_jolt, three_jolts = get_adapters(adapters[0], adapters[1:])
return one_jolt + 1, three_jolts
# An adapter is optionnal if it is between two adapters that differs of 1 unit
# This function get the removable adapter, grouped as they follow each other
def get_removables_by_group(adapters: list) -> list:
removables = []
group = []
for index, adapter in enumerate(adapters):
try:
if adapters[index - 1] + 1 == adapter == adapters[index + 1] - 1:
group.append(adapter)
else:
removables.append(group)
group = []
except IndexError:
removables.append(group)
group = []
return removables
one, three = get_adapters(0, the_adapters)
print(one * (three + 1))
removables_adapters = get_removables_by_group([0] + the_adapters)
print(removables_adapters)
count = 1
for group in removables_adapters:
if len(group) == 1:
count = count * 2
if len(group) == 2:
count = count * 4
if len(group) == 3:
count = count * 7
print(count)