Last active
December 15, 2022 20:36
-
-
Save justecorruptio/17c7ce87046a1eed03702754d6133e5b to your computer and use it in GitHub Desktop.
Revisions
-
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 10 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,39 +1,29 @@ from itertools import product import re fh = open('input', 'r') lines = {} for line in fh: sx, sy, bx, by = map(int, re.findall('-?\d+', line)) d = abs(bx - sx) + abs(by - sy) + 1 for endpoints, mb, norm in [ ((sx - d, sx), (-1, sy - d + sx), -1), ((sx, sx + d), ( 1, sy - d - sx), -1), ((sx, sx + d), (-1, sy + d + sx), 1), ((sx - d, sx), ( 1, sy + d - sx), 1), ]: lines.setdefault(mb, {-1:[], 1:[]})[norm].append( endpoints ) slopes = {-1: [], 1:[]} for (m, b), norms in lines.items(): for (x1, x2), (x3, x4) in product(*norms.values()): o1, o2 = max(x1, x3), min(x2, x4) if o1 <= o2: slopes[m].append((b, (o1, o2))) for (b1, (x1, x2)), (b2, (x3, x4)) in product(slopes[-1], slopes[1]): x = (b2 - b1) // -2 y = x + b2 if x1 <= x <= x2 and x3 <= x <= x4: print(x * 4000000 + y) -
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 18 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,46 +2,23 @@ import re fh = open('input', 'r') def overlap(e1, e2): x1, x2 = e1 x3, x4 = e2 return max(x1, x3), min(x2, x4) segments = [] for line in fh: sx, sy, bx, by = map(int, re.findall('-?\d+', line)) d = abs(bx - sx) + abs(by - sy) + 1 segments.extend([ ((sx - d, sx), (-1, sy - d + sx), -1), ((sx, sx + d), ( 1, sy - d - sx), -1), ((sx, sx + d), (-1, sy + d + sx), 1), ((sx - d, sx), ( 1, sy + d - sx), 1), ]) lines = {} for (endpoints, mb, norm) in segments: lines.setdefault(mb, {}).setdefault(norm, []).append( endpoints ) @@ -51,13 +28,12 @@ def overlap(e1, e2, m): if len(norms) != 2: continue for e1, e2 in product(*norms.values()): x1, x2 = overlap(e1, e2) if x1 <= x2: slopes[m].append((b, (x1, x2))) for (b1, (x1, x2)), (b2, (x3, x4)) in product(slopes[-1], slopes[1]): x = (b2 - b1) // -2 y = x + b2 if x1 <= x <= x2 and x3 <= x <= x4: print(x * 4000000 + y) -
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 55 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,63 @@ from itertools import combinations, product import re fh = open('input', 'r') def slope_inter(p1, p2): x1, y1 = p1 x2, y2 = p2 m = (y2 - y1) // (x2 - x1) b = y1 - m * x1 return (m, b) def is_overlap(e1, e2): (x1, y1), (x2, y2) = sorted(e1, key=lambda v: v[0]) (x3, y3), (x4, y4) = sorted(e2, key=lambda v: v[0]) return x3 <= x2 and x4 >= x1 def overlap(e1, e2, m): (x1, y1), (x2, y2) = sorted(e1, key=lambda v: v[0]) (x3, y3), (x4, y4) = sorted(e2, key=lambda v: v[0]) if m == 1: return (max(x1, x3), max(y1, y3)), (min(x2, x4), min(y2, y4)) else: return (max(x1, x3), min(y1, y3)), (min(x2, x4), max(y2, y4)) segments = [] for line in fh: sx, sy, bx, by = map(int, re.findall('-?\d+', line)) d = abs(bx - sx) + abs(by - sy) p1 = (sx - d - 1, sy) p2 = (sx, sy - d - 1) p3 = (sx + d + 1, sy) p4 = (sx, sy + d + 1) segments.extend([ ((p1, p2), slope_inter(p1, p2), -1), ((p2, p3), slope_inter(p2, p3), -1), ((p3, p4), slope_inter(p3, p4), 1), ((p4, p1), slope_inter(p4, p1), 1), ]) lines = {} for (endpoints, mb, norm) in segments: lines.setdefault(mb, {}).setdefault(norm, []).append( endpoints ) slopes = {-1: [], 1:[]} for (m, b), norms in lines.items(): if len(norms) != 2: continue for e1, e2 in product(*norms.values()): if is_overlap(e1, e2): slopes[m].append(overlap(e1, e2, m)) for e1, e2 in product(*slopes.values()): m1, b1 = slope_inter(*e1) m2, b2 = slope_inter(*e2) x = (b2 - b1) // (m1 - m2) y = m1 * x + b1 if is_overlap(e1, ((x, y), (x, y))) and is_overlap(e2, ((x, y), (x, y))): print(x * 4000000 + y) -
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ import re fh = open('input', 'r') @@ -7,7 +6,7 @@ sx, sy, bx, by = map(int, re.findall('-?\d+', line)) d = abs(bx - sx) + abs(by - sy) + 1 for m, n in [(-1, -1), (-1, 1), (1, -1), (1, 1)]: b = sy + n * d - m * sx rays.setdefault((m, b), set()).add(n) -
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 1 addition and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,10 +11,7 @@ b = sy + n * d - m * sx rays.setdefault((m, b), set()).add(n) lines = [mb for mb, norms in rays.items() if len(norms) == 2] (m1, b1), (m2, b2) = lines x = (b2 - b1) // (m1 - m2) -
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,14 +9,14 @@ for m, n in product([-1, 1], repeat=2): b = sy + n * d - m * sx rays.setdefault((m, b), set()).add(n) lines = [] for mb, norms in rays.items(): if len(norms) == 2: lines.append(mb) (m1, b1), (m2, b2) = lines x = (b2 - b1) // (m1 - m2) y = m1 * x + b1 print(x * 4000000 + y) -
justecorruptio revised this gist
Dec 15, 2022 . 1 changed file with 2 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,13 +9,11 @@ for m, n in product([-1, 1], repeat=2): b = sy + n * d - m * sx rays.setdefault((m, b), [False, False])[(n + 1) // 2] = True final_lines = {} for (m, b), norms in rays.items(): if all(norms): final_lines[m] = b (m1, b1), (m2, b2) = final_lines.items() -
justecorruptio created this gist
Dec 15, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ from itertools import combinations, product import re fh = open('input', 'r') rays = {} for line in fh: sx, sy, bx, by = map(int, re.findall('-?\d+', line)) d = abs(bx - sx) + abs(by - sy) + 1 for m, n in product([-1, 1], repeat=2): b = sy + n * d - m * sx rays.setdefault((m, b), []).append(n) final_lines = {} for (m, b), norms in rays.items(): for n1, n2 in combinations(norms, 2): if n1 == n2: continue final_lines[m] = b (m1, b1), (m2, b2) = final_lines.items() x = (b2 - b1) // (m1 - m2) y = m1 * x + b1 print(x * 4000000 + y)