From a5a664736db644467e93161567a3c34911e23249 Mon Sep 17 00:00:00 2001 From: Thibaud Date: Mon, 28 Jul 2025 19:48:46 +0200 Subject: [PATCH] 2018 day6 (slow) --- 2018/day6/day6.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/2018/day6/day6.py b/2018/day6/day6.py index d7c594b..5c30051 100644 --- a/2018/day6/day6.py +++ b/2018/day6/day6.py @@ -1,9 +1,55 @@ +from collections import defaultdict, namedtuple + + +Point = namedtuple('Point', ['x', 'y']) + + def dist(a, b): "manhattan distance" return abs(a.x - b.x) + abs(a.y - b.y) + +def iter_grid(bounds): + min_, max_ = bounds + for x in range(min_.x, max_.x + 1): + for y in range(min_.y, max_.y + 1): + yield Point(x, y) + + +def is_edge(p, bounds): + start, end = bounds + return p.x == start.x or p.x == end.x or p.y == start.y or p.y == end.y + + def main(inp): - pass + grid = [Point(*map(int, l.split(", "))) for l in inp] + bounds = (Point(min(p.x for p in grid), min(p.y for p in grid)), + Point(max(p.x for p in grid), max(p.y for p in grid))) + + areas = defaultdict(int) + infinite_regions = set() + for p in iter_grid(bounds): + # find dist to every point of grid + distances = sorted((dist(p2, p), i) for i, p2 in enumerate(grid)) + # equally far from two or more coordinates, don't count + if distances[0][0] == distances[1][0]: + continue + _, index = distances[0] + areas[index] += 1 + if is_edge(p, bounds): + infinite_regions.add(index) + + # remove all infinite regions by index + for i in infinite_regions: + del areas[i] + print("Part 1: ", max(areas.values())) + + count = 0 + for cur in iter_grid(bounds): + s = sum(dist(cur, p) for p in grid) + if s < 10_000: + count += 1 + print("Part 2: ", count) if __name__ == "__main__":