day 10 part 2
This commit is contained in:
parent
f45cf1cac5
commit
7c0ebe4c50
2 changed files with 40 additions and 6 deletions
44
lib/day10.ex
44
lib/day10.ex
|
@ -3,15 +3,19 @@ import Integer, only: [mod: 2]
|
|||
defmodule Day10 do
|
||||
@behaviour Solution
|
||||
|
||||
def init_state(_) do
|
||||
%{ vm: VirtualMachine.new(), acc: 0 }
|
||||
def init_state(p) when p == :part1 do
|
||||
%{ vm: VirtualMachine.new(), acc: 0, p: p }
|
||||
end
|
||||
|
||||
def init_state(p) when p == :part2 do
|
||||
%{ vm: VirtualMachine.new(), crt: "", p: p}
|
||||
end
|
||||
|
||||
def execute(line, state) when line == "" do
|
||||
state
|
||||
end
|
||||
|
||||
def execute(line, state) do
|
||||
def execute(line, state) when state.p == :part1 do
|
||||
VirtualMachine.plan_execute(line) |>
|
||||
Enum.reduce(
|
||||
state,
|
||||
|
@ -21,12 +25,42 @@ defmodule Day10 do
|
|||
vm = tock.(vm)
|
||||
%{ vm: vm, acc: acc}
|
||||
end
|
||||
)
|
||||
) |> Map.put(:p, state.p)
|
||||
end
|
||||
|
||||
def get_answer(state) do
|
||||
def execute(line, state) when state.p == :part2 do
|
||||
VirtualMachine.plan_execute(line) |>
|
||||
Enum.reduce(
|
||||
state,
|
||||
fn (tock, %{vm: vm, crt: crt}) ->
|
||||
next_tick = vm.tick + 1
|
||||
|
||||
char = case mod(vm.tick, 40) do
|
||||
x when x >= (vm.x - 1) and x <= (vm.x + 1) -> "#"
|
||||
_ -> "."
|
||||
end
|
||||
|
||||
crt = crt <> char
|
||||
|
||||
crt = crt <> if mod(next_tick, 40) == 0 do
|
||||
"\n"
|
||||
else ""
|
||||
end
|
||||
|
||||
vm = tock.(vm)
|
||||
|
||||
%{ vm: vm, crt: crt}
|
||||
end
|
||||
) |> Map.put(:p, state.p)
|
||||
end
|
||||
|
||||
def get_answer(state) when state.p == :part1 do
|
||||
state.acc
|
||||
end
|
||||
|
||||
def get_answer(state) when state.p == :part2 do
|
||||
state.crt
|
||||
end
|
||||
end
|
||||
|
||||
defmodule VirtualMachine do
|
||||
|
|
2
main.exs
2
main.exs
|
@ -1 +1 @@
|
|||
AOCRunner.run(Day10, :part1)
|
||||
AOCRunner.run(Day10, :part2)
|
||||
|
|
Loading…
Add table
Reference in a new issue