Here's my solution for part 2 in Elixir. It shares most of its code with part 1, so I don't think it's all that interesting to share both.
defmodule BootLoader do def instr("nop " <> _value, acc, ptr) do {acc, ptr + 1} end def instr("acc " <> value, acc, ptr) do {acc + String.to_integer(value), ptr + 1} end def instr("jmp " <> value, acc, ptr) do {acc, ptr + String.to_integer(value)} end def execute(lines, seen, acc, ptr) do IO.puts("Executing #{ptr}") cond do ptr in seen -> IO.puts("already seen!") false ptr == length(lines) -> IO.puts("final result #{acc}") true true -> {new_acc, new_ptr} = instr(Enum.at(lines, ptr), acc, ptr) execute(lines, [ptr | seen], new_acc, new_ptr) end end def get_replacement("nop" <> x) do "jmp" <> x end def get_replacement("jmp" <> x) do "nop" <> x end def get_replacement(foo) do foo end def try_replacement(lines, rep_index) do rep = get_replacement(Enum.at(lines, rep_index)) lines2 = List.replace_at(lines, rep_index, rep) if not execute(lines2, [], 0, 0) do try_replacement(lines, rep_index + 1) end end def main() do {:ok, code} = File.read("8.txt") lines = String.split(code, "\n") lines = List.delete_at(lines, length(lines)-1) IO.puts("program size: #{length(lines)}") try_replacement(lines, 0) end end BootLoader.main()
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here's my solution for part 2 in Elixir. It shares most of its code with part 1, so I don't think it's all that interesting to share both.