logo

adventofcode

Unnamed repository; edit this file 'description' to name the repository.
commit: d54a4164b3a23faf48d61643c7007164d1b2a9b9
parent: 5e085cd22976cc01bf10ecbb1a2d1e801fdf2618
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Mon,  2 Dec 2019 13:57:11 +0100

day2: part two

Diffstat:

Mlib/day2.ex5+++++
Ascripts/day2_2.ex40++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+), 0 deletions(-)

diff --git a/lib/day2.ex b/lib/day2.ex @@ -13,4 +13,9 @@ defmodule Day2 do def opcode([99, _, _, _], _) do :halt end + + # noop + def opcode(_, _) do + :noop + end end diff --git a/scripts/day2_2.ex b/scripts/day2_2.ex @@ -0,0 +1,40 @@ +line = + "1,1,2,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,6,19,23,1,23,13,27,2,6,27,31,1,5,31,35,2,10,35,39,1,6,39,43,1,13,43,47,2,47,6,51,1,51,5,55,1,55,6,59,2,59,10,63,1,63,6,67,2,67,10,71,1,71,9,75,2,75,10,79,1,79,5,83,2,10,83,87,1,87,6,91,2,9,91,95,1,95,5,99,1,5,99,103,1,103,10,107,1,9,107,111,1,6,111,115,1,115,5,119,1,10,119,123,2,6,123,127,2,127,6,131,1,131,2,135,1,10,135,0,99,2,0,14,0" + +{:ok, agent} = Agent.start_link(fn -> [] end) + +Enum.each(0..99, fn n -> + Enum.each(0..99, fn v -> + IO.puts("noun=" <> Integer.to_string(n) <> ", verb=" <> Integer.to_string(v)) + + Agent.update(agent, fn _ -> + line + |> String.split([" ", ",", "\n"]) + |> Enum.map(&String.to_integer(&1)) + |> List.replace_at(1, n) + |> List.replace_at(2, v) + end) + + Agent.get(agent, & &1) + |> Enum.chunk_every(4) + |> Enum.each(fn opcode -> + result = + opcode + |> Day2.opcode(Agent.get(agent, & &1)) + + if :halt == result do + if 19_690_720 == Enum.at(Agent.get(agent, & &1), 0) do + IO.puts( + "100*" <> + Integer.to_string(n) <> + "+" <> Integer.to_string(v) <> "=" <> Integer.to_string(100 * n + v) + ) + + exit(:normal) + end + else + Agent.update(agent, fn _ -> result end) + end + end) + end) +end)