Skip to content

Commit a94f3d6

Browse files
committed
Refactor find_mirrorH function to use Enum.drop_while and Enum.at
1 parent 9a06bf1 commit a94f3d6

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

13/sol.exs

+6-13
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,15 @@ defmodule Day13 do
4545
|> Kernel.==(target)
4646
end
4747

48-
def find_mirrorH(pattern, type, target) do
49-
Enum.reduce(
50-
1..(length(lim_f(type).(pattern, 0)) - 1),
51-
nil,
52-
fn mirror_loc, acc ->
53-
case acc do
54-
nil -> if valid_mirror?(pattern, mirror_loc, type, target), do: mirror_loc, else: nil
55-
_ -> acc
56-
end
57-
end
58-
)
48+
def find_mirror(pattern, type, target) do
49+
1..(length(lim_f(type).(pattern, 0)) - 1)
50+
|> Enum.drop_while(fn x -> !valid_mirror?(pattern, x, type, target) end)
51+
|> Enum.at(0)
5952
end
6053

6154
def find_mirror(pattern, target) do
62-
case find_mirrorH(pattern, :row, target) do
63-
nil -> {:col, find_mirrorH(pattern, :col, target)}
55+
case find_mirror(pattern, :row, target) do
56+
nil -> {:col, find_mirror(pattern, :col, target)}
6457
x -> {:row, x}
6558
end
6659
end

0 commit comments

Comments
 (0)