aboutsummaryrefslogtreecommitdiff
path: root/2020/10/puzzle-2.py
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
committerThomas Voss <thomasvoss@live.com> 2021-10-29 23:02:39 +0200
commite7c9108b95e39d7ea5a29ae06d619c4727f11027 (patch)
tree237261eef3afd0720be77dbcbb9599fa66a24b67 /2020/10/puzzle-2.py
Initial commit
Diffstat (limited to '2020/10/puzzle-2.py')
-rwxr-xr-x2020/10/puzzle-2.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/2020/10/puzzle-2.py b/2020/10/puzzle-2.py
new file mode 100755
index 0000000..bdf7bbf
--- /dev/null
+++ b/2020/10/puzzle-2.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+from typing import List
+
+adaptors: list[str]
+
+
+def combos(i: int, counts: dict[int, dict[int, int]] = {}) -> int:
+ if i == len(adaptors) - 1:
+ return 1
+ if i in counts:
+ return counts[i]
+
+ ans = 0
+ for j in range(i + 1, len(adaptors)):
+ if adaptors[j] - adaptors[i] <= 3:
+ ans += combos(j, counts)
+
+ counts[i] = ans
+ return ans
+
+
+def main() -> None:
+ global adaptors
+ with open("input", "r") as f:
+ adaptors = list(map(int, f.readlines()))
+
+ adaptors.append(0)
+ adaptors.sort()
+ adaptors.append(max(adaptors) + 3)
+
+ print(combos(0))
+
+
+if __name__ == "__main__":
+ main()