One more DRY-up

master
Dustin Swan 4 weeks 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))
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

Loading…
Cancel
Save