diff --git a/elixir/livebook/2024/day5.livemd b/elixir/livebook/2024/day5.livemd index 624df08..da9ab3a 100644 --- a/elixir/livebook/2024/day5.livemd +++ b/elixir/livebook/2024/day5.livemd @@ -15,9 +15,11 @@ input = """ rules = for rule <- rules |> String.split("\n"), [a, b] = rule |> String.split("|"), a = String.to_integer(a), - b = String.to_integer(b) + b = String.to_integer(b), + reduce: %{} do - {a, b} + acc -> + Map.update(acc, a, MapSet.new([b]), fn c -> MapSet.put(c, b) end) end ``` @@ -33,11 +35,9 @@ updates = for update <- updates |> String.trim() |> String.split("\n"), ```elixir rule_sorter = fn a, b -> - a_precedes_b = Enum.any?(rules, &(&1 == {a, b})) - b_precedes_a = Enum.any?(rules, &(&1 == {b, a})) + b_precedes_a = Map.get(rules, b, MapSet.new()) |> MapSet.member?(a) cond do b_precedes_a -> false - a_precedes_b -> true true -> true end end @@ -53,6 +53,4 @@ for update <- updates, mid_idx = floor(len / 2) acc + Enum.at(sorted, mid_idx) end - -# 10495 too high ```