Skip to content

Commit 4eb6fb3

Browse files
committed
Completed day 10 of year 2020
1 parent e592110 commit 4eb6fb3

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

2020/10.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
split_data = True
5+
completed = True
6+
raw_data = None # Not To be touched
7+
8+
def part1(data):
9+
data = sorted(int(x) for x in data)
10+
data.append(data[-1]+3)
11+
12+
diff = [0, 0, 0]
13+
prev_adp = 0
14+
for adapter in data:
15+
diff[adapter - prev_adp - 1] += 1
16+
prev_adp = adapter
17+
18+
return diff[0] * diff[2]
19+
20+
def part2(data):
21+
# This code only works because:
22+
# There is no adapter difference of 2
23+
# The maximum streak of ones seen is 4
24+
# The multipliers were manually calculated by sitting down and doing it on paper.
25+
# More efficient methods exists such as recursion checking using memoization...
26+
27+
28+
29+
30+
streak_mult = [1, 1, 2, 4, 7]
31+
data = sorted(int(x) for x in data)
32+
data.append(data[-1]+3)
33+
34+
total_comb = 1
35+
prev_adp = 0
36+
one_streak = 0
37+
for adapter in data:
38+
if adapter - prev_adp == 1:
39+
one_streak += 1
40+
else:
41+
total_comb *= streak_mult[one_streak]
42+
one_streak = 0
43+
prev_adp = adapter
44+
45+
return total_comb
46+
47+
48+
# The following is a recurive solution from reddit megathread that is more neat and clean
49+
from functools import cache
50+
51+
data.insert(0, 0)
52+
53+
@cache
54+
def recursive_sol(rating):
55+
if rating == 0: return 1 # Only count if we were able to reach all the way down...
56+
options = [opt for opt in range(rating-3, rating) if opt in data]
57+
return sum(recursive_sol(opt) for opt in options)
58+
59+
return recursive_sol(data[-1])

0 commit comments

Comments
 (0)