summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tomasz@kramkow.ski>2022-12-13 09:54:09 +0000
committerTomasz Kramkowski <tomasz@kramkow.ski>2022-12-13 09:54:09 +0000
commit5f0216b74b70071da7de5d0252a3170039cc2fee (patch)
tree09106a9cea1e69a4791ac3380de5b1f8bf52a1d4
parent7bfeff1cb9b142c1d0998ef3c63a780d231a7329 (diff)
downloadaoc2022-5f0216b74b70071da7de5d0252a3170039cc2fee.tar.gz
aoc2022-5f0216b74b70071da7de5d0252a3170039cc2fee.tar.xz
aoc2022-5f0216b74b70071da7de5d0252a3170039cc2fee.zip
13
-rw-r--r--13.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/13.py b/13.py
new file mode 100644
index 0000000..b0acec0
--- /dev/null
+++ b/13.py
@@ -0,0 +1,36 @@
+from utils import open_day
+from ast import literal_eval
+from functools import cmp_to_key
+
+with open_day(13) as f:
+ pairs = [tuple(literal_eval(v) for v in p.split('\n')) for p in f.read().rstrip().split('\n\n')]
+
+def compare(l, r):
+ if isinstance(l, list):
+ if isinstance(r, list):
+ for a, b in zip(l, r):
+ cmp = compare(a, b)
+ if cmp != 0:
+ return cmp
+ return compare(len(l), len(r))
+ else:
+ return compare(l, [r])
+ else:
+ if isinstance(r, list):
+ return compare([l], r)
+ else:
+ return min(max(l - r, -1), 1)
+
+p1 = 0
+for i, (l, r) in enumerate(pairs):
+ cmp = compare(l, r)
+ if cmp < 0:
+ p1 += i + 1
+print(p1)
+
+p2 = [[[2]],[[6]]]
+for l, r in pairs:
+ p2.append(l)
+ p2.append(r)
+p2.sort(key=cmp_to_key(compare))
+print((p2.index([[2]]) + 1) * (p2.index([[6]]) + 1))