-
Notifications
You must be signed in to change notification settings - Fork 3
/
day12.ex
147 lines (119 loc) · 3.08 KB
/
day12.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
defmodule Elixir2016.Day12 do
alias Elixir2016.Day12.BunnyVM
def part1(filename) do
vm =
parse(filename)
|> BunnyVM.new()
|> BunnyVM.execute()
vm.registers
end
def part2(filename) do
vm =
parse(filename)
|> BunnyVM.new()
|> BunnyVM.register_set("c", 1)
|> BunnyVM.execute()
vm.registers
end
def parse(filename) do
File.stream!(filename)
|> Stream.map(&String.trim/1)
|> Stream.map(&String.split/1)
|> Enum.map(fn list ->
list |> Enum.map(&try_parse_int/1)
end)
end
def try_parse_int(string) do
case Integer.parse(string) do
:error -> string
{num, _} -> num
end
end
end
defmodule Elixir2016.Day12.BunnyVM do
alias Elixir2016.Day12.BunnyVM
defstruct [
:memory,
:pc,
:registers
]
def init_registers() do
Enum.to_list(?a..?d)
|> List.to_string()
|> String.graphemes()
|> Map.new(fn letter -> {letter, 0} end)
end
def new(memory) do
memory_map =
memory
|> Enum.with_index()
|> Enum.map(fn {line, i} -> {i, line} end)
|> Enum.into(%{})
%BunnyVM{
memory: memory_map,
pc: 0,
registers: init_registers()
}
end
def register_set(vm, reg, value) do
registers = Map.put(vm.registers, reg, value)
%{vm | registers: registers}
end
def lookup(_, arg) when is_integer(arg), do: arg
def lookup(vm, arg) when is_binary(arg) do
vm.registers |> Map.get(arg)
end
def execute(%BunnyVM{memory: memory, pc: pc} = vm) do
instruction = memory |> Map.get(pc)
if instruction != nil do
do_execute_step(vm, instruction)
|> execute()
else
vm
end
end
# Optimization: pc 4 always jumps to pc 9
def do_execute_step(%{pc: 4} = vm, _) do
%{vm | pc: 9}
end
# Optimization: pc 10-12: Sets a += b, b = 0, then moves to 13
def do_execute_step(%{pc: 10} = vm, _inst) do
a = Map.get(vm.registers, "a")
b = Map.get(vm.registers, "b")
registers =
vm.registers
|> Map.put("a", a + b)
|> Map.put("b", 0)
%{vm | registers: registers, pc: 12}
end
# Optimization: pc 18-20: Sets a += d, d = 0, then moves to 21
def do_execute_step(%{pc: 18} = vm, _) do
a = Map.get(vm.registers, "a")
d = Map.get(vm.registers, "d")
registers =
vm.registers
|> Map.put("a", a + d)
|> Map.put("d", 0)
%{vm | registers: registers, pc: 20}
end
def do_execute_step(vm, ["cpy", arg1, arg2]) do
registers = Map.put(vm.registers, arg2, lookup(vm, arg1))
%{vm | registers: registers, pc: vm.pc + 1}
end
def do_execute_step(vm, ["inc", arg1]) do
registers = Map.update!(vm.registers, arg1, fn x -> x + 1 end)
%{vm | registers: registers, pc: vm.pc + 1}
end
def do_execute_step(vm, ["dec", arg1]) do
registers = Map.update!(vm.registers, arg1, fn x -> x - 1 end)
%{vm | registers: registers, pc: vm.pc + 1}
end
def do_execute_step(vm, ["jnz", arg1, arg2]) do
arg1 = lookup(vm, arg1)
if arg1 != 0 do
%{vm | pc: vm.pc + arg2}
else
%{vm | pc: vm.pc + 1}
end
end
end