logo

adventofcode

Code used to solve https://adventofcode.com/, one branch per year git clone https://hacktivis.me/git/adventofcode.git

day2_2.ex (1326B)


  1. line =
  2. "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"
  3. {:ok, agent} = Agent.start_link(fn -> [] end)
  4. Enum.each(0..99, fn n ->
  5. Enum.each(0..99, fn v ->
  6. IO.puts("noun=" <> Integer.to_string(n) <> ", verb=" <> Integer.to_string(v))
  7. Agent.update(agent, fn _ ->
  8. line
  9. |> String.split([" ", ",", "\n"])
  10. |> Enum.map(&String.to_integer(&1))
  11. |> List.replace_at(1, n)
  12. |> List.replace_at(2, v)
  13. end)
  14. Agent.get(agent, & &1)
  15. |> Enum.chunk_every(4)
  16. |> Enum.each(fn opcode ->
  17. result =
  18. opcode
  19. |> Day2.opcode(Agent.get(agent, & &1))
  20. if :halt == result do
  21. if 19_690_720 == Enum.at(Agent.get(agent, & &1), 0) do
  22. IO.puts(
  23. "100*" <>
  24. Integer.to_string(n) <>
  25. "+" <> Integer.to_string(v) <> "=" <> Integer.to_string(100 * n + v)
  26. )
  27. exit(:normal)
  28. end
  29. else
  30. Agent.update(agent, fn _ -> result end)
  31. end
  32. end)
  33. end)
  34. end)