logo

adventofcode

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

day2.ex (1404B)


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