Skip to content

Commit 05662d9

Browse files
committedDec 12, 2023
day 10 (refactor part one, to prepare for part two)
1 parent 432c1d7 commit 05662d9

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed
 

‎day-10.livemd

+21-22
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,24 @@ sample_input_2 =
2626
defmodule PipeMaze do
2727
def number_of_steps_to_farthest_point(input) do
2828
map = parse_map(input)
29-
initial_paths = get_initial_paths(map)
30-
number_of_steps_farthest_from(map, initial_paths)
29+
30+
[_, start_location] = initial_path = get_initial_path(map)
31+
32+
initial_path
33+
|> find_loop_path(start_location, map)
34+
|> length()
35+
|> div(2)
3136
end
3237

33-
defp get_initial_paths(map) do
38+
defp get_initial_path(map) do
3439
start_location = get_start_location(map)
3540

36-
start_location
37-
|> get_neighbours(map)
38-
|> Enum.filter(&(start_location in get_reachable_neighbours(map, &1)))
39-
|> Enum.map(&[&1, start_location])
41+
[connected_location_1, _connected_location_2] =
42+
start_location
43+
|> get_neighbours(map)
44+
|> Enum.filter(&(start_location in get_reachable_neighbours(map, &1)))
45+
46+
[connected_location_1, start_location]
4047
end
4148

4249
@neighbour_offsets [{-1, 0}, {0, 1}, {1, 0}, {0, -1}]
@@ -50,13 +57,12 @@ defmodule PipeMaze do
5057
defp is_within_bounds?({row, col}, map),
5158
do: row >= 0 and row <= length(map) and col >= 0 and col <= length(hd(map))
5259

53-
defp number_of_steps_farthest_from(map, paths_followed) do
54-
[first_path, second_path] = new_paths = Enum.map(paths_followed, &advance_path(map, &1))
60+
defp find_loop_path(acc_path, start_location, map) do
61+
[hd | rest] = new_path = advance_path(map, acc_path)
5562

56-
cond do
57-
hd(first_path) == hd(second_path) -> length(first_path) - 1
58-
hd(first_path) in second_path -> length(first_path) - 2
59-
true -> number_of_steps_farthest_from(map, new_paths)
63+
case hd do
64+
^start_location -> rest
65+
_ -> find_loop_path(new_path, start_location, map)
6066
end
6167
end
6268

@@ -72,11 +78,6 @@ defmodule PipeMaze do
7278

7379
defp advance_path(map, [last, previous | _] = path) do
7480
next = get_reachable_neighbours(map, last) |> Enum.find(&(&1 != previous))
75-
76-
if next == nil do
77-
raise "boom : #{inspect(path)}"
78-
end
79-
8081
[next | path]
8182
end
8283

@@ -122,9 +123,7 @@ Path.join(__DIR__, "day-10.input")
122123
|> File.read!()
123124
|> PipeMaze.number_of_steps_to_farthest_point()
124125

125-
# ?
126+
# 7173
126127
```
127128

128-
```elixir
129-
130-
```
129+
## Part two

0 commit comments

Comments
 (0)
Please sign in to comment.