Factor out parse_rows_of_integers(input) to a new Aoc2024.Utils module

This commit is contained in:
Aurélien Geron
2024-12-07 12:38:40 +13:00
parent 761adb7faf
commit 6925d4b44e
2 changed files with 17 additions and 15 deletions
+1 -15
View File
@@ -1,20 +1,6 @@
defmodule Aoc2024.Day01 do defmodule Aoc2024.Day01 do
def parse(input) do def parse(input) do
numbers = Aoc2024.Utils.parse_rows_of_integers(input)
input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line
|> String.split(~r/\s+/)
|> Enum.map(fn int_str ->
case int_str |> Integer.parse() do
{int, ""} -> int
_ -> raise "Invalid integer #{int_str}"
end
end)
end)
numbers
|> Enum.map(&List.to_tuple/1) |> Enum.map(&List.to_tuple/1)
|> Enum.unzip() |> Enum.unzip()
end end
+16
View File
@@ -0,0 +1,16 @@
defmodule Aoc2024.Utils do
def parse_rows_of_integers(input) do
input
|> String.split("\n", trim: true)
|> Enum.map(fn line ->
line
|> String.split(~r/\s+/)
|> Enum.map(fn int_str ->
case int_str |> Integer.parse() do
{int, ""} -> int
_ -> raise "Invalid integer #{int_str}"
end
end)
end)
end
end