-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
512 lines (512 loc) · 27.8 KB
/
.Rhistory
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
df
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
df
df <- test_input %>%
parse_input() %>%
filter(val == "sp") %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)], df$pat_sp[df$num == 1 & !is.na(df$num)])
bottom <- unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top))), ~length(.x) == 1))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
# str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle, top, bottom, lower_left), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
df
test_input %>%
parse_input() %>%
filter(val == "out")
test_input %>%
parse_input()
test_input %>%
parse_input() %>%
filter(val == "output")
outs <- test_input %>%
parse_input() %>%
filter(val == "output") %>%
pull(pat)
map2(outs, df$pat_sp, ~str_diff(.x, .y))
i <- 1
i <- 1
str_diff(outs[i], df$pat_sp)
which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)
df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)]
out <- c()
for(i in 1:outs){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
out <- c()
outs
i <- 1
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
i <- 2
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
i <- 3
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
i <- 4
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
out <- c()
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
####PART 2####
part2 <- function(input){
df <- input %>%
parse_input() %>%
filter(val == "sp") %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)], df$pat_sp[df$num == 1 & !is.na(df$num)])
bottom <- unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top))), ~length(.x) == 1))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
outs <- input %>%
parse_input() %>%
filter(val == "output") %>%
pull(pat)
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
return(out)
}
part2(test_input)
####PART 2####
part2 <- function(input){
df <- input %>%
parse_input() %>%
filter(val == "sp") %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)], df$pat_sp[df$num == 1 & !is.na(df$num)])
bottom <- unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top))), ~length(.x) == 1))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
outs <- input %>%
parse_input() %>%
filter(val == "output") %>%
pull(pat)
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
return(paste(out, collapse = ""))
}
part2(test_input)
part2(test_input2)
input <- test_input2
df <- input %>%
parse_input() %>%
filter(val == "sp") %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
df
View(df)
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)], df$pat_sp[df$num == 1 & !is.na(df$num)])
View(df)
df$pat_sp[df$num == 7 & !is.na(df$num)]
df$pat_sp[df$num == 7 & !is.na(df$num)]
df$pat_sp[df$num == 7 & !is.na(df$num)][1]
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top)))
unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1))
df$pat_sp[df$char == 6]
unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1))
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
bottom
paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1]
paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top)
df$pat_sp[df$num == 4 & !is.na(df$num)][1]
map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top)))
View(df)
paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1]
df$pat_sp[df$num == 4 & !is.na(df$num)][1]
df$pat_sp[df$char == 6]
str_diff("abcdeg", "cgeb")
paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top)
df$pat_sp[df$num == 4 & !is.na(df$num)][1]
View(df)
df$pat_sp[df$num == 7 & !is.na(df$num)][1]
df$pat_sp[df$num == 1 & !is.na(df$num)][1]
cgebd
test_input <- readLines("day08/test_input.txt")
test_input2 <- readLines("day08/test_input2.txt")
test_input2
df <- input %>%
parse_input()
df.
df
View(df)
df <- input %>%
parse_input() %>%
filter(val == "sp") %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
df
?>split
?split
lines <- split(df, id)
lines <- split(df, "id")
View(lines)
lines <- split(df, df$id)
doinit <- function(df){
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)][1], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
outs <- input %>%
parse_input() %>%
filter(val == "output") %>%
pull(pat)
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
return(paste(out, collapse = ""))
}
doinit(lines[[1]])
doinit(lines[[2]])
map(lines, ~doinit(.x))
df <- lines[[1]]
df
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)][1], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
df
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df
df$num[df$char == 6 & is.na(df$num)] <- 6
df
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
df
df
df <- input %>%
parse_input() %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
df
lines <- split(df, df$id)
df <- lines[[1]]
df
outs <- df %>%
filter(val == "output") %>%
pull(pat)
outs <- df %>%
filter(val == "output")
df
outs <- df %>%
filter(val == "output") %>%
pull(pat_sp)
outs
df <- df %>%
filter(val == "sp")
df
outs <- df %>%
filter(val == "output") %>%
pull(pat_sp)
df <- df %>%
filter(val == "sp")
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)][1], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
df
outs
outs <- df %>%
filter(val == "output") %>%
pull(pat_sp)
df <- input %>%
parse_input() %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
dat <- input %>%
parse_input() %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
dat <- input %>%
parse_input() %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
lines <- split(dat, dat$id)
df <- lihnes[[1]]
df <- lines[[1]]
df
outs <- df %>%
filter(val == "output") %>%
pull(pat_sp)
outs
df <- df %>%
filter(val == "sp")
df
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)][1], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
dat <- input %>%
parse_input() %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
lines <- split(dat, dat$id)
doinit <- function(df){
outs <- df %>%
filter(val == "output") %>%
pull(pat_sp)
df <- df %>%
filter(val == "sp")
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)][1], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
return(paste(out, collapse = ""))
}
map(lines, ~doinit(.x))
reduce(map(lines, ~doinit(.x)), sum)
reduce(map(lines, ~as.numeric(doinit(.x))), sum)
####PART 2####
part2 <- function(input){
dat <- input %>%
parse_input() %>%
left_join(disp_ref %>% filter(num %in% c(1,4,7,8)), by = "char", suffix = c("_sp", "_ref"))
str_diff <- function(x, y){
if(nchar(x) > nchar(y)){
setdiff(strsplit(x, "")[[1]], strsplit(y, "")[[1]])
} else {
setdiff(strsplit(y, "")[[1]], strsplit(x, "")[[1]])
}
}
lines <- split(dat, dat$id)
doinit <- function(df){
outs <- df %>%
filter(val == "output") %>%
pull(pat_sp)
df <- df %>%
filter(val == "sp")
top <- str_diff(df$pat_sp[df$num == 7 & !is.na(df$num)][1], df$pat_sp[df$num == 1 & !is.na(df$num)][1])
bottom <- unique(unlist(keep(map(df$pat_sp[df$char == 6], ~str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top))), ~length(.x) == 1)))
lower_left <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)][1], paste0(df$pat_sp[df$num == 4 & !is.na(df$num)][1], top, bottom))
middle <- unlist(keep(map(df$pat_sp[df$char == 5], ~str_diff(paste0(df$pat_sp[df$num == 7 & !is.na(df$num)], bottom), .x)), ~length(.x) == 1))
upper_left <- str_diff(paste0(df$pat_sp[df$num == 1 & !is.na(df$num)], middle), df$pat_sp[df$num == 4 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, paste0(df$pat_sp[df$num == 4 & !is.na(df$num)], top, bottom)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 9
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove(df$pat_sp[df$num == 8 & !is.na(df$num)], middle)), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 0
df$num[df$char == 6 & is.na(df$num)] <- 6
upper_right <- str_diff(df$pat_sp[df$num == 8 & !is.na(df$num)], df$pat_sp[df$num == 6 & !is.na(df$num)])
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 5
upd <- which(map_lgl(df$pat_sp, ~identical(str_diff(.x, str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(upper_left, lower_left), collapse = "|"))), character(0))) == TRUE)
df$num[df$pat_sp == df$pat_sp[upd]] <- 3
df$num[df$char == 5 & is.na(df$num)] <- 2
lower_right <- unlist(keep(map(df$pat_sp[df$char == 5 & is.na(df$num)], ~str_diff(str_remove_all(df$pat_sp[df$num == 8 & !is.na(df$num)], paste(c(lower_left, upper_right), collapse = "|")), .x)), ~length(.x) == 1))
out <- c()
for(i in 1:length(outs)){
out <- c(out, df$num[which(map_lgl(df$pat_sp, ~identical(str_diff(.x, outs[i]), character(0))) == TRUE)])
}
return(paste(out, collapse = ""))
}
reduce(map(lines, ~as.numeric(doinit(.x))), sum)
}
part2(test_input)
part2(test_input2)
part2(day08_input)
library("tidyverse")
library("httr")
library("lubridate")
library("RSelenium")
library("jsonlite")
# api_key <- "B3A2B55E-9BFE-11EC-B9BF-42010A800003"
#
# nwlng <- -122.106047
# nwlat <- 37.705143
# selng <- -122.044295
# selat <- 37.667840
#
# url <- paste0("https://api.purpleair.com/v1/sensors?api_key=", api_key, "&fields=pm2.5_24hour&location_type=0&nwlng=", nwlng, "&nwlat=", nwlat, "&selng=", selng, "&selat=", selat)
#
# r <- GET(url)
#
# tt <- content(r, "text")
#
# fromJSON(tt)
upper_ca <- "https://www.purpleair.com/sensorlist?exclude=true&nwlat=42.11169264269327&selat=38.902851909465056&nwlng=-127.21693019415514&selng=-118.05005048050876&sensorsActive2=604800"
#middle_ca <- "https://www.purpleair.com/sensorlist?exclude=true&nwlat=38.52782643027081&selat=34.188375296227846&nwlng=-126.2903320825237&selng=-117.12345236887734&sensorsActive2=604800"
middle_ca1 <- "https://www.purpleair.com/sensorlist?exclude=true&nwlat=38.52782643027081&selat=37.099677828553325&nwlng=-126.2903320825237&selng=-115.72824674915512&sensorsActive2=604800"
middle_ca2 <- "https://www.purpleair.com/sensorlist?exclude=true&nwlat=37.099677828553325&selat=34.476128330100636&nwlng=-126.2903320825237&selng=-115.72824674915512&sensorsActive2=604800"
lower_ca <- "https://www.purpleair.com/sensorlist?exclude=true&nwlat=34.476128330100636&selat=29.688297298600844&nwlng=-122.37039603143214&selng=-113.21157022426017&sensorsActive2=604800"
shell('docker run -d -p 4449:4444 -v C:\\Users\\Carlin\\Documents\\purpleaiR:/home/seluser/Downloads selenium/standalone-chrome')
shell('docker ps')
eCaps <- list(
chromeOptions =
list(prefs = list(
"profile.default_content_settings.popups" = 0L,
"download.prompt_for_download" = FALSE,
"download.default_directory" = "C:\\Users\\Carlin\\Documents\\purpleaiR"
)
)
)
remDr <- remoteDriver(remoteServerAddr = "localhost", port = 4449L, browserName = "chrome", extraCapabilities = eCaps)
remDr$open()
remDr$open()
remDr$navigate(middle_ca2)
start_date <- remDr$findElement(using = "css", "[id = 'startdatepicker']")
start_date$sendKeysToElement(list("1/1/2018"))
end_date <- remDr$findElement(using = "css", "[id = 'enddatepicker']")
end_date$sendKeysToElement(list("1/1/2022"))
avg <- remDr$findElement(using = 'xpath', "//*/option[@value = '1440']")
avg$clickElement()
buttons <- remDr$findElements(using = "xpath", "//button[text()='Download Primary (A)']")
map(buttons[1:100], function(x) {
x$clickElement()
Sys.sleep(240)
})