-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday04.R
51 lines (45 loc) · 1.28 KB
/
day04.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
library(tidyverse)
# read the data
input <- read_lines("data/2024/day04.txt") |>
map(~ str_split(.x, "")[[1]]) |>
do.call(what = rbind)
# part 1 solution
# helper function
extract_sequences <- function(grid) {
rows <- nrow(grid)
cols <- ncol(grid)
# horizontal & vertical sequences
sequences <- list(
horizontal = map(1:rows, ~ paste(grid[.x, ], collapse = "")),
horizontal_rev = map(1:rows, ~ paste(rev(grid[.x, ]), collapse = "")),
vertical = map(1:cols, ~ paste(grid[, .x], collapse = "")),
vertical_rev = map(1:cols, ~ paste(rev(grid[, .x]), collapse = ""))
)
# diagonal sequences
diagonals <- list(
diag1 = map(
-(rows - 1):(cols - 1),
~ paste(grid[row(grid) - col(grid) == .x], collapse = "")
),
diag1_rev = map(
-(rows - 1):(cols - 1),
~ paste(rev(grid[row(grid) - col(grid) == .x]), collapse = "")
),
diag2 = map(
2:(rows + cols),
~ paste(grid[row(grid) + col(grid) == .x], collapse = "")
),
diag2_rev = map(
2:(rows + cols),
~ paste(rev(grid[row(grid) + col(grid) == .x]), collapse = "")
)
)
c(sequences, diagonals) |>
unlist() |>
discard(~ .x == "")
}
# how many times does an X-MAS appear?
input |>
extract_sequences() |>
str_count("XMAS") |>
sum()