aboutsummaryrefslogtreecommitdiff
path: root/2017
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-06 23:27:58 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-06 23:27:58 +0100
commite984cdcd76fd861b8ec633f15f0f4255394c22d1 (patch)
treef10ea845253d7dd4aa7af48c2643f891e474085e /2017
parentac0ae55b0c2cec33a18b9a4e1ebe3558f3467b57 (diff)
Add day 6 solutions
Diffstat (limited to '2017')
-rw-r--r--2017/06/.gitignore1
-rw-r--r--2017/06/Makefile8
-rw-r--r--2017/06/input1
-rw-r--r--2017/06/puzzles.py38
4 files changed, 48 insertions, 0 deletions
diff --git a/2017/06/.gitignore b/2017/06/.gitignore
new file mode 100644
index 0000000..ffc46fe
--- /dev/null
+++ b/2017/06/.gitignore
@@ -0,0 +1 @@
+puzzle-[12].py
diff --git a/2017/06/Makefile b/2017/06/Makefile
new file mode 100644
index 0000000..247194a
--- /dev/null
+++ b/2017/06/Makefile
@@ -0,0 +1,8 @@
+all:
+ sed '/START PART 2/,/END PART 2/d' puzzles.py >puzzle-1.py
+ sed '/START PART 1/,/END PART 1/d' puzzles.py >puzzle-2.py
+ chmod +x puzzle-[12].py
+
+.PHONY: clean
+clean:
+ rm -f puzzle-[12].py
diff --git a/2017/06/input b/2017/06/input
new file mode 100644
index 0000000..177ae3d
--- /dev/null
+++ b/2017/06/input
@@ -0,0 +1 @@
+5 1 10 0 1 7 13 14 3 12 8 10 7 12 0 6
diff --git a/2017/06/puzzles.py b/2017/06/puzzles.py
new file mode 100644
index 0000000..0130cf8
--- /dev/null
+++ b/2017/06/puzzles.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python3
+
+from collections import defaultdict
+# START PART 2
+from itertools import count
+# END PART 2
+from typing import DefaultDict, Literal
+
+
+def main() -> None:
+ with open("input", "r", encoding="utf-8") as f:
+ banks = tuple(map(int, f.read().split()))
+
+ # START PART 1
+ memory: DefaultDict[tuple[int, ...], Literal[0]] = defaultdict(int)
+ # END PART 1 START PART 2
+ inc = count(start=0, step=1)
+ memory: DefaultDict[tuple[int, ...], int] = defaultdict(lambda: next(inc))
+ # END PART 2
+
+ while banks not in memory:
+ memory[banks]
+ i, v = max(enumerate(banks), key=lambda k: (k[1], -k[0]))
+
+ banks = banks[:i] + (0,) + banks[i + 1 :]
+ for _ in range(v):
+ i = (i + 1) % 16
+ banks = banks[:i] + ((banks[i] + 1),) + banks[i + 1 :]
+
+ # START PART 1
+ print(len(memory))
+ # END PART 1 START PART 2
+ print(len(memory) - memory[banks])
+ # END PART 2
+
+
+if __name__ == "__main__":
+ main()