-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_08.Rmd
329 lines (281 loc) · 8.42 KB
/
day_08.Rmd
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# Handheld Halting
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(crayon.enabled = NULL)
library(tidyverse)
library(R6)
library(unglue)
START_TIME <- Sys.time()
```
This is my attempt to solve [Day 8](https://adventofcode.com/2020/day/8).
```{r load data}
sample <- read_lines("samples/day_08_sample.txt")
actual <- read_lines("inputs/day_08_input.txt")
```
## Part 1
Today I'm going to build an [R6 class](https://adv-r.hadley.nz/r6.html) to handle the state of the computer.
```{r Computer R6 Class}
Computer <- R6Class(
"Computer",
public = list(
initialize = function(instructions) {
private$instructions <- instructions %>%
unglue_data("{inst} {i}", convert = TRUE) %>%
mutate(c = 0)
},
get_accumulator = function() {
private$accumulator
},
get_instructions_count = function() {
sum(private$instructions$c)
},
run_next = function() {
next_instruction <- private$next_instruction()
private$run_instruction(next_instruction$inst,
next_instruction$i)
invisible(self)
},
run_until_repeat = function() {
repeat {
next_instruction <- private$next_instruction()
if (next_instruction$c > 0) {
break()
}
private$run_instruction(next_instruction$inst,
next_instruction$i)
}
invisible(self)
},
print = function(...) {
i <- private$instructions[private$ptr, ]
cat("Accumulator: ", private$accumulator, "\n")
cat("Pointer: ", private$ptr, "\n")
cat("Instructions Ran:", self$get_instructions_count(), "\n")
cat("Next Instruction:",
i$inst,
" ",
sprintf("%+d", i$i),
" (",
i$c,
")\n")
}
),
private = list(
accumulator = 0,
ptr = 1,
instructions = list(),
next_instruction = function() {
private$instructions[private$ptr, ]
},
run_instruction = function(inst, i) {
private$instructions[private$ptr, "c"] <- 1 +
private$instructions[private$ptr, "c"]
private$ptr <- private[[inst]](i)
},
# instruction functions: must return the next pointer value
acc = function(i) {
private$accumulator <- private$accumulator + i
private$ptr + 1
},
jmp = function(i) {
private$ptr + i
},
nop = function(i) {
private$ptr + 1
}
)
)
```
Now we can initialize our sample computer:
```{r part 1 build computer}
sample_computer <- Computer$new(sample)
sample_computer
```
And we can run the next instruction:
```{r part 1 run next instruction}
sample_computer$run_next()
sample_computer
```
We can run the computer until we repeat an instruction:
```{r part 1 run until repeat}
sample_computer$run_until_repeat()
sample_computer
```
According to the days text, the accumulator should be 5, and we should have run 7 instructions, which we can see is the
output from the `sample_computer`.
Now we can run the actual data:
```{r part 1 actual}
actual_computer <- Computer$new(actual)
actual_computer$run_until_repeat()
actual_computer
```
## Part 2
We need to modify our computer slightly. First, we need to introduce a way to see if our computer has
[halted](https://en.wikipedia.org/wiki/Halting_problem), which will be when the pointer has exceeded the length of the
instructions.
Second, we need to be able to flip `nop` to `jmp` and `jmp` to `nop`. There may be a smarter way to figure out which to
flip, but I'm just going to iterate through the initial input and flip one at a time. If the computer halt's then we
have found our solution.
Rather than recreating the `Computer` class, we can use `Computer$set()` to add a `halted` method and a
`run_until_halted_or_repeat` method.
```{r part 2 add methods}
Computer$set("public", "is_halted", function() {
private$ptr > nrow(private$instructions)
})
Computer$set("public", "run_until_halted_or_repeat", function() {
repeat {
if (self$is_halted()) {
break()
}
next_instruction <- private$next_instruction()
if (next_instruction$c > 0) {
break()
}
private$run_instruction(next_instruction$inst,
next_instruction$i)
}
invisible(self)
})
```
Now we need to build a function to take our list of instructions and flip all of the `nop`/`jmp` instructions:
```{r part 2 flip instructions}
flip_instructions <- function(input) {
input %>%
str_detect("^(?!acc)") %>%
which() %>%
map(function(.x) {
ix <- input[[.x]]
str_sub(input[[.x]], 1, 3) <- if(str_sub(input[[.x]], 1, 3) == "nop") {
"jmp"
} else {
"nop"
}
input
})
}
```
Our sample flipped looks like this:
```{r part 2 sample flip}
flip_instructions(sample)
```
We have 4 set's of instructions to test. This isn't the most efficient way of solving this... we could flip each of the
instructions in turn, and run the computer on that. This would allow us to exit early. But I don't think that this will
cause us much issues as our actual input isn't huge (`r length(actual)` instructions).
Now we just need to build a function to iterate over the flipped instructions and run until we find a solution, and
return the results of that computer.
```{r part 2 function}
part_2 <- function(input) {
for (i in flip_instructions(input)) {
computer <- Computer$new(i)
computer$run_until_halted_or_repeat()
if (computer$is_halted()) {
return(list(input = input, computer = computer))
}
}
stop("No solution found!")
}
```
```{r part 2 sample test}
part_2(sample)
```
This is the result that we are expecting, so we can run this for the actual data:
```{r part 2 actual}
part_2(actual)$computer
```
## Extra: update the computer class
Redefining the entire class to include the added methods in part 2, and to redefine the print method
```{r Extre Computer R6 Class}
Computer <- R6Class(
"Computer",
public = list(
initialize = function(instructions) {
private$instructions <- instructions %>%
unglue_data("{inst} {i}", convert = TRUE) %>%
mutate(c = 0)
},
get_accumulator = function() {
private$accumulator
},
get_instructions_count = function() {
sum(private$instructions$c)
},
run_next = function() {
next_instruction <- private$next_instruction()
private$run_instruction(next_instruction$inst,
next_instruction$i)
invisible(self)
},
run_until_repeat = function() {
repeat {
next_instruction <- private$next_instruction()
if (next_instruction$c > 0) {
break()
}
private$run_instruction(next_instruction$inst,
next_instruction$i)
}
invisible(self)
},
is_halted = function() {
private$ptr > nrow(private$instructions)
},
run_until_halted_or_repeat = function() {
repeat {
if (self$is_halted()) {
break()
}
next_instruction <- private$next_instruction()
if (next_instruction$c > 0) {
break()
}
private$run_instruction(next_instruction$inst,
next_instruction$i)
}
invisible(self)
},
print = function(...) {
cat("Accumulator: ", private$accumulator, "\n")
cat("Pointer: ", private$ptr, "\n")
cat("Instructions Ran:", self$get_instructions_count(), "\n")
cat("Next Instruction: ")
if (self$is_halted()) {
cat("HALTED\n")
} else {
i <- private$instructions[private$ptr, ]
cat(i$inst,
" ",
sprintf("%+d", i$i),
" (",
i$c,
")\n")
}
}
),
private = list(
accumulator = 0,
ptr = 1,
instructions = list(),
next_instruction = function() {
private$instructions[private$ptr, ]
},
run_instruction = function(inst, i) {
private$instructions[private$ptr, "c"] <- 1 +
private$instructions[private$ptr, "c"]
private$ptr <- private[[inst]](i)
},
# instruction functions: must return the next pointer value
acc = function(i) {
private$accumulator <- private$accumulator + i
private$ptr + 1
},
jmp = function(i) {
private$ptr + i
},
nop = function(i) {
private$ptr + 1
}
)
)
```
---
*Elapsed Time: `r round(Sys.time() - START_TIME, 3)`s*