@@ -26,17 +26,24 @@ sample_input_2 =
26
26
defmodule PipeMaze do
27
27
def number_of_steps_to_farthest_point (input) do
28
28
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 )
31
36
end
32
37
33
- defp get_initial_paths (map) do
38
+ defp get_initial_path (map) do
34
39
start_location = get_start_location (map)
35
40
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]
40
47
end
41
48
42
49
@neighbour_offsets [{- 1 , 0 }, {0 , 1 }, {1 , 0 }, {0 , - 1 }]
@@ -50,13 +57,12 @@ defmodule PipeMaze do
50
57
defp is_within_bounds? ({row, col}, map),
51
58
do: row >= 0 and row <= length (map) and col >= 0 and col <= length (hd (map))
52
59
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 )
55
62
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)
60
66
end
61
67
end
62
68
@@ -72,11 +78,6 @@ defmodule PipeMaze do
72
78
73
79
defp advance_path (map, [last, previous | _ ] = path) do
74
80
next = get_reachable_neighbours (map, last) |> Enum .find (& (&1 != previous))
75
-
76
- if next == nil do
77
- raise " boom : #{ inspect (path)} "
78
- end
79
-
80
81
[next | path]
81
82
end
82
83
@@ -122,9 +123,7 @@ Path.join(__DIR__, "day-10.input")
122
123
|> File .read! ()
123
124
|> PipeMaze .number_of_steps_to_farthest_point ()
124
125
125
- # ?
126
+ # 7173
126
127
```
127
128
128
- ``` elixir
129
-
130
- ```
129
+ ## Part two
0 commit comments