Tidy up day 5 a bit

This commit is contained in:
Adam Millerchip 2023-12-06 21:57:43 +09:00
parent 684438a5a1
commit f180c206ac
1 changed files with 10 additions and 13 deletions

View File

@ -24,8 +24,8 @@ defmodule Day5 do
maps maps
|> Enum.reduce(seed_ranges, &process_ranges/2) |> Enum.reduce(seed_ranges, &process_ranges/2)
|> Enum.min_by(& &1.first) |> Enum.map(& &1.first)
|> then(& &1.first) |> Enum.min()
end end
def process_ranges(_map_ranges, []), do: [] def process_ranges(_map_ranges, []), do: []
@ -35,23 +35,20 @@ defmodule Day5 do
Enum.find(map_ranges, fn {source, _offset} -> not Range.disjoint?(source, item_range) end) Enum.find(map_ranges, fn {source, _offset} -> not Range.disjoint?(source, item_range) end)
case overlap do case overlap do
nil ->
[item_range | process_ranges(map_ranges, rest)]
{source, offset} -> {source, offset} ->
to_shift = max(source.first, item_range.first)..min(source.last, item_range.last) to_shift = max(source.first, item_range.first)..min(source.last, item_range.last)
# ugh. Tidy this up
remainders = remainders =
List.flatten([ [
if(item_range.first < source.first, if(item_range.first < source.first, do: item_range.first..(source.first - 1)),
do: [item_range.first..(source.first - 1)], if(item_range.last > source.last, do: (source.last + 1)..item_range.last)
else: [] ]
), |> Enum.reject(&is_nil/1)
if(item_range.last > source.last, do: [(source.last + 1)..item_range.last], else: [])
])
[Range.shift(to_shift, offset) | process_ranges(map_ranges, remainders ++ rest)] [Range.shift(to_shift, offset) | process_ranges(map_ranges, remainders ++ rest)]
nil ->
[item_range | process_ranges(map_ranges, rest)]
end end
end end