Switched from maps to lists for the gates
This commit is contained in:
parent
cadb944182
commit
65a54ef8b8
1 changed files with 5 additions and 8 deletions
|
|
@ -1,6 +1,6 @@
|
|||
defmodule Day24 do
|
||||
def run do
|
||||
{wires, gates} = parse_input("test.txt")
|
||||
{wires, gates} = parse_input("data.txt")
|
||||
|
||||
# part1
|
||||
part1 = run_loop(wires, gates) |> wire_value("z") |> b_to_d()
|
||||
|
|
@ -41,15 +41,12 @@ defmodule Day24 do
|
|||
end
|
||||
|
||||
defp run(wires, gates) do
|
||||
Enum.reduce(gates, wires, fn {{left, right}, effects}, acc ->
|
||||
Enum.reduce(gates, wires, fn %{left: left, right: right, output: output, op: op}, acc ->
|
||||
l = Map.get(wires, left, nil)
|
||||
r = Map.get(wires, right, nil)
|
||||
|
||||
if !is_nil(l) and !is_nil(r) do
|
||||
Enum.reduce(effects, acc, fn {op, output}, acc2 ->
|
||||
res = op.(l, r)
|
||||
Map.put(acc2, output, res)
|
||||
end)
|
||||
Map.put(acc, output, op.(l, r))
|
||||
else
|
||||
acc
|
||||
end
|
||||
|
|
@ -70,7 +67,7 @@ defmodule Day24 do
|
|||
end)
|
||||
|
||||
gates =
|
||||
Enum.reduce(gates, %{}, fn el, map ->
|
||||
Enum.reduce(gates, [], fn el, map ->
|
||||
[logic, output] = String.split(el, " -> ")
|
||||
[left, op, right] = String.split(logic, " ")
|
||||
|
||||
|
|
@ -81,7 +78,7 @@ defmodule Day24 do
|
|||
"XOR" -> &Bitwise.bxor/2
|
||||
end
|
||||
|
||||
Map.update(map, {left, right}, [{op, output}], fn acc -> [{op, output} | acc] end)
|
||||
[%{left: left, right: right, op: op, output: output} | map]
|
||||
end)
|
||||
|
||||
{wires, gates}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue