From abe366b3b3baddd32d6313e56a66eae23cec6f57 Mon Sep 17 00:00:00 2001 From: setop Date: Tue, 13 Dec 2022 13:23:40 +0100 Subject: [PATCH] day 13, with help --- d13/run.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 d13/run.py diff --git a/d13/run.py b/d13/run.py new file mode 100644 index 0000000..b5eb7fe --- /dev/null +++ b/d13/run.py @@ -0,0 +1,33 @@ +import sys + +P = [ tuple(map(eval, p.split("\n"))) for p in sys.stdin.read().split("\n\n") ] + +def C(l,r): + T = (type(l),type(r)) + if T == (int,int): + if lr # 0 or 1 + if T == (int,list): + return C([l],r) + if T == (list,int): + return C(l,[r]) + # list,list + for q in zip(l,r): + c = C(*q) + if c: return c + return C(len(l),len(r)) + +# part 1 +S = 0 +for i,p in enumerate(P): + if C(*p) <= 0: + S += i+1 +print(S) + +# part 2 +from functools import cmp_to_key +Q = [ q for (l,r) in P for q in [l,r] ] +Q.append([[2]]) +Q.append([[6]]) +Q.sort(key=cmp_to_key(C)) +print((Q.index([[2]])+1)*(Q.index([[6]])+1))