I try to solve this year’s Advent of Code 2024 riddles in R. This is Day 2 - see https:adventofcode.com/2024/day/2
Part 1: Safe reports #
Lets first read the task:
The unusual data (your puzzle input) consists of many reports, one report per line. Each report is a list of numbers called levels that are separated by spaces. For example:
7 6 4 2 1
1 2 7 8 9
9 7 6 2 1
1 3 2 4 5
8 6 4 4 1
1 3 6 7 9
This example data contains six reports each containing five levels.
The engineers are trying to figure out which reports are safe. The Red-Nosed reactor safety systems can only tolerate levels that are either gradually increasing or gradually decreasing. So, a report only counts as safe if both of the following are true:
- The levels are either all increasing or all decreasing.
- Any two adjacent levels differ by at least one and at most three.
In the example above, the reports can be found safe or unsafe by checking those rules:
7 6 4 2 1
: Safe because the levels are all decreasing by 1 or 2.1 2 7 8 9
: Unsafe because2 7
is an increase of 5.9 7 6 2 1
: Unsafe because6 2
is a decrease of 4.1 3 2 4 5
: Unsafe because1 3
is increasing but3 2
is decreasing.8 6 4 4 1
: Unsafe because4 4
is neither an increase or a decrease.1 3 6 7 9
: Safe because the levels are all increasing by 1, 2, or 3.So, in this example,
2
reports are safe.Analyze the unusual data from the engineers. How many reports are safe?
My input file: 2024-12-02-1-aoc.txt
First, let’s load the data and take a look:
data <- data.table::fread("2024-12-02-1-aoc.txt", header = FALSE, fill = TRUE)
head(data)
V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 |
---|---|---|---|---|---|---|---|
<int> | <int> | <int> | <int> | <int> | <int> | <int> | <int> |
74 | 76 | 78 | 79 | 76 | NA | NA | NA |
38 | 40 | 43 | 44 | 44 | NA | NA | NA |
1 | 2 | 4 | 6 | 8 | 9 | 13 | NA |
65 | 68 | 70 | 72 | 75 | 76 | 81 | NA |
89 | 91 | 92 | 95 | 93 | 94 | NA | NA |
15 | 17 | 16 | 18 | 19 | 17 | NA | NA |
My core idea here was to compute the differences between each element in a row -
luckily, R
provides an inbuilt diff()
function that provides exactly what I
need. Then, we check if all differences are either 1, 2, 3 or -1, -2, -3. Note
that each row has a different number of elements, so we have to omit NA values
via na.omit
when applying the check.
isalltrue <- function(rep) {
return(all(diff(rep) %in% c(1, 2, 3)) |
all(diff(rep) %in% c(-1, -2, -3)))
}
isreport <- function(row) {
return(isalltrue(na.omit(row)))
}
valid.reports <- apply(data, 1, isreport)
table(valid.reports)
valid.reports
FALSE TRUE
743 257
Part 2: Problem Dampener #
Let’s read the task for part 2!
The engineers are surprised by the low number of safe reports until they realize they forgot to tell you about the Problem Dampener.
The Problem Dampener is a reactor-mounted module that lets the reactor safety systems tolerate a single bad level in what would otherwise be a safe report. It’s like the bad level never happened!
Now, the same rules apply as before, except if removing a single level from an unsafe report would make it safe, the report instead counts as safe.
More of the above example’s reports are now safe:
- -
7 6 4 2 1
: Safe without removing any level.- -
1 2 7 8 9
: Unsafe regardless of which level is removed.- -
9 7 6 2 1
: Unsafe regardless of which level is removed.- -
1 3 2 4 5
: Safe by removing the second level,3
.- -
8 6 4 4 1
: Safe by removing the third level,4
.- -
1 3 6 7 9
: Safe without removing any level.Thanks to the Problem Dampener,
4
reports are actually safe!Update your analysis by handling situations where the Problem Dampener can remove a single level from unsafe reports. How many reports are now safe?
So, this task is a good example on how easy it is to get lost in a overly complicated approach. My first approach was basically to somehow check if the general trend was descending or ascending and based on that to remove opposite differences. This was too complicated and in the end I found 4 less safe reports than were needed… (Also note the beautiful print debuggin…)
isalltrue <- function(reptrue1, reptrue2) {
return((all(reptrue1) == TRUE) | (all(reptrue2) == TRUE))
}
iscorrtrue <- function(rep, reptrue) {
rep <- rep[-which.min(reptrue)]
repdiff <- diff(rep)
reptrue1 <- !(repdiff < 1 | repdiff > 3)
reptrue2 <- !(repdiff < -3 | repdiff > -1)
out <- isalltrue(reptrue1, reptrue2)
print(rep)
print(out)
return(out)
}
isreport2 <- function(row) {
print("")
rep <- na.omit(as.numeric(row))
print(c(rep))
repdiff <- c(1, diff(rep))
reptrue1 <- !(repdiff < 1 | repdiff > 3)
repdiff <- c(-1, diff(rep))
reptrue2 <- !(repdiff < -3 | repdiff > -1)
if (isalltrue(reptrue1, reptrue2) == TRUE) {
out <- isalltrue(reptrue1, reptrue2)
print("----- 1 -----")
print(out)
} else if (sum(diff(rep) > 0) > sum(diff(rep) < 0)) {
print("----- 2 -----")
out <- iscorrtrue(rep, reptrue1)
} else if (sum(diff(rep) < 0) > sum(diff(rep) > 0)) {
print("----- 3 -----")
out <- iscorrtrue(rep, reptrue2)
} else {
out <- FALSE
}
if (out == FALSE) {
print("----- 4 -----")
rep <- na.omit(as.numeric(row))[-1]
repdiff <- diff(rep)
reptrue1 <- !(repdiff < 1 | repdiff > 3)
reptrue2 <- !(repdiff < -3 | repdiff > -1)
out <- isalltrue(reptrue1, reptrue2)
print(c(rep))
print(out)
}
return(out)
}
My actual working approach was in the end brute force. I just threw out each element in a row and checked if the report became positive - much simpler and effective!
isreport2 <- function(row) {
rep <- na.omit(row)
out <- isalltrue(rep)
if (out == FALSE) {
for (i in seq_along(rep)) {
# rep <- rep[-i] does not update the variable
# with rep.i, a new variable is assigned (rep.1, rep.2, ...)
rep.i <- rep[-i]
out <- isalltrue(rep.i)
if (out == TRUE) {
break
}
}
}
return(out)
}
sum(apply(data, 1, isreport2))
328