Skip to content

Commit 6ad3cc1

Browse files
Add Day 09 Python solution
1 parent add7eb9 commit 6ad3cc1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Day-09/python/das_/day09.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
red_tiles = list(map(lambda line: (int((coordinates := line.split(","))[0]), int(coordinates[1])), open("input09.txt").read().split("\n")))
2+
areas = []
3+
4+
for i in range(len(red_tiles)):
5+
for j in range(i + 1, len(red_tiles)):
6+
area = (abs(red_tiles[i][0] - red_tiles[j][0]) + 1) * (abs(red_tiles[i][1] - red_tiles[j][1]) + 1)
7+
areas.append((red_tiles[i], red_tiles[j], area, i))
8+
9+
sorted_areas = sorted(areas, key=lambda area: area[2], reverse=True)
10+
print("Part 1:", sorted_areas[0][2])
11+
12+
for area in sorted_areas:
13+
left, right = sorted([area[0][0], area[1][0]])
14+
down, up = sorted([area[0][1], area[1][1]])
15+
x2, y2 = area[0]
16+
valid = True
17+
for i in range(len(red_tiles)):
18+
x1, y1 = x2, y2
19+
x2, y2 = red_tiles[(area[3] + i) % len(red_tiles)][0], red_tiles[(area[3] + i) % len(red_tiles)][1]
20+
x_l, x_h = sorted([x1, x2])
21+
y_l, y_h = sorted([y1, y2])
22+
if (left < x1 < right and down < y1 < up) or (down < y1 < up and x_l <= left < x_h and x_l < right <= x_h) or (left < x1 < right and y_l <= down < y_h and y_l < up <= y_h):
23+
valid = False
24+
break
25+
if valid:
26+
part2 = area[2]
27+
break
28+
29+
print("Part 2:", part2)

0 commit comments

Comments
 (0)