Screenshot of the following R code
library(dplyr)
library(aocodeR)
input <- aoc_get_input(
day = 4, year = 2024,
cookie_path = ".cookie.txt"
) |>
readr::read_fwf()
n_cols <- nchar(input[1, "X1", drop = TRUE])
cols <- seq(n_cols)
colnames <- paste0("X", seq(n_cols))
tbl <- input |>
tidyr::separate(X1, colnames, sep = cols, remove = TRUE) |>
as.matrix()
# Part 1 ----
detect_xmas <- function(tbl, loc, dir = c(1L, 0L)) {
idx <- data.frame(
row = loc[1] + seq(3L) * dir[1],
col = loc[2] + seq(3L) * dir[2]
) |>
as.matrix()
x <- try(head(tbl[idx], 3L), silent = TRUE)
if (inherits(x, "try-error") || length(x) != 3L) {
return(FALSE)
}
isTRUE(all.equal(c("X", x), c("X", "M", "A", "S")))
}
locs <- which(tbl == "X", arr.ind = TRUE)
locs <- split(locs, row(locs))
purrr::map(locs, ~ {
loc <- .x
dirs <- list(
c(1, 0), c(0, 1),
c(-1, 0), c(0, -1),
c(1, 1), c(-1, -1),
c(1, -1), c(-1, 1)
)
purrr::map_lgl(
dirs,
~ detect_xmas(tbl, loc = loc, dir = .x)
)
}) |>
unlist() |>
sum()
## PART 2 ----
detect_x_mas <- function(tbl, loc = loc) {
rows <- loc[1] + c(-1, 1)
cols <- loc[2] + c(-1, 1)
grid <- try(
tbl[
seq(rows[1], rows[2]),
seq(cols[1], cols[2])
],
silent = TRUE
)
if (inherits(grid, "try-error") || any(dim(grid) != c(3, 3))) {
return(FALSE)
}
all(
setequal(grid[c(1, 9)], c("M", "S")),
setequal(grid[c(3, 7)], c("M", "S"))
)
}
locs <- which(tbl == "A", arr.ind = TRUE)
locs <- split(locs, row(locs))
purrr::map_lgl(
locs,
~ detect_x_mas(tbl, loc = .x)
) |>
sum()
Screenshot of the following R code:
library(aocodeR)
# -- Process input ----
input <- aoc_get_input(
day = 5, year = 2024,
cookie_path = ".cookie.txt"
) |>
readr::read_lines()
updates <- input[stringr::str_detect(input, ",")] |>
strsplit(split = ",") |>
purrr::map(~ as.integer(.x))
rules <- input[stringr::str_detect(input, "\\|")] |>
strsplit(split = "\\|") |>
purrr::map(~ as.integer(.x))
# PART 1 ----
## -- Functions ----
get_later_pages <- function(page, rules) {
is_page_rule <- purrr::map_lgl(rules, ~ .x[1] == page)
rules[is_page_rule] |>
purrr::map_int(~ .x[2])
}
mem_get_later_pages <- memoise::memoise(get_later_pages)
check_page <- function(i, update, rules) {
page <- update[i]
later_pages <- mem_get_later_pages(page, rules)
!any(update[seq(i - 1)] %in% later_pages)
}
check_update <- function(update, rules) {
for (i in seq_along(update)[-1]) {
if (!check_page(i, update, rules)) {
return(FALSE)
}
}
TRUE
}
get_middle <- function(x) {
x[length(x) %/% 2 + 1]
}
## -- workflow ----
valid_updates <- purrr::map_lgl(updates, ~ check_update(.x, rules))
updates[valid_updates] |>
purrr::map_int(get_middle) |>
sum()
# Part 2 ----
# Functions
get_earlier_pages <- function(page, rules) {
is_page_rule <- purrr::map_lgl(rules, ~ .x[2] == page)
rules[is_page_rule] |>
purrr::map_int(~ .x[1])
}
mem_get_earlier_pages <- memoise::memoise(get_earlier_pages)
reorder_update <- function(update) {
idx <- purrr::map_int(
seq_along(update),
~ page_idx(.x, update, rules)
) |>
order()
update[idx]
}
page_idx <- function(i, update, rules) {
sum(update[-i] %in% mem_get_earlier_pages(update[i], rules)) + 1
}
# Workflow
invalid_updates <- updates[!valid_updates]
purrr::map(invalid_updates, reorder_update) |>
purrr::map_int(get_middle) |>
sum()
Managed to spend some time catching up with
#AdventOfCode
(days 4 & 5) in
#rstats
...longer than I hoped on day 5 but satisfied to finally crack it!
aocoder π¦ for importing puzzles into R can be found here: github.com/annakrystall...
05.12.2024 20:49
π 7
π 0
π¬ 0
π 0
In R, set `options(shiny.devmode = TRUE)` in your .Rprofile or call `shiny::devmode(TRUE)`. In Python, use `shiny run` with the `--dev-mode` flag.
Did you know #Shiny has a developer mode that turns on auto-reloading, an in-app error console and more? #RStats
05.12.2024 18:08
π 50
π 6
π¬ 3
π 0
Gingerbread house
My favourite Xmas tradition and recipe passed down by my American mom is making gingerbread houses!
It's sth I now do with my fiancee's niece, nephew and their cousins...we made 5 this year!! Sure it would warm mom's heart to know β₯οΈ
05.12.2024 08:16
π 1
π 0
π¬ 0
π 0
Dani bay
After being battered by high winds for the last 3 weeks, a calm morning has finally dawned!
27.11.2024 07:39
π 5
π 0
π¬ 0
π 0
Same!
22.11.2024 11:47
π 1
π 0
π¬ 0
π 0
ππ
21.11.2024 16:46
π 1
π 0
π¬ 0
π 0
I'm an #rstats RSE too! And eager to connect with others on here too.
Thanks for this list BTW! It made it much easier to find folks quickly
21.11.2024 16:42
π 4
π 0
π¬ 1
π 0
I would love love love to connect to more Research Software Engineers!
This is quite #rstats heavy currently, but is open to all of any level!
go.bsky.app/4kfWypW
20.11.2024 12:44
π 104
π 37
π¬ 26
π 0