Last active
December 11, 2023 01:40
-
-
Save ursulams/7750cd0f107c9d3c4b0b70c8d8e5f0aa to your computer and use it in GitHub Desktop.
2023 advent of code day 6
This file contains 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 | |
puzzle_input <- "Time: 52 94 75 94 | |
Distance: 426 1374 1279 1216" | |
df <- data.frame("times" = type.convert(unlist(strsplit(sub("^.*:\\s+(.*)\\n.*", "\\1", puzzle_input), "\\s+")), as.is = TRUE), | |
"distances" = type.convert(unlist(strsplit(sub(".*Distance:\\s+(.*).*", "\\1", puzzle_input), "\\s+")), as.is = TRUE)) | |
# distance as function of (racing - button) * button with time as constant | |
# 0 = -1*(button^2) +- (racing * button) - distance | |
# get all possible values of b for any given distance | |
get_b <- function(t, d){ | |
b2 <- (-t - sqrt((t^2 - 4 * d))) / -2 | |
b1 <- (-t + sqrt((t^2 - 4 * d))) / -2 | |
values <- as.integer(b2 - b1) | |
} | |
df$buttons <- mapply(get_b, df$times, df$distances) | |
prod(df$buttons) | |
# second star | |
t <- as.numeric(paste(df$times, collapse = "")) | |
d <- as.numeric(paste(df$distance, collapse = "")) | |
prod(mapply(get_b, t,d)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment