Skip to content

Commit 1d3b5b2

Browse files
committed
Day 10: Part1 & Part2 done
1 parent 682c28a commit 1d3b5b2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

day10/day10.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
import sys
3+
import re
4+
from itertools import islice, permutations
5+
from collections import Counter, defaultdict
6+
from typing import Any, List, Iterator, Generator
7+
8+
# from ..tools import sliding_window
9+
10+
11+
def window(seq: Iterator[Any], n: int = 2) -> Generator[Any, None, None]:
12+
"Returns a sliding window (of width n) over data from the iterable"
13+
" s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... "
14+
"taken from https://docs.python.org/release/2.3.5/lib/itertools-example.html"
15+
it = iter(seq)
16+
result = tuple(islice(it, n))
17+
if len(result) == n:
18+
yield result
19+
for elem in it:
20+
result = result[1:] + (elem,)
21+
yield result
22+
23+
24+
def calculate_diffs(inlist) -> int:
25+
result = Counter()
26+
jolt = 0
27+
dist = 0
28+
for i in inlist:
29+
30+
diff = i - jolt
31+
if diff == 1:
32+
dist+=1
33+
result[diff]+=1
34+
jolt = i
35+
36+
# add final 3 jolt jump
37+
result[3]+=1
38+
return int(result[1]*result[3])
39+
40+
def get_variants(inlist) -> int:
41+
# defaultdict from int will default to 0 if an entry does not exist
42+
ways = defaultdict(int)
43+
ways[0] = 1
44+
# skip the leading 0, as that is always reached
45+
# and already account for above :)
46+
for i in inlist[1:]:
47+
ways[i] = ways[i-1]+ways[i-2]+ways[i-3]
48+
return ways[max(inlist)]
49+
50+
51+
if __name__ == "__main__":
52+
with open(sys.argv[1], "r") as infile:
53+
# add initial 0 as input jolt
54+
jolts = [0]
55+
for nextline in infile:
56+
jolt = int(nextline.strip())
57+
jolts.append(jolt)
58+
jolts = sorted(jolts)
59+
part1 = calculate_diffs(jolts)
60+
print("Part1: ", part1)
61+
print("Part2: ", get_variants(jolts))

0 commit comments

Comments
 (0)