summaryrefslogtreecommitdiffstats
path: root/20/py/d10.py
diff options
context:
space:
mode:
Diffstat (limited to '20/py/d10.py')
-rw-r--r--20/py/d10.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/20/py/d10.py b/20/py/d10.py
new file mode 100644
index 0000000..2b3bc2e
--- /dev/null
+++ b/20/py/d10.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+import aoc20
+import functools
+import sys
+
+
+def pt1(_in):
+ jolts = [int(n.strip()) for n in _in]
+ jolts.append(max(jolts)+3)
+ jolts = sorted(jolts)
+ jolt = 0
+ diffs = [0, 0, 0]
+ for j in jolts:
+ diffs[j-jolt-1] += 1
+ jolt = j
+ return diffs[0] * diffs[2]
+
+
+def pt2(_in):
+ jolts = [int(n.strip()) for n in _in]
+ jolts.append(max(jolts)+3)
+ jolts = sorted(jolts)
+ jolts.insert(0, 0)
+ jolt = 0
+ reach = {}
+ for i in range(len(jolts)):
+ reachable = []
+ j = i+1
+ while j < len(jolts) and jolts[j] - jolts[i] <= 3:
+ reachable.append(jolts[j])
+ j += 1
+ reach[jolts[i]] = tuple(reachable)
+
+ @functools.cache
+ def ways(start, dest):
+ if start == dest:
+ return 1
+ return sum(ways(child, dest) for child in reach[start])
+
+ return ways(jolts[0], jolts[-1])
+
+
+if __name__ == "__main__":
+ input = aoc20.read_input(sys.argv[1:], 10)
+ print(pt1(input))
+ print(pt2(input))