optimize day 5 solution
This commit is contained in:
parent
25c462652f
commit
f6f74d5343
1 changed files with 5 additions and 7 deletions
|
@ -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
|
||||
```
|
||||
|
|
Loading…
Add table
Reference in a new issue