From 60ea8eb79148a85ffc13c80aedac476402647c94 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Sat, 22 Mar 2025 11:41:18 +0100 Subject: [PATCH] problem 5 --- inputs/input5 | 60 ++++++++++++++++++++++++++++++++++++++++++++++ inputs/input5_test | 8 +++++++ problem5.py | 54 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 inputs/input5 create mode 100644 inputs/input5_test create mode 100644 problem5.py diff --git a/inputs/input5 b/inputs/input5 new file mode 100644 index 0000000..3f8e747 --- /dev/null +++ b/inputs/input5 @@ -0,0 +1,60 @@ +(-97, 92) +(-223, -177) +(397, -379) +(-197, 99) +(-179, 342) +(-25, 123) +(368, -120) +(110, 189) +(24, 138) +(361, 191) +(-48, 90) +(-325, 400) +(-359, 107) +(-313, -249) +(-1, -376) +(189, -143) +(63, -242) +(-118, 22) +(337, 210) +(138, 148) +(-255, -44) +(-236, 375) +(134, -98) +(77, 230) +(45, 87) +(352, -179) +(304, -385) +(380, 368) +(-111, -349) +(324, 48) +(-364, -125) +(-105, 199) +(321, 302) +(-254, -342) +(-337, 121) +(-44, 292) +(-260, 128) +(310, 313) +(-128, 383) +(305, 349) +(367, -194) +(90, -235) +(218, -41) +(288, 341) +(-335, -276) +(13, 89) +(-272, 395) +(148, 348) +(-394, -313) +(-94, 85) +(192, 306) +(-9, -364) +(-275, -43) +(306, 273) +(63, -150) +(224, 177) +(-13, -263) +(384, 95) +(-323, 57) +(309, 309) \ No newline at end of file diff --git a/inputs/input5_test b/inputs/input5_test new file mode 100644 index 0000000..96f82c7 --- /dev/null +++ b/inputs/input5_test @@ -0,0 +1,8 @@ +(-16, -191) +(92, 186) +(157, -75) +(39, -132) +(-42, 139) +(-74, -150) +(200, 197) +(-106, 105) \ No newline at end of file diff --git a/problem5.py b/problem5.py new file mode 100644 index 0000000..6c2fd97 --- /dev/null +++ b/problem5.py @@ -0,0 +1,54 @@ +from soupsieve import closest +from tools.coordinate import Coordinate, DistanceAlgorithm + +my_input = open("inputs/input5", "r").read().splitlines() +my_pos = Coordinate(0, 0) +coords = [] +for line in my_input: + x, y = line.split(", ") + coords.append(Coordinate(int(x[1:]), int(y[:-1]))) + +p3 = 0 + + +def get_closest_to(start: Coordinate, exclude: list[Coordinate] = None) -> Coordinate: + if exclude is None: + exclude = [] + + closest_coord = None + closest_dist = int(1e10) + for c in coords: + if c == start or c in exclude: + continue + + next_dist = c.getDistanceTo(closest, algorithm=DistanceAlgorithm.MANHATTAN, includeDiagonals=False) + if next_dist < closest_dist: + closest_dist = next_dist + closest_coord = c + + return closest_coord + + +min_dist = int(1e10) +max_dist = 0 +closest = None +for c in coords: + dist = Coordinate(0, 0).getDistanceTo(c, algorithm=DistanceAlgorithm.MANHATTAN, includeDiagonals=False) + if min_dist > dist: + min_dist = dist + closest = c + max_dist = max(max_dist, dist) + +p1 = max_dist - min_dist +p2 = closest.getDistanceTo(get_closest_to(closest), algorithm=DistanceAlgorithm.MANHATTAN, includeDiagonals=False) +p3 = my_pos.getDistanceTo(closest, algorithm=DistanceAlgorithm.MANHATTAN, includeDiagonals=False) +visited = [closest] +while len(visited) < len(coords): + next_island = get_closest_to(closest, exclude=visited) + p3 += next_island.getDistanceTo(closest, algorithm=DistanceAlgorithm.MANHATTAN, includeDiagonals=False) + visited.append(next_island) + closest = next_island + +print(p1) +print(p2) +print(p3)