Created
December 12, 2025 23:04
-
-
Save ursulams/effa28b21cdafc3e36328a901d165d03 to your computer and use it in GitHub Desktop.
2025 advent of code day 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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