Skip to content

Instantly share code, notes, and snippets.

@ursulams
Created December 12, 2025 23:04
Show Gist options
  • Select an option

  • Save ursulams/effa28b21cdafc3e36328a901d165d03 to your computer and use it in GitHub Desktop.

Select an option

Save ursulams/effa28b21cdafc3e36328a901d165d03 to your computer and use it in GitHub Desktop.
2025 advent of code day 4
# day 4 2025
# first star
input <- do.call(rbind, strsplit(readLines("input.txt"),""))
# calculate distances from each matrix cell to all others
# extract each cell's neighboring values for each element- max distance is one
indices <- which(input == input, arr.ind = TRUE)
d <- apply(as.matrix(dist(indices, "maximum", diag = TRUE)), 1, function(i) input[i == 1])
names(d) <- input
d <- d[grep("@", names(d))]
# get neighbors and filter out those with 4+ roll neighbors
neighbors <- lapply(d, function(x) sum(x == "@"))
length(neighbors[neighbors < 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment