-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent_of_code_2023.R
715 lines (506 loc) · 16.3 KB
/
advent_of_code_2023.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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
# advent of code 2023
# Day 1: Trebuchet?! ------------------------------------------------------
word_numbers <- setNames(object = seq(1,9,1),
nm = c('one', 'two', 'three', 'four',
'five', 'six', 'seven', 'eight', 'nine'))
#' Find calibration value
#'
#' @param data data read with readLines
#'
#' @return numeric vector
#' @export
#'
calibration_value <- function(data) {
sum(
sapply(
X = data,
FUN = function(x) {
s <- unlist(strsplit(x[1], split = ""))
numbers <- which(!s %in% letters)
first <- head(s[numbers], 1)
last <- tail(s[numbers], 1)
as.numeric(paste0(first, last))
}
)
)
}
# test
readLines(con = "~/Projects/advent_of_code_2023/day1-sample.txt") %>%
calibration_value()
# solution
readLines(con = "~/Projects/advent_of_code_2023/day1-task.txt") %>%
calibration_value()
# Day 1: Trebuchet?! - part 2 ---------------------------------------------
#' Calculate sum of calibrated value
#'
#' @param data data read with readLines
#'
#' @return numeric
#' @export
#'
calibration_value_all <- function(data) {
pattern <- '(?=([0-9]|one|two|three|four|five|six|seven|eight|nine))'
data %>%
stringr::str_match_all(pattern = pattern) %>%
purrr::map(\(x) x[,2]) %>%
lapply(., FUN = function(x) ifelse(nchar(x) >= 3, word_numbers[x], x)) %>%
lapply(., FUN = function(x) as.numeric(paste0(head(x, n =1), tail(x, n =1)))) %>%
unlist() %>%
sum()
}
# test
readLines("~/Projects/advent_of_code_2023/day1-sample-2.txt") %>%
calibration_value_all()
# solution
readLines(con = "~/Projects/advent_of_code_2023/day1-task.txt") %>%
calibration_value_all()
# Day 2: Cube Conundrum ---------------------------------------------------
# The Elf would first like to know which games would have been possible
# if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
# 3 red, 5 green, 4 blue
day2_sample <- readLines("~/Projects/advent_of_code_2023/data/day2-sample.txt")
#' Get number of sets in each game
#'
#' @param data
#' @param return_n_sets to return split object or number of sets in each game
#'
#' @return either vector or list
#'
count_sets <- function(data, return_n_sets = TRUE) {
tmp <-
unname(
sapply(data,
function(x) strsplit(gsub(pattern = ".*: ", replacement = "", x = x),
split = ";")
)
)
if(return_n_sets) sapply(tmp, length) else tmp
}
#' Compose matrix
#'
#' @param data input data loaded by readLines()
#' @param set set to choose
#'
#' @return matrix
#'
compose_matrix <- function(data, set) {
# extract games and sets
t <- count_sets(data, return_n_sets = FALSE)
# split elements
t2 <- sapply(t, function(x) strsplit(x[set], split= ","))
t2 <- sapply(t2, function(x) if(isTRUE(is.na(x))) { c("0 blue", "0 green", "0 red")} else x)
# order elements
colors <- c("blue", "red", "green")
t2 <- lapply(t2, function(x) {
if(length(x) < 3) {
missing_value <-
paste0("0 ", colors[! colors %in% gsub(pattern = '[^a-z]', replacement = "", x = x)])
x <- append(x, missing_value)
}
tmp_order <- order(gsub(pattern = '[^a-z]', replacement = "", x = x))
x <- x[tmp_order]
x <- as.numeric(gsub(pattern = '[^0-9]', replacement = "", x = x))
})
matrix(unlist(t2),
ncol = 3,
byrow = TRUE,
dimnames = list(c(), c("blue", "green", "red"))
)[1:length(data), ]
}
#' Sum number of valid games
#'
#' @param input_data input data loaded by readLines()
#' @param blue n blue observations
#' @param green n green observations
#' @param red n red observations
#'
#' @returns list
#'
sum_games <- function(input_data, blue, green, red) {
n_sets_per_game <- count_sets(data = input_data, return_n_sets = T)
#n_sets <- 1:max(n_sets_per_game)
n_games <- 1:length(input_data)
sum_matrix <- matrix(0,
nrow = 1,
ncol = length(input_data),
dimnames = list("sums", c() )
)
for(game in seq_along(n_games)) {
n_sets <- 1:max(n_sets_per_game[game])
for(set in seq_along(n_sets)) {
tmp <- t(compose_matrix(data = input_data, set = n_sets[set]))[,game]
sum_matrix[,game] <- ifelse(all(tmp <= c(blue, green, red)),
sum_matrix[,game] + 1,
sum_matrix[,game] +0)
}
}
out <- list(mtx = sum_matrix, sum = sum(which(sum_matrix == n_sets_per_game)))
return(out)
}
# 14 blue cubes, 13 green cubes, and 12 red cubes
# sample
day2_sample <- readLines("~/Projects/advent_of_code_2023/data/day2-sample.txt")
sum_games(input_data = day2_sample,
blue = 14,
green = 13,
red = 12)
# solution
day2_task <- readLines("~/Projects/advent_of_code_2023/data/day2-task.txt")
sum_games(input_data = day2_task,
blue = 14,
green = 13,
red = 12)
# part 2 ------------------------------------------------------------------
fewest_balls <- function(input_data, blue, green, red) {
n_sets_per_game <- count_sets(data = input_data, return_n_sets = T)
n_games <- 1:length(input_data)
res <- list()
for(game in seq_along(n_games)) {
tmp <- list()
n_sets <- 1:max(n_sets_per_game[game])
for(set in seq_along(n_sets)) {
tmp[[paste(game, set)]] <- t(compose_matrix(data = input_data, set = n_sets[set]))[,game]
}
res[[game]] <- tmp %>% do.call(rbind,.) %>% apply(MARGIN = 2, FUN = max)
}
out <- list(mtx = res, sum = sum(sapply(res, prod)))
return(out)
}
# sample
fewest_balls(input_data = day2_sample,
blue = 14,
green = 13,
red = 12)
# solution
fewest_balls(input_data = day2_task,
blue = 14,
green = 13,
red = 12)
# Day 3: Gear Ratios ------------------------------------------------------
# that was tough one. I did most of the task by myself, but then stuck and search for hints
# still I would like to work on my initial idea with symbol
day3_sample <- readLines("~/Projects/advent_of_code_2023/data/day3-sample.txt")
day3_task <- readLines("~/Projects/advent_of_code_2023/data/day3-task.txt")
neighbor <- function(x, y, x_max = nrow(res_mtx), y_max = ncol(res_mtx)) {
xs <- x + c(-1, 0, 1)
ys <- y + c(-1, 0, 1)
xs <- xs[xs > 0]
ys <- ys[ys > 0]
xs <- xs[xs <= x_max]
ys <- ys[ys <= y_max]
mtx[xs, ys]
}
#data <- day3_task
data <- day3_sample
n_elements <- table(strsplit(data, split = "") %>% sapply(., length))
mtx <-
strsplit(data, split = "") %>%
unlist() %>%
matrix(ncol = n_elements, byrow = T)
digits <- c(as.character(seq(0,9,1)), ".")
numbers <- digits[-length(digits)]
# for(row in 1:nrow(mtx)) {
# for(col in 1:ncol(mtx)) {
#
# if(! mtx[row,col] %in% digits ) {
# mtx[row, col] <- "Symbol"
# }
# }
# }
#
# symbol_locations <- which(mtx == "Symbol", arr.ind = TRUE)
res_mtx <- matrix(FALSE, nrow = ncol(mtx), ncol = ncol(mtx))
for(row in 1:nrow(mtx)) {
for(col in 1:ncol(mtx)) {
res_mtx[row, col] <- ifelse(
mtx[row, col] %in% numbers,
any(!neighbor(row, col) %in% digits),
FALSE
)
}
}
num_len <- data %>% stringr::str_locate_all("\\d+")
res <- c()
for (i in seq_along(data)) {
rows <- num_len[[i]]
for (row in seq_len(nrow(rows))) {
start <- rows[row, 1]
end <- rows[row, 2]
if (any(res_mtx[i, seq(start, end)])) {
res <- c(res, stringr::str_sub(data[i], start, end))
}
}
}
as.numeric(res) %>% sum()
# part 2 ------------------------------------------------------------------
# Day 4: Scratchcards -----------------------------------------------------
day4_sample <- readLines("~/Projects/advent_of_code_2023/data/day4-sample.txt")
day4_task <- readLines("~/Projects/advent_of_code_2023/data/day4-task.txt")
count_points <- function(data) {
#browser()
split_list <- sapply(X = data, strsplit, split = "\\|")
split_list <- lapply(X = split_list, gsub, pattern = "Card [0-9]+:", replacement = "")
names(split_list) <- stringr::str_replace(string = names(split_list),
pattern = ":.*",
replacement = "")
split_list <- sapply(X = split_list, strsplit, split = " ")
split_list <- lapply(X = split_list, stringi::stri_remove_empty)
#split_list <- sapply(split_list, as.numeric)
n_cards <- 1:(length(split_list)/2)
c1 <- seq(1,max(n_cards)*2,2)
c2 <- seq(2,max(n_cards)*2,2)
res <- c()
for(i in n_cards) {
if(i < max(n_cards)) {
res <- append(res, length(intersect(split_list[[c1[i]]], split_list[[c2[i]]])))
}
}
points_total <- c()
n_matches <- c()
for(i in seq_along(res)) {
if(res[i] == 0){
points_total <- append(points_total, 0)
n_matches <- append(n_matches, 0)
} else {
points_total <- append(points_total, (2^(res[i]-1)))
n_matches <- append(n_matches, res[i])
}
}
list("n_matches" = c(n_matches, 0),
"points_total" = c(points_total, 0),
"sum" = sum(points_total),
"total_cards" = max(n_cards))
}
# sample
count_points(data = day4_sample)
count_points(data = day4_task)
# part 2 ------------------------------------------------------------------
res_sample <- count_points(data = day4_sample)
res_task <- count_points(data = day4_task)
m <- res_task$n_matches
# I missed this part and needed to get inspiration from others
cards <- rep(1, res_task$total_cards)
for(i in seq_along(cards)) {
if(m[i] !=0) {
cards[ (i+1):(i + m[i]) ] <- cards[ (i+1):(i + m[i]) ] + cards[i] # and this
}
}
sum(cards)
# Day 6: Wait For It -----------------------------------------------------
# sample
data <- readLines("~/Projects/advent_of_code_2023/data/day6-sample.txt")
# task
data <- readLines("~/Projects/advent_of_code_2023/data/day6-task.txt")
s <- strsplit(data, ":") %>%
lapply(., function(x) {
tmp <- as.numeric(unlist(strsplit(x[-1], " ")))
tmp[!is.na(tmp)]
})
raceTime <- s[[1]]
raceDistance <- s[[2]]
combinations <- c()
for(i in seq_along(raceTime)) {
t <- seq_len(raceTime[i])
sums <- 0
for(j in t) {
remaining_time <- max(t) - j
covered_distance <- j*remaining_time
sums <- sum(append(sums, covered_distance > raceDistance[i]))
}
combinations <- append(combinations, sums)
}
prod(combinations)
# part2 -------------------------------------------------------------------
# sample
data <- readLines("~/Projects/advent_of_code_2023/data/day6-sample.txt")
# task
data <- readLines("~/Projects/advent_of_code_2023/data/day6-task.txt")
s <- strsplit(data, ":") %>%
lapply(., function(x) {
tmp <- as.numeric(unlist(strsplit(x[-1], " ")))
tmp[!is.na(tmp)]
})
raceTime_2 <- as.numeric(paste0(s[[1]], collapse = ""))
raceDistance_2 <- as.numeric(paste0(s[[2]], collapse = ""))
# slow solution
# sums <- 0
#
# for(i in seq_len(raceTime_2)) {
#
# remaining_time <- raceTime_2 - i
# covered_distance <- i * remaining_time
# sums <- sum(append(sums, covered_distance > raceDistance_2))
# }
# much faster
time_intervals <- seq_len(raceTime_2)
combinations <- (raceTime_2 - time_intervals) * time_intervals > raceDistance_2
sum(combinations)
# Day 7: Camel Cards ------------------------------------------------------
# Day 8: Haunted Wasteland -----------------------------------------------
day8_sample <- readLines("~/Projects/advent_of_code_2023/data/day8-sample.txt")
day8_sample_2 <- readLines("~/Projects/advent_of_code_2023/data/day8-sample-2.txt")
day8_taks <- readLines("~/Projects/advent_of_code_2023/data/day8-task.txt")
count_steps <- function(input) {
directions <- unlist(strsplit(input[1], ""))
directions <- ifelse(directions == "L", 1, 2)
nodes <- gsub("[()]", "", input[-c(1,2)])
tmp <-
lapply(nodes, function(x) {
z <- unlist(strsplit(x, "="))
gsub(x = unlist(strsplit(z, ","))," ", "")
})
data <- lapply(tmp, function(x) x[-1])
names(data) <- lapply(tmp, function(x) x[1])
nm <- "AAA"
i <- 1
steps <- 0
while(nm != "ZZZ") {
dir <- directions[i]
nm <- data[[nm]][[dir]]
steps <- steps +1
i <- i+1
if(i > length(directions)) i <- 1
}
steps
}
count_steps(input = day8_sample)
count_steps(input = day8_sample_2)
count_steps(input = day8_taks)
# part 2 ------------------------------------------------------------------
day8_p2 <- readLines("~/Projects/advent_of_code_2023/data/day8-sample-part2.txt")
day8_taks <- readLines("~/Projects/advent_of_code_2023/data/day8-task.txt")
input <- day8_p2
directions <- unlist(strsplit(input[1], ""))
directions <- ifelse(directions == "L", 1, 2)
nodes <- gsub("[()]", "", input[-c(1,2)])
tmp <-
lapply(nodes, function(x) {
z <- unlist(strsplit(x, "="))
gsub(x = unlist(strsplit(z, ","))," ", "")
})
data <- lapply(tmp, function(x) x[-1])
names(data) <- lapply(tmp, function(x) x[1])
# working on sample but not on task data
start_pos <- names(data)[grepl("^..A$", names(data))]
i <- 1
steps <- 0
while(! all(grepl("^..Z$", start_pos))) {
dir <- directions[i]
start_pos <- sapply(X = data[ start_pos ], function(x) x[dir])
steps <- steps +1
i <- i+1
if(i > length(directions)) i <- 1
}
steps
#####
# TO BE CONTINUED
start_pos <- names(data)[grepl("^..A$", names(data))]
out <- list()
for(j in seq_along(start_pos)) {
i <- 1
steps <- 0
res <- c()
while(length(res) < 4) {
dir <- directions[i]
start_pos <- sapply(X = data[ start_pos[j] ], function(x) x[dir])
print(start_pos)
steps <- steps +1
i <- i+1
if(i > length(directions)) i <- 1
if(grepl("^..Z$", start_pos[j])) res <- c(res, steps)
}
out[[j]] <- res
}
steps
# Day 9: Mirage Maintenance -----------------------------------------------
day9_sample <-
readLines("~/Projects/advent_of_code_2023/data/day9-sample.txt") %>%
lapply(., function(x) {
tmp <- unname(strsplit(x, " "))
as.numeric(unlist(tmp))
})
day9_task <-
readLines("~/Projects/advent_of_code_2023/data/day9-task.txt") %>%
lapply(., function(x) {
tmp <- unname(strsplit(x, " "))
as.numeric(unlist(tmp))
})
vec_diff <- function(vec) {
res <- rep(NA, length(vec) -1)
for(i in seq_along(vec)) {
if(i < length(vec)) {
res[i] <- vec[i+1] - vec[i]
}
}
if(all(res == 0)) res <- c(res,0)
res
}
fill_mtx <- function(vec) {
input_len <- length(vec)
extra_row <- vec_diff(vec)
mtx <- matrix(NA, byrow = T, ncol = input_len +1)
mtx <- vec
mtx <- rbind(mtx, c(rep(0, length(vec) - length(extra_row)), extra_row))
i <- 2
j <- 1
while(!all(extra_row == 0)) {
extra_row <- vec_diff(mtx[i,][-c(1:j)])
mtx <- rbind(mtx, c(rep(0, length(vec) - length(extra_row)), extra_row))
i <- i+1
j <- j+1
}
mtx
}
fill_last_col <- function(mtx) {
last_column <- mtx[,ncol(mtx)]
pos <- (nrow(mtx):1)
last_col <- rep(NA, nrow(mtx))
for(i in seq_along(pos)) {
last_col[pos[i]] <- sum(last_column[(length(last_column) -(i -1)): length(last_column)])
}
last_col
}
# finally solution
res <- rep(NA, length(day9_task))
for(i in 1:length(day9_task)) {
tmp <- fill_mtx(vec = day9_task[[i]])
res[i] <- fill_last_col(mtx = tmp)[1]
}
sum(res)
# solution inspired by python user - decided to translate to R
extrapolate <- function(l) {
diffs <- diff(l)
return(tail(l, 1) + if(length(l) > 1) extrapolate(diffs) else 0)
}
res2 <- rep(NA, 200)
for(i in 1:length(day9_task)) {
res2[i] <- extrapolate(day9_task[[i]])
}
# part2 -------------------------------------------------------------------
get_reverse_vec <- function(data) {
mtx <- fill_mtx(data)
vec <- rep(NA, nrow(mtx))
for(i in 1:nrow(mtx)) {
vec[i] <- mtx[i,i]
}
vec
}
fill_rev_col <- function(col) {
pos <- (length(col):1)
rev_col <- rep(NA, length(col))
tmp <- 0
for(i in seq_along(pos)) {
if(i <= length(pos)) {
rev_col[pos[i]] <- col[pos[i]] - tmp
tmp <- rev_col[!is.na(rev_col)][1]
}
}
rev_col
}
# solution part 2
res <- rep(NA, length(day9_task))
for(i in 1:length(day9_task)) {
tmp <- get_reverse_vec(data = day9_task[[i]])
res[i] <- fill_rev_col(col = tmp)[1]
}
sum(res)