problem 5

This commit is contained in:
Stefan Harmuth 2025-03-22 11:41:18 +01:00
parent 5f1b5de744
commit 60ea8eb791
3 changed files with 122 additions and 0 deletions

60
inputs/input5 Normal file
View File

@ -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)

8
inputs/input5_test Normal file
View File

@ -0,0 +1,8 @@
(-16, -191)
(92, 186)
(157, -75)
(39, -132)
(-42, 139)
(-74, -150)
(200, 197)
(-106, 105)

54
problem5.py Normal file
View File

@ -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)