commit 3aa4adb519229ec0d4751a15e3e77467a55d4324
parent 2dcde482dafa08128e47e430679d868ad4a5361f
Author: Eamon Caddigan <ec@eamoncaddigan.net>
Date: Fri, 5 Dec 2025 14:30:06 -0800
Solve day 05, part 2
Diffstat:
1 file changed, 34 insertions(+), 0 deletions(-)
diff --git a/src/day05.R b/src/day05.R
@@ -66,9 +66,43 @@ test_part1 <- function() {
stopifnot(part1(test_input) == 3)
}
+count_fresh_ids <- function(fresh_ranges) {
+ add_remaining_fresh_ids <- function(row_id, last_fresh_id) {
+ new_ids <- if (fresh_ranges[row_id, 2] <= last_fresh_id) {
+ 0
+ } else if (fresh_ranges[row_id, 1] <= last_fresh_id) {
+ fresh_ranges[row_id, 2] - last_fresh_id
+ } else {
+ fresh_ranges[row_id, 2] - fresh_ranges[row_id, 1] + 1
+ }
+
+ if (row_id == nrow(fresh_ranges)) {
+ new_ids
+ } else {
+ new_ids +
+ add_remaining_fresh_ids(
+ row_id + 1,
+ max(last_fresh_id, fresh_ranges[row_id, 2])
+ )
+ }
+ }
+ add_remaining_fresh_ids(1, 0)
+}
+
+part2 <- function(input_lines) {
+ input_parsed <- parse_input(input_lines)
+ count_fresh_ids(input_parsed$fresh_ranges)
+}
+
+test_part2 <- function() {
+ part2(test_input) == 14
+}
+
main <- function() {
test_part1()
cat("Part 1 solution: ", part1(puzzle_input), "\n")
+ test_part2()
+ cat(sprintf("Part 2 solution: %0.0f\n", part2(puzzle_input)))
}
main()