commit 9b44bf6e9f09fa23967bd471e421b2355c2ed340
parent 98b333587165f6f2b97b5d74e40ed9d36132d43c
Author: Eamon Caddigan <ec@eamoncaddigan.net>
Date: Thu, 4 Dec 2025 12:57:38 -0800
Solve day 03, part 2
Diffstat:
| M | src/day04.R | | | 38 | ++++++++++++++++++++++++++++++++------ |
1 file changed, 32 insertions(+), 6 deletions(-)
diff --git a/src/day04.R b/src/day04.R
@@ -43,22 +43,48 @@ count_neighbors <- function(diagram) {
neighbors
}
+mark_moveable <- function(diagram) {
+ (diagram == "@") & (count_neighbors(diagram) < 4)
+}
+
part1 <- function(input_lines) {
- diagram <- parse_input(input_lines)
- neighbors <- count_neighbors(diagram)
- is_movable <- (diagram == "@") & (neighbors < 4)
- sum(is_movable)
+ input_lines |>
+ parse_input() |>
+ mark_moveable() |>
+ sum()
}
test_part1 <- function() {
stopifnot(part1(test_input) == 13)
}
+count_moveable <- function(diagram) {
+ total_moved <- 0
+ repeat {
+ moveable <- mark_moveable(diagram)
+ moveable_count <- sum(moveable)
+ if (moveable_count == 0) break
+ total_moved <- total_moved + moveable_count
+ diagram[moveable] <- "."
+ }
+ total_moved
+}
+
+part2 <- function(input_lines) {
+ input_lines |>
+ parse_input() |>
+ count_moveable()
+}
+
+test_part2 <- function() {
+ stopifnot(part2(test_input) == 43)
+}
+
main <- function() {
test_part1()
cat("Part 1 solution: ", part1(puzzle_input), "\n")
- # test_part2()
- # cat("Part 2 solution: ", sprintf("%0.0f", part2(puzzle_input)), "\n")
+ test_part2()
+ cat("Part 2 solution: ", part2(puzzle_input), "\n")
}
main()