From 6925d4b44e47909f3f30f134d61a68326b00e6d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Geron?= Date: Sat, 7 Dec 2024 12:38:40 +1300 Subject: [PATCH] Factor out parse_rows_of_integers(input) to a new Aoc2024.Utils module --- lib/aoc2024/day_01.ex | 16 +--------------- lib/aoc2024/utils.ex | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 15 deletions(-) create mode 100644 lib/aoc2024/utils.ex diff --git a/lib/aoc2024/day_01.ex b/lib/aoc2024/day_01.ex index 2e08032..c738877 100644 --- a/lib/aoc2024/day_01.ex +++ b/lib/aoc2024/day_01.ex @@ -1,20 +1,6 @@ defmodule Aoc2024.Day01 do def parse(input) do - numbers = - 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 + Aoc2024.Utils.parse_rows_of_integers(input) |> Enum.map(&List.to_tuple/1) |> Enum.unzip() end diff --git a/lib/aoc2024/utils.ex b/lib/aoc2024/utils.ex new file mode 100644 index 0000000..e1b9e27 --- /dev/null +++ b/lib/aoc2024/utils.ex @@ -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