Skip to content

Commit cb214c3

Browse files
committed
Day 23 ⭐⭐
1 parent 4caea82 commit cb214c3

File tree

4 files changed

+64
-44
lines changed

4 files changed

+64
-44
lines changed

2023/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,7 @@ Absolute ugly brute force. Best not to run this...
168168
### 2023-12-23
169169

170170
- Forgot an enumerate
171+
- Had to figure out how to search all paths possible...
172+
- Using networkx, but turns out it doesn't have an easy way to find the "longest path", all set to the shortest one.
173+
- Mixed up the check `has_nodes` and `has_edges`, which gave an error. I should sleep probably...
174+
- Takes a bit to run part 2

2023/day23.py

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
from day import Day
22
from aocd import submit
33
from heapq import heappush, heappop
4+
import networkx as nx
5+
import time
6+
import sys
7+
8+
sys.setrecursionlimit(10000)
49

510

611
class Clouds:
712
def __init__(self, data):
813
self.parse(data)
914
self.max = len(self.grid)
1015
self.visited = set()
16+
self.graph = nx.Graph()
1117

1218
def __repr__(self):
1319
return f"Clouds({self.start}, {self.end})"
@@ -27,7 +33,8 @@ def parse(self, data):
2733

2834
def get_neighbors(self, pos):
2935
for i in [1j, -1j, 1, -1]:
30-
yield pos - i
36+
if pos + i in self.grid:
37+
yield pos + i
3138

3239
def longest_hike(self):
3340
dummy = 0
@@ -60,8 +67,43 @@ def longest_hike(self):
6067
heappush(queue, (dist - 1, dummy := dummy + 1, neighbor, visited.copy()))
6168
return -max_dist
6269

70+
def create_graph(self, dist=0, last_node=None, pos=None, last_pos=None):
71+
if last_node is None:
72+
last_node = self.start
73+
if pos is None:
74+
pos = self.start
75+
76+
# End of the grid
77+
if pos == self.end:
78+
self.graph.add_edge(last_node, pos, weight=dist)
79+
return
80+
# Already visited
81+
if self.graph.has_edge(last_node, pos):
82+
return
83+
84+
neighbors = list(self.get_neighbors(pos))
85+
# Adding a node to the graph when it's a junction
86+
if len(neighbors) > 2:
87+
self.graph.add_edge(last_node, pos, weight=dist)
88+
last_node = pos
89+
dist = 0
90+
# Moving further in the grid
91+
for neighbor in neighbors:
92+
if neighbor in self.grid and neighbor != last_pos:
93+
self.create_graph(dist + 1, last_node, neighbor, pos)
94+
95+
def unconstrained_longest(self):
96+
self.create_graph()
97+
print(self.graph)
98+
# Find longest Path in graph
99+
max_dist = 0
100+
for path in nx.all_simple_paths(G=self.graph, source=self.start, target=self.end):
101+
max_dist = max(max_dist, nx.path_weight(self.graph, path, weight="weight"))
102+
return max_dist
103+
63104
def print(self):
64105
for i in range(1 + int(max(x.real for x in self.grid))):
106+
print(i, end=" ")
65107
for ii in range(1 + int(max(x.imag for x in self.grid))):
66108
if i + ii * 1j in self.visited:
67109
print("O", end="")
@@ -77,49 +119,23 @@ def print(self):
77119
def main(day, part=1):
78120
clouds = Clouds(day.data)
79121
print(clouds)
80-
clouds.longest_hike()
81-
clouds.print()
82122
if part == 1:
83-
return clouds.longest_hike()
123+
x = clouds.longest_hike()
124+
clouds.print()
125+
return x
84126
if part == 2:
85-
pass
127+
return clouds.unconstrained_longest()
86128

87129

88130
if __name__ == "__main__":
89131
day = Day(23)
90132
day.download()
91133

92134
day.load()
93-
# data = """#.#####################
94-
# #.......#########...###
95-
# #######.#########.#.###
96-
# ###.....#.>.>.###.#.###
97-
# ###v#####.#v#.###.#.###
98-
# ###.>...#.#.#.....#...#
99-
# ###v###.#.#.#########.#
100-
# ###...#.#.#.......#...#
101-
# #####.#.#.#######.#.###
102-
# #.....#.#.#.......#...#
103-
# #.#####.#.#.#########v#
104-
# #.#...#...#...###...>.#
105-
# #.#.#v#######v###.###v#
106-
# #...#.>.#...>.>.#.###.#
107-
# #####v#.#.###v#.#.###.#
108-
# #.....#...#...#.#.#...#
109-
# #.#########.###.#.#.###
110-
# #...###...#...#...#.###
111-
# ###.###.#.###v#####v###
112-
# #...#...#.#.>.>.#.>.###
113-
# #.###.###.#.###.#.#v###
114-
# #.....###...###...#...#
115-
# #####################.#"""
116-
117-
# day.load(data)
118135
p1 = main(day)
119136
print(p1)
120137
submit(p1, part="a", day=23, year=2023)
121138

122-
# day.load()
123-
# p2 = main(day, part=2)
124-
# print(p2)
125-
# submit(p2, part="b", day=23, year=2023)
139+
p2 = main(day, part=2)
140+
print(p2)
141+
submit(p2, part="b", day=23, year=2023)

2023/tests/test_day23.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@ def test_example_p2(example):
5959

6060

6161
def test_part2(day):
62-
assert main(day, part=2) == False
62+
assert main(day, part=2) == 6490

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ Get acquainted with Advent of Code and solve some of the puzzles in Python.
6060

6161
### [2023](./2023/)
6262

63-
| | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th | 10th | 11th | 12th | 13th |
64-
| ------ | --- | --- | --- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- |
65-
| Part 1 ||||||||||||||
66-
| Part 2 ||||||||||||||
67-
68-
| | 14th | 15th | 16th | 17th | 18th | 19th | 20th | 21st | 22nd | 23rd | 24th | 25th |
69-
| ------ | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- | ---------------- |
70-
| Part 1 |||||||| <!--2023.21.1--> ||| <!--2023.24.1--> | <!--2023.25.1--> |
71-
| Part 2 |||||||| <!--2023.21.2--> || <!--2023.23.2--> | <!--2023.24.2--> | <!--2023.25.2--> |
63+
| | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th | 10th | 11th | 12th | 13th |
64+
| ------ | --- | --- | --- | --- | --- | --- | --- | --- | --- | ---- | ---- | ---- | ---- |
65+
| Part 1 |||| | | | | | | | | | |
66+
| Part 2 |||| | | | | | | | | | |
67+
68+
| | 14th | 15th | 16th | 17th | 18th | 19th | 20th | 21st | 22nd | 23rd | 24th | 25th |
69+
| ------ | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---------------- | ---------------- |
70+
| Part 1 | | | | | | | || | | <!--2023.24.1--> | <!--2023.25.1--> |
71+
| Part 2 | | | | | | | || | | <!--2023.24.2--> | <!--2023.25.2--> |
7272

7373
### [2022](./2022/)
7474

0 commit comments

Comments
 (0)