summaryrefslogtreecommitdiffstats
path: root/13.py
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2023-12-13 14:32:28 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2023-12-13 14:32:28 +0000
commitef213fd0c871fc871f758941298ad839f998dc21 (patch)
treedfaeb2a06fb33ba4ec1e6ab0be591d753b6bc48b /13.py
parent403e334db290adfa7a64c617c83c4b69e7fc12b6 (diff)
downloadaoc2023-ef213fd0c871fc871f758941298ad839f998dc21.tar.gz
aoc2023-ef213fd0c871fc871f758941298ad839f998dc21.tar.xz
aoc2023-ef213fd0c871fc871f758941298ad839f998dc21.zip
day 13
Diffstat (limited to '13.py')
-rw-r--r--13.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/13.py b/13.py
new file mode 100644
index 0000000..2538e53
--- /dev/null
+++ b/13.py
@@ -0,0 +1,35 @@
+# pyright: strict
+from collections.abc import Iterable
+from functools import cache
+from sys import stdin
+
+
+@cache
+def rocks_to_int(bits: Iterable[str]) -> int:
+ return sum(2**i for i, bit in enumerate(bits) if bit == "#")
+
+
+@cache
+def diff(nums: tuple[int], cut: int) -> int:
+ return sum(
+ (nums[a] ^ nums[b]).bit_count()
+ for a, b in zip(range(cut - 1, -1, -1), range(cut, len(nums)))
+ )
+
+
+def find_mirror(nums: tuple[int, ...], difference: int) -> int:
+ for n in range(1, len(nums)):
+ if diff(nums, n) == difference:
+ return n
+ return 0
+
+
+p1 = p2 = 0
+for pattern in stdin.read().rstrip("\n").split("\n\n"):
+ pattern = pattern.split("\n")
+ rows = tuple(map(rocks_to_int, pattern))
+ cols = tuple(map(rocks_to_int, zip(*pattern)))
+ p1 += find_mirror(cols, 0) or find_mirror(rows, 0) * 100
+ p2 += find_mirror(cols, 1) or find_mirror(rows, 1) * 100
+
+print(p1, p2, sep="\n")