From 7b40516b66b3a3bc09f7a132c7b69ff77d73a83a Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Thu, 15 Dec 2022 12:52:39 -0800 Subject: [PATCH] day 15 part 2 alternate solution --- day15/clip.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 day15/clip.py diff --git a/day15/clip.py b/day15/clip.py new file mode 100644 index 0000000..50a8135 --- /dev/null +++ b/day15/clip.py @@ -0,0 +1,35 @@ +import pyclipper # https://pypi.org/project/pyclipper/ + +data = [] +for line in open("input"): + words = line.replace(":", "").replace(",","").split() + coords = [int(w[w.index('=')+1:]) for w in words if '=' in w] + data.append(coords) + + +pc = pyclipper.Pyclipper() +for sx,sy,bx,by in data: + dx = abs(sx-bx) + dy = abs(sy-by) + d = dx+dy + pc.AddPath([(sx+d,sy),(sx,sy+d),(sx-d,sy),(sx,sy-d)], pyclipper.PT_CLIP, closed=True) + +M = 4000000 +pc.AddPath([(0,0),(M,0),(M,M),(0,M)], pyclipper.PT_SUBJECT, closed=True) + +solution = pc.Execute(pyclipper.CT_DIFFERENCE, pyclipper.PFT_NONZERO, pyclipper.PFT_NONZERO) + +# should return a single small polygon shaped like a diamond +# the answer we want is the point at the center of the diamond + +assert len(solution) == 1 +assert pyclipper.Area(solution[0]) < 4 + +def avg(ns): + return sum(ns)/len(ns) + +for p in solution: + x = avg([x for x,y in p]) + y = avg([y for x,y in p]) + print(x,y) + print("frequency =",M*x + y)