Last active
December 8, 2025 02:52
-
-
Save ursulams/744cc54e18d764b675151c27edf7366e to your computer and use it in GitHub Desktop.
2025 advent of code day 3
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
| # first star | |
| input <- read.fwf("input.txt", widths = rep(1L, 100L)) | |
| find_joltage <- function(x) { | |
| joltage <- ifelse(which.max(x) == length(x), as.integer(paste0(max(x[1:length(x) - 1]), max(x))), | |
| as.integer(paste0(max(x), max(x[(which.max(x) + 1):length(x)])))) | |
| return(joltage) | |
| } | |
| sum(apply(input, 1, find_joltage)) | |
| # second star | |
| # take the highest possible joltage each time so long as there's enough numbers remaining to the right | |
| find_more_joltage <- function(x, batteries = 12){ | |
| battery <- max(x[1:(length(x) - (batteries - 1))]) | |
| idx <- which.max(x[1:(length(x) - (batteries - 1))]) | |
| if (batteries != 0) { | |
| return(10^(batteries - 1)*battery + find_more_joltage(x[-(1:idx)], (batteries - 1))) | |
| } | |
| else { | |
| return(0) | |
| } | |
| } | |
| format(sum(apply(input, 1, find_more_joltage)), scientific = FALSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment