fork download
  1. defmodule Main do
  2.  
  3. IO.puts("Starting")
  4.  
  5. input_program = [1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,6,19,23,2,23,6,27,1,5,27,31,1,10,31,35,2,6,35,39,1,39,13,43,1,43,9,47,2,47,10,51,1,5,51,55,1,55,10,59,2,59,6,63,2,6,63,67,1,5,67,71,2,9,71,75,1,75,6,79,1,6,79,83,2,83,9,87,2,87,13,91,1,10,91,95,1,95,13,99,2,13,99,103,1,103,10,107,2,107,10,111,1,111,9,115,1,115,2,119,1,9,119,0,99,2,0,14,0]
  6.  
  7. defmodule LoopThroughArray do
  8. def runcommand(list, n) when n >= length(list)-1 do
  9. IO.puts Enum.at(list,0)
  10. end
  11.  
  12. def runcommand(list, n) do
  13. command = Enum.at(list,n)
  14. p1 = Enum.at(list,n+1)
  15. p2 = Enum.at(list,n+2)
  16. p3 = Enum.at(list,n+3)
  17. v1 = Enum.at(list,p1)
  18. v2 = Enum.at(list,p2)
  19.  
  20. cond do
  21. command == 99 ->
  22. IO.puts Enum.at(list,0)
  23. Enum.at(list,0)
  24. command == 1 ->
  25. vsum = v1+v2
  26. IO.puts "Add #{v1} #{v2} = #{vsum}"
  27. List.delete_at(list, Enum.at(list,p3))
  28. List.insert_at(list, Enum.at(list,p3), vsum)
  29. runcommand(list, n + 4)
  30. command == 2 ->
  31. vprod = v1*v2
  32. IO.puts "Multiply #{v1} #{v2} = #{vprod}"
  33. List.delete_at(list, Enum.at(list,p3))
  34. List.insert_at(list, Enum.at(list,p3), vprod)
  35. runcommand(list, n + 4)
  36. true ->
  37. IO.puts "error"
  38. end
  39. end
  40. end
  41. pos0= LoopThroughArray.runcommand(input_program, 0)
  42.  
  43. IO.puts pos0
  44. IO.puts("Finished")
  45. end
  46.  
  47.  
Success #stdin #stdout 0.27s 35944KB
stdin
Standard input is empty
stdout
Starting
Add 1 1 = 2
Add 0 0 = 0
Add 3 1 = 4
Add 1 1 = 2
Multiply 5 0 = 0
Add 2 19 = 21
Multiply 23 2 = 46
Add 1 27 = 28
Add 4 31 = 35
Multiply 2 35 = 70
Add 39 5 = 44
Add 43 3 = 46
Multiply 47 4 = 188
Add 1 51 = 52
Add 55 4 = 59
Multiply 59 2 = 118
Multiply 2 63 = 126
Add 1 67 = 68
Multiply 3 71 = 213
Add 75 2 = 77
Add 2 79 = 81
Multiply 83 3 = 249
Multiply 87 5 = 435
Add 4 91 = 95
Add 95 5 = 100
Multiply 5 99 = 495
Add 103 4 = 107
Multiply 107 4 = 428
Add 111 3 = 114
Add 115 0 = 115
Add 3 119 = 122
1
1
Finished