Code
values <- readLines("input.txt")You try to ask why they can’t just use a weather machine (“not powerful enough”) and where they’re even sending you (“the sky”) and why your map looks mostly blank (“you sure ask a lot of questions”) and hang on did you just say the sky (“of course, where do you think snow comes from”) when you realize that the Elves are already loading you into a trebuchet (“please hold still, we need to strap you in”).
As they’re making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
Consider your entire calibration document. What is the sum of all of the calibration values?
Using regular expressions to only keep the numbers, can split out using strsplit and get the first and last digit, and then convert into numbers ans sum.
Your calculation isn’t quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid “digits”.
What is the sum of all of the calibration values?
This was a pain as it wasn’t clear that overlapping numbers count as both numbers (originally I had used sub to replace the first number).
Using the regular expression to replace the first letter of the word with the number to make it easier to find all the number words in the string. (?<=.{2}) means that there are two characters to look behind before considering the expression. substr would just find the first part of the letter.
y <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
values <- sapply(values, USE.NAMES = FALSE, \(x) {
p <- regexpr(paste(y, collapse = "|"), x)
while (p > 0) {
number <- substr(x, p, p + attr(p, "match.length") - 1)
x <- sub(
paste0("(?<=.{", p - 1, "})", substr(number, 1, 1)),
match(number, y),
x,
perl = TRUE
)
p <- regexpr(paste(y, collapse = "|"), x)
}
x
})
gsub("[^0-9]", "", values) |>
strsplit("") |>
sapply(\(x) as.numeric(paste0(head(x, 1L), tail(x, 1L)))) |>
sum()[1] 54019
This could be improved to just replace the word except the last character, as the most overlap is the one character.
y <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
values <- sapply(values, USE.NAMES = FALSE, \(x) {
p <- regexpr(paste(y, collapse = "|"), x)
while (p > 0) {
number <- substr(x, p, p + attr(p, "match.length") - 1)
x <- sub(
substr(number, 1, nchar(number) - 1),
match(number, y),
x,
perl = TRUE
)
p <- regexpr(paste(y, collapse = "|"), x)
}
x
})
gsub("[^0-9]", "", values) |>
strsplit("") |>
sapply(\(x) as.numeric(paste0(head(x, 1L), tail(x, 1L)))) |>
sum()[1] 54019
---
title: "2023 - Day 1: Trebuchet?!"
format:
html:
code-fold: show
code-tools: true
code-block-bg: true
code-block-border-left: "#AB63BD"
---
> You try to ask why they can't just use a weather machine ("not powerful enough") and where they're even sending you ("the sky") and why your map looks mostly blank ("you sure ask a lot of questions") and hang on did you just say the sky ("of course, where do you think snow comes from") when you realize that the Elves are already loading you into a trebuchet ("please hold still, we need to strap you in").
>
> As they're making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
## Set-Up
```{r setup}
values <- readLines("input.txt")
```
## Part 1
> The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
>
> Consider your entire calibration document. What is the sum of all of the calibration values?
Using regular expressions to only keep the numbers, can split out using `strsplit` and get the first and last digit, and then convert into numbers ans sum.
```{r part_1}
gsub("[^0-9]", "", values) |>
strsplit("") |>
sapply(\(x) paste0(head(x, 1L), tail(x, 1L))) |>
as.numeric() |>
sum()
```
## Part 2
> Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid "digits".
>
> What is the sum of all of the calibration values?
This was a pain as it wasn't clear that overlapping numbers count as both numbers (originally I had used `sub` to replace the first number).
Using the regular expression to replace the first letter of the word with the number to make it easier to find all the number words in the string. `(?<=.{2})` means that there are two characters to look behind before considering the expression. `substr` would just find the first part of the letter.
```{r part_2}
y <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
values <- sapply(values, USE.NAMES = FALSE, \(x) {
p <- regexpr(paste(y, collapse = "|"), x)
while (p > 0) {
number <- substr(x, p, p + attr(p, "match.length") - 1)
x <- sub(
paste0("(?<=.{", p - 1, "})", substr(number, 1, 1)),
match(number, y),
x,
perl = TRUE
)
p <- regexpr(paste(y, collapse = "|"), x)
}
x
})
gsub("[^0-9]", "", values) |>
strsplit("") |>
sapply(\(x) as.numeric(paste0(head(x, 1L), tail(x, 1L)))) |>
sum()
```
This could be improved to just replace the word except the last character, as the most overlap is the one character.
```{r part_2_imp}
y <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
values <- sapply(values, USE.NAMES = FALSE, \(x) {
p <- regexpr(paste(y, collapse = "|"), x)
while (p > 0) {
number <- substr(x, p, p + attr(p, "match.length") - 1)
x <- sub(
substr(number, 1, nchar(number) - 1),
match(number, y),
x,
perl = TRUE
)
p <- regexpr(paste(y, collapse = "|"), x)
}
x
})
gsub("[^0-9]", "", values) |>
strsplit("") |>
sapply(\(x) as.numeric(paste0(head(x, 1L), tail(x, 1L)))) |>
sum()
```