-
Notifications
You must be signed in to change notification settings - Fork 3
/
day25.ex
178 lines (149 loc) · 4.06 KB
/
day25.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
defmodule Elixir2016.Day25 do
alias Elixir2016.Day25.BunnyVM
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
def part1(filename) do
prog = parse(filename)
len = 10
desired_signal =
Stream.iterate(0, fn x ->
if x == 0, do: 1, else: 0
end)
|> Enum.take(len)
Stream.iterate(1, fn x -> x + 1 end)
|> Stream.map(fn x ->
signal = vm_signal_from_value(prog, x, len)
{x, signal}
end)
|> Stream.filter(fn {_x, signal} ->
signal == desired_signal
end)
|> Enum.take(1)
|> Enum.map(fn {x, _signal} -> x end)
end
def vm_signal_from_value(prog, init_val, len) do
vm =
prog
|> BunnyVM.new()
|> BunnyVM.register_set("a", init_val)
|> BunnyVM.execute(len)
vm.output
end
end
defmodule Elixir2016.Day25.BunnyVM do
alias Elixir2016.Day25.BunnyVM
defstruct [
:memory,
:pc,
:registers,
:toggled,
:output
]
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(),
toggled: false,
output: []
}
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
# Execute until we have `len` number of outputs
def execute(%BunnyVM{memory: memory, pc: pc} = vm, len) do
instruction = memory |> Map.get(pc)
if instruction != nil and length(vm.output) < len do
# %{pc: pc, i: instruction} |> IO.inspect()
# %{pc: pc, i: instruction, registers: vm.registers} |> IO.inspect()
do_execute_step(vm, instruction)
|> execute(len)
else
vm
end
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)
arg2 = lookup(vm, arg2)
if arg1 != 0 do
%{vm | pc: vm.pc + arg2}
else
%{vm | pc: vm.pc + 1}
end
end
def do_execute_step(vm, ["out", arg1]) do
arg1 = lookup(vm, arg1)
%{vm | pc: vm.pc + 1, output: Enum.concat(vm.output, [arg1])}
end
def do_execute_step(vm, ["tgl", arg1]) do
arg1 = lookup(vm, arg1)
toggle_idx = vm.pc + arg1
if Map.has_key?(vm.memory, toggle_idx) do
# "Toggling index #{toggle_idx}" |> IO.inspect()
memory = Map.update!(vm.memory, toggle_idx, &toggle_instruction/1)
%{vm | memory: memory, pc: vm.pc + 1}
else
%{vm | pc: vm.pc + 1}
end
end
def toggle_instruction([inst, a, b]) do
[toggle_instruction(inst), a, b]
end
def toggle_instruction([inst, a]) do
[toggle_instruction(inst), a]
end
def toggle_instruction(inst) do
# For one-argument instructions, inc becomes dec, and all other one-argument instructions become inc.
# For two-argument instructions, jnz becomes cpy, and all other two-instructions become jnz.
case inst do
## One
"inc" -> "dec"
"dec" -> "inc"
"tgl" -> "inc"
## Two
"cpy" -> "jnz"
"jnz" -> "cpy"
end
end
end