One more DRY-up

master
Dustin Swan 1 month ago
parent ebaeda4dd4
commit 20b941641c
Signed by: dustinswan
GPG Key ID: AB49BD6B2B3A6377

@ -4,7 +4,7 @@ defmodule Day5 do
{invalidUpdates, validUpdates} = Enum.split_with(updates, &updateInvalid?(&1, rules)) {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 # 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 = invalidSorted =
@ -12,7 +12,7 @@ defmodule Day5 do
Enum.sort(update, &Enum.member?(rules, [&1, &2])) Enum.sort(update, &Enum.member?(rules, [&1, &2]))
end) end)
IO.puts("Part 2: #{Enum.sum(Enum.map(invalidSorted, &middleDigit/1))}") IO.puts("Part 2: #{sumMiddles(invalidSorted)}")
end end
defp parseFile(file) do defp parseFile(file) do
@ -31,9 +31,12 @@ defmodule Day5 do
end) end)
end end
defp middleDigit(l) do defp sumMiddles(updates) do
middle = trunc(length(l) / 2) Enum.map(updates, fn update ->
String.to_integer(Enum.at(l, middle)) middle = trunc(length(update) / 2)
String.to_integer(Enum.at(update, middle))
end)
|> Enum.sum()
end end
end end

Loading…
Cancel
Save