-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday21.R
222 lines (202 loc) · 5.66 KB
/
day21.R
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
# --- Day 21: Springdroid Adventure ---
library(tidyverse)
library(gtools)
source("intcode.R")
options(digits = 22, scipen=999)
dt <- read_lines("day21.txt") %>%
str_split(",", simplify = TRUE) %>%
as.numeric()
# --- Part One ---
ic <- intcode_create(dt) %>%
intcode_run_to_input()
# create a function that takes a list of strings, converts them to ascii and
# adds it to the intcode computer as input
intcode_add_ascii <- function(ic, speed = c("WALK", "RUN"), strings) {
x <- c(strings, paste0(speed, "\n")) %>%
#map_chr(paste, collapse = ",") %>%
paste(collapse = "\n") %>%
asc()
# the intcode_add_input expects arguments to be passed to it by ..., so we
# need to use the lift_dl function from purrr to allow us to pass in a vector
# instead
lift_dl(partial(intcode_add_input, ic))(x[,1])
}
# helper function to solve todays problem. Returns the answer, or shows the
# output and returns NA if it fails to find a solution
run <- function(ic, speed = c("WALK", "RUN"), ...) {
ic <- intcode_add_ascii(ic, speed, list(...)) %>%
intcode_run()
out <- intcode_output(ic)
# if the last output is > 127 it can't be an ascii character
if (out[[length(out)]] > 127) {
solution <- out[[length(out)]]
} else {
cat(chr(out), "\n")
cat("ctr:", ic$cycles, "\n")
solution <- as.numeric(NA)
}
solution
}
# Let's consider scenarios: We assume that all gaps are less than our jump
# distance. The jump distance is equal to D (if we jump when D is false, then
# we will land in the hole.)
#--------------------------------
# Single gap, no island
#--------------------------------
# D.DDDDDDDDDDD
# CC.CCCCCCCCCC
# BBB.BBBBBBBBB
# AAAA.AAAAAAAA
#
# #####.#######
# ..J...x...... = AB_D
#--------------------------------
# D..DDDDDDDDDD
# CC..CCCCCCCCC
# BBB..BBBBBBBB
# AAAA..AAAAAAA
#
# #####..###### = A__D
# ...J...x.....
#--------------------------------
# D...DDDDDDDDDD
# CC...CCCCCCCCC
# BBB...BBBBBBBB
# AAAA...AAAAAAA
#
# #####...###### = ___D
# ....J...x.....
#--------------------------------
# Two gaps, island to hop onto
#--------------------------------
# D.D.D.DDDDDDDD
# CC.C..CCCCCCCC
# BBB.B.BBBBBBBB
# AAAA.A.AAAAAAA
#
# #####.#.###### = AB_D -> _BCD
# ..J...J...x...
#--------------------------------
# D.D..DDDDDDDDD
# CC.C..CCCCCCCC
# BBB.B..BBBBBBB
# AAAA.A..AAAAAA
#
# #####.#..##### = AB_D -> __CD
# ..J...J...x...
#--------------------------------
# D.D...DDDDDDDD
# CC.C...CCCCCCC
# BBB.B...BBBBBB
# AAAA.A...AAAAA
#
# #####.#...#### = AB_D -> ___D
# ..J...J...x...
#--------------------------------
# D..D.DDDDDDDDD
# CC..C.CCCCCCCC
# BBB..B.BBBBBBB
# AAAA..A.AAAAAA
#
# #####..#.##### = A__D -> _BCD
# ...J...J...x..
#--------------------------------
# D..D..DDDDDDDD
# CC..C..CCCCCCC
# BBB..B..BBBBBB
# AAAA..A..AAAAA
#
# #####..#..#### = A__D -> __CD
# ...J...J...x..
#--------------------------------
# In the mapped out scenarios above we always need D TRUE, and we always need at
# least one of A,B,C FALSE.
# We can think of this as:
# - We need D to be true, we are going to jump D squares. If D is false we will
# land in a hole
# - We don't want to jump if there isn't a hole, so if A,B,C are all true then
# there is no immediate hole to jump over
# - If D is true, but either A, B or C are false, then there is a hole, and we
# can jump it, so jump
# That gives us:
# (!A | !B | !C) & D
# which we could write as
# !(A & B & C) & D
#
# On further inspection, we can see that in every scenario mapped out we don't
# need to check B, only C and A are important
run(ic,
"WALK",
"OR A T", # T starts as false, so or with A
#"AND B T", # now and B and T, not actually required
"AND C T", # now and C and T
"NOT T J", # invert T into J
"AND D J") # and D and J
# --- Part Two ---
#---------------------------------------------------
# I..IIIIIIIIIIIIII
# HH..HHHHHHHHHHHHH
# .GG..GGGGGGGGGGGG
# F.FF..FFFFFFFFFFF
# .E.EE..EEEEEEEEEE
# D.D.DD..DDDDDDDDD
# CC.C.CC..CCCCCCCC
# BBB.B.BB..BBBBBBB
# AAAA.A.AA..AAAAAA
#
# #####.#.##..##### _B_DE__HI -> A__DEFGHI
# ....J...J...x.... (new)
# ..J...J...x...... AB_D_FG__ -> _BC_EFGHI [no jump]
#---------------------------------------------------
# ..IIIIIIIIIIIIIII
# H..HHHHHHHHHHHHHH
# .G..GGGGGGGGGGGGG
# F.F..FFFFFFFFFFFF
# .E.E..EEEEEEEEEEE
# D.D.D..DDDDDDDDDD
# CC.C.C..CCCCCCCCC
# BBB.B.B..BBBBBBBB
# AAAA.A.A..AAAAAAA
#
# #####.#.#..###### _B_D__FGHI -> __CDEFGHI
# ....J...J...x....
# ..J...J...x......
#---------------------------------------------------
# II...IIIIIIIIIIII
# .HH...HHHHHHHHHHH
# G.GG...GGGGGGGGGG
# FF.FF...FFFFFFFFF
# .EE.EE...EEEEEEEE
# D.DD.DD...DDDDDDD
# CC.CC.CC...CCCCCC
# BBB.BB.BB...BBBBB
# AAAA.AA.AA...AAAA
#
# #####.##.##...### AB_DE_GH_ -> A_CD___HI -> ___DEFGHI
# ..J...J...J...x......
# The second jump is the same as part one: the only difference we need to check
# is the first part. The third example shows we need B now. The constant pattern
# in the new inputs is that we need either E or H, let's try changing our check
# to
# !(A & B & C) & D & (E | H)
run(ic,
"RUN",
"OR A T", # T starts as false, so or with A
"AND B T", # now and B and T, not actually required
"AND C T", # now and C and T
"NOT T J", # invert T into J
"AND D J", # and D and J
"NOT E T", # set T to NOT E,
"NOT T T", # then set T to NOT T, this will now be E
"OR H T",
"AND T J")
# from reddit, the most minimal example found is below
# (https://www.reddit.com/r/adventofcode/comments/edntkk/2019_day_21_minimal_instructions/fbmcnxh/)
#run(ic,
# "RUN",
# "NOT H J",
# "OR C J",
# "AND B J",
# "AND A J",
# "NOT J J",
# "AND D J")