Day 5
This commit is contained in:
parent
e7408ad2ff
commit
1b94a352b1
|
@ -0,0 +1,205 @@
|
|||
defmodule Day5 do
|
||||
def read_program do
|
||||
File.read!("lib/day5/input.txt")
|
||||
|> String.trim()
|
||||
|> String.split(",")
|
||||
|> Enum.map(&String.to_integer/1)
|
||||
end
|
||||
|
||||
def run_program do
|
||||
read_program()
|
||||
|> run()
|
||||
end
|
||||
|
||||
def test do
|
||||
run([
|
||||
3,
|
||||
21,
|
||||
1008,
|
||||
21,
|
||||
8,
|
||||
20,
|
||||
1005,
|
||||
20,
|
||||
22,
|
||||
107,
|
||||
8,
|
||||
21,
|
||||
20,
|
||||
1006,
|
||||
20,
|
||||
31,
|
||||
1106,
|
||||
0,
|
||||
36,
|
||||
98,
|
||||
0,
|
||||
0,
|
||||
1002,
|
||||
21,
|
||||
125,
|
||||
20,
|
||||
4,
|
||||
20,
|
||||
1105,
|
||||
1,
|
||||
46,
|
||||
104,
|
||||
999,
|
||||
1105,
|
||||
1,
|
||||
46,
|
||||
1101,
|
||||
1000,
|
||||
1,
|
||||
20,
|
||||
4,
|
||||
20,
|
||||
1105,
|
||||
1,
|
||||
46,
|
||||
98,
|
||||
99
|
||||
])
|
||||
end
|
||||
|
||||
def run(memory, ip \\ 0)
|
||||
|
||||
def run(memory, ip) when ip < length(memory) do
|
||||
IO.inspect(memory)
|
||||
|
||||
case eval(Enum.drop(memory, ip), memory) do
|
||||
{memory, :halt} ->
|
||||
memory
|
||||
|
||||
{memory, :cont, offset} ->
|
||||
run(memory, ip + offset)
|
||||
|
||||
{memory, :jump, ip} ->
|
||||
run(memory, ip)
|
||||
end
|
||||
end
|
||||
|
||||
def run(memory, _ip), do: memory
|
||||
|
||||
defmacro opcode(op, expected) do
|
||||
quote do
|
||||
rem(unquote(op), 100) == unquote(expected)
|
||||
end
|
||||
end
|
||||
|
||||
def get_param(op, param, val, memory) do
|
||||
place = param + 2
|
||||
|
||||
if op |> div(pow(10, place)) |> rem(10) == 1 do
|
||||
val
|
||||
else
|
||||
Enum.at(memory, val)
|
||||
end
|
||||
end
|
||||
|
||||
def pow(base, 1), do: base
|
||||
|
||||
def pow(base, exp) do
|
||||
base * pow(base, exp - 1)
|
||||
end
|
||||
|
||||
# halt
|
||||
def eval([99 | _], memory) do
|
||||
{memory, :halt}
|
||||
end
|
||||
|
||||
# add
|
||||
def eval([op, a, b, dest | _], memory) when opcode(op, 1) do
|
||||
a_val = get_param(op, 0, a, memory)
|
||||
b_val = get_param(op, 1, b, memory)
|
||||
|
||||
{
|
||||
List.replace_at(memory, dest, a_val + b_val),
|
||||
:cont,
|
||||
4
|
||||
}
|
||||
end
|
||||
|
||||
# multiply
|
||||
def eval([op, a, b, dest | _], memory) when opcode(op, 2) do
|
||||
a_val = get_param(op, 0, a, memory)
|
||||
b_val = get_param(op, 1, b, memory)
|
||||
|
||||
{
|
||||
List.replace_at(memory, dest, a_val * b_val),
|
||||
:cont,
|
||||
4
|
||||
}
|
||||
end
|
||||
|
||||
# input
|
||||
def eval([3, addr | _], memory) do
|
||||
res = IO.gets("intcode> ") |> String.trim() |> String.to_integer()
|
||||
|
||||
{
|
||||
List.replace_at(memory, addr, res),
|
||||
:cont,
|
||||
2
|
||||
}
|
||||
end
|
||||
|
||||
# output
|
||||
def eval([op, param | _], memory) when opcode(op, 4) do
|
||||
get_param(op, 0, param, memory) |> IO.inspect()
|
||||
|
||||
{memory, :cont, 2}
|
||||
end
|
||||
|
||||
# jump if non-zero
|
||||
def eval([op, condition, ip | _], memory) when opcode(op, 5) do
|
||||
case get_param(op, 0, condition, memory) do
|
||||
0 ->
|
||||
{memory, :cont, 3}
|
||||
|
||||
_ ->
|
||||
{memory, :jump, get_param(op, 1, ip, memory)}
|
||||
end
|
||||
end
|
||||
|
||||
# jump if zero
|
||||
def eval([op, condition, ip | _], memory) when opcode(op, 6) do
|
||||
case get_param(op, 0, condition, memory) do
|
||||
0 ->
|
||||
{memory, :jump, get_param(op, 1, ip, memory)}
|
||||
|
||||
_ ->
|
||||
{memory, :cont, 3}
|
||||
end
|
||||
end
|
||||
|
||||
# less than
|
||||
def eval([op, a, b, dest | _], memory) when opcode(op, 7) do
|
||||
a_val = get_param(op, 0, a, memory)
|
||||
b_val = get_param(op, 1, b, memory)
|
||||
|
||||
memory =
|
||||
if a_val < b_val do
|
||||
List.replace_at(memory, dest, 1)
|
||||
else
|
||||
List.replace_at(memory, dest, 0)
|
||||
end
|
||||
|
||||
{memory, :cont, 4}
|
||||
end
|
||||
|
||||
# equals
|
||||
def eval([op, a, b, dest | _], memory) when opcode(op, 8) do
|
||||
a_val = get_param(op, 0, a, memory)
|
||||
b_val = get_param(op, 1, b, memory)
|
||||
|
||||
memory =
|
||||
if a_val == b_val do
|
||||
List.replace_at(memory, dest, 1)
|
||||
else
|
||||
List.replace_at(memory, dest, 0)
|
||||
end
|
||||
|
||||
{memory, :cont, 4}
|
||||
end
|
||||
end
|
|
@ -0,0 +1 @@
|
|||
3,225,1,225,6,6,1100,1,238,225,104,0,2,171,209,224,1001,224,-1040,224,4,224,102,8,223,223,1001,224,4,224,1,223,224,223,102,65,102,224,101,-3575,224,224,4,224,102,8,223,223,101,2,224,224,1,223,224,223,1102,9,82,224,1001,224,-738,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1101,52,13,224,1001,224,-65,224,4,224,1002,223,8,223,1001,224,6,224,1,223,224,223,1102,82,55,225,1001,213,67,224,1001,224,-126,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1,217,202,224,1001,224,-68,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,1002,176,17,224,101,-595,224,224,4,224,102,8,223,223,101,2,224,224,1,224,223,223,1102,20,92,225,1102,80,35,225,101,21,205,224,1001,224,-84,224,4,224,1002,223,8,223,1001,224,1,224,1,224,223,223,1101,91,45,225,1102,63,5,225,1101,52,58,225,1102,59,63,225,1101,23,14,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,1008,677,677,224,1002,223,2,223,1006,224,329,101,1,223,223,1108,226,677,224,1002,223,2,223,1006,224,344,101,1,223,223,7,677,226,224,102,2,223,223,1006,224,359,1001,223,1,223,8,677,226,224,102,2,223,223,1005,224,374,1001,223,1,223,1107,677,226,224,102,2,223,223,1006,224,389,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,7,226,677,224,102,2,223,223,1005,224,419,1001,223,1,223,1007,677,677,224,102,2,223,223,1006,224,434,1001,223,1,223,107,226,226,224,1002,223,2,223,1005,224,449,1001,223,1,223,1008,677,226,224,102,2,223,223,1006,224,464,1001,223,1,223,1007,677,226,224,1002,223,2,223,1005,224,479,1001,223,1,223,108,677,677,224,1002,223,2,223,1006,224,494,1001,223,1,223,108,226,226,224,1002,223,2,223,1006,224,509,101,1,223,223,8,226,677,224,102,2,223,223,1006,224,524,101,1,223,223,107,677,226,224,1002,223,2,223,1005,224,539,1001,223,1,223,8,226,226,224,102,2,223,223,1005,224,554,101,1,223,223,1108,677,226,224,102,2,223,223,1006,224,569,101,1,223,223,108,677,226,224,102,2,223,223,1006,224,584,1001,223,1,223,7,677,677,224,1002,223,2,223,1005,224,599,101,1,223,223,1007,226,226,224,102,2,223,223,1005,224,614,1001,223,1,223,1107,226,677,224,102,2,223,223,1006,224,629,101,1,223,223,1107,226,226,224,102,2,223,223,1005,224,644,1001,223,1,223,1108,677,677,224,1002,223,2,223,1005,224,659,101,1,223,223,107,677,677,224,1002,223,2,223,1006,224,674,1001,223,1,223,4,223,99,226
|
Loading…
Reference in New Issue