From 20b941641ce53c51881257de4abb4a67cd4f72fa Mon Sep 17 00:00:00 2001 From: Dustin Swan Date: Thu, 5 Dec 2024 16:58:17 -0700 Subject: [PATCH] One more DRY-up --- day5/main.exs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/day5/main.exs b/day5/main.exs index 0ee8ea1..46f4a70 100644 --- a/day5/main.exs +++ b/day5/main.exs @@ -4,7 +4,7 @@ defmodule Day5 do {invalidUpdates, validUpdates} = Enum.split_with(updates, &updateInvalid?(&1, rules)) - IO.puts("Part 1: #{Enum.sum(Enum.map(validUpdates, &middleDigit/1))}") + IO.puts("Part 2: #{sumMiddles(validUpdates)}") # sort the invalid ones with a sorter that looks for [a, b] in the rules list. if [a, b] exists, sort a before b invalidSorted = @@ -12,7 +12,7 @@ defmodule Day5 do Enum.sort(update, &Enum.member?(rules, [&1, &2])) end) - IO.puts("Part 2: #{Enum.sum(Enum.map(invalidSorted, &middleDigit/1))}") + IO.puts("Part 2: #{sumMiddles(invalidSorted)}") end defp parseFile(file) do @@ -31,9 +31,12 @@ defmodule Day5 do end) end - defp middleDigit(l) do - middle = trunc(length(l) / 2) - String.to_integer(Enum.at(l, middle)) + defp sumMiddles(updates) do + Enum.map(updates, fn update -> + middle = trunc(length(update) / 2) + String.to_integer(Enum.at(update, middle)) + end) + |> Enum.sum() end end