-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_02.ex
61 lines (51 loc) · 1.55 KB
/
day_02.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
defmodule Aoc.Y2019.Day02 do
@moduledoc """
Solved https://adventofcode.com/2019/day/2
"""
import Aoc.Helper.IO
def run_part1 do
get_input()
|> set_initial_data()
|> solve_part1()
end
def run_part2 do
reset_state = get_input() |> set_initial_data()
for noun <- 0..99, verb <- 0..99 do
{noun, verb}
end
|> Enum.filter(fn {noun, verb} ->
reset_state |> set_initial_data(noun, verb) |> solve_part1 === 19_690_720
end)
|> get_result()
end
def solve_part1(list, cursor \\ 0) when list != [] do
case Enum.at(list, cursor) do
1 -> list |> add(cursor + 1, cursor + 2, cursor + 3) |> solve_part1(cursor + 4)
2 -> list |> multiply(cursor + 1, cursor + 2, cursor + 3) |> solve_part1(cursor + 4)
99 -> hd(list)
_ -> {:error, "Invalid OPcode"}
end
end
def set_initial_data(list, op1 \\ 12, op2 \\ 2) do
[opcode | [_ | [_ | list]]] = list
[opcode | [op1 | [op2 | list]]]
end
defp add(list, op1, op2, res) do
value = Enum.at(list, Enum.at(list, op1)) + Enum.at(list, Enum.at(list, op2))
List.replace_at(list, Enum.at(list, res), value)
end
defp multiply(list, op1, op2, res) do
value = Enum.at(list, Enum.at(list, op1)) * Enum.at(list, Enum.at(list, op2))
List.replace_at(list, Enum.at(list, res), value)
end
def get_result([{noun, verb}]) do
100 * noun + verb
end
def get_input() do
get_string_input("2019", "02")
|> String.trim()
|> String.split(",")
|> Enum.map(&String.to_integer/1)
end
def solved_status(), do: :solved
end