|
|
|
@ -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}
|
|
|
|
|