-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent8.exs
71 lines (60 loc) · 2.02 KB
/
advent8.exs
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
62
63
64
65
66
67
68
69
70
71
defmodule Advent8 do
def calculate(rows) do
registers =
rows
|> Enum.map(&parse_command/1)
|> Enum.reduce(%{max: 0}, &apply_command/2)
IO.inspect("Part 2")
IO.inspect(get_register_value(registers, "max"))
registers = Map.delete(registers, "max")
IO.inspect("Part 1")
IO.inspect(registers |> Map.values() |> Enum.max())
end
def parse_command(row) do
[command, condition] = String.split(row, "if")
%{
command: get_command(command, &get_inc_or_dec/1),
condition: get_command(condition, &get_operation/1)
}
end
def get_command(command, operation_finder) do
[register, command, value] = command |> String.trim() |> String.split(" ")
%{
register: register,
command: operation_finder.(command),
value: String.to_integer(value)
}
end
def get_inc_or_dec("inc"), do: &Kernel.+/2
def get_inc_or_dec("dec"), do: &Kernel.-/2
def get_operation("<"), do: &Kernel.</2
def get_operation("<="), do: &Kernel.<=/2
def get_operation(">"), do: &Kernel.>/2
def get_operation(">="), do: &Kernel.>=/2
def get_operation("=="), do: &Kernel.==/2
def get_operation("!="), do: &Kernel.!=/2
def apply_command(command, registers) do
current_value = get_register_value(registers, command.condition.register)
if command.condition.command.(current_value, command.condition.value) do
value = get_register_value(registers, command.command.register)
new_value = command.command.command.(value, command.command.value)
registers = Map.put(registers, command.command.register, new_value)
max = get_register_value(registers, "max")
if new_value > max do
Map.put(registers, "max", new_value)
else
registers
end
else
registers
end
end
def get_register_value(registers, register) do
case Map.fetch(registers, register) do
{:ok, value} -> value
:error -> 0
end
end
end
{:ok, input} = File.read("input8")
input |> String.trim() |> String.split("\n") |> Advent8.calculate()