advent_of_code_2025

My (attempted) solutions to the 2025 Advent of Code
git clone https://git.eamoncaddigan.net/advent_of_code_2025.git
Log | Files | Refs | README

commit 98b333587165f6f2b97b5d74e40ed9d36132d43c
parent ce1530a4675d326b66cadabe566364326c963740
Author: Eamon Caddigan <ec@eamoncaddigan.net>
Date:   Thu,  4 Dec 2025 12:40:00 -0800

Solve day 03, part 1

Diffstat:
Asrc/day04.R | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 64 insertions(+), 0 deletions(-)

diff --git a/src/day04.R b/src/day04.R @@ -0,0 +1,64 @@ +#!/usr/bin/env Rscript + +source("src/utils.R") + +test_input <- c( + "..@@.@@@@.", + "@@@.@.@.@@", + "@@@@@.@.@@", + "@.@@@@..@.", + "@@.@@@@.@@", + ".@@@@@@@.@", + ".@.@.@.@@@", + "@.@@@.@@@@", + ".@@@@@@@@.", + "@.@.@@@.@." +) +puzzle_input <- aoc_input(4) + +parse_input <- function(input_lines) { + input_lines |> + strsplit("", fixed = TRUE) |> + (\(x) matrix(unlist(x), nrow = length(x), byrow = TRUE))() +} + +count_neighbors <- function(diagram) { + nc <- ncol(diagram) + nr <- nrow(diagram) + neighbors <- matrix(0, nrow = nr, ncol = nc) + is_occupied <- ifelse(diagram == "@", 1, 0) + + # shift = -1: 1 to nr-1 |-> 2 to nr-0 + # shift = +1: 2 to nr-0 |-> 1 to nr-1 + # shift = 0: 1 to nr-0 |-> 1 to nr-0 + for (r_shift in (-1:1)) { + for (c_shift in (-1:1)) { + if (r_shift != 0 || c_shift != 0) { + neighbors[(1 + max(0, r_shift)):(nr + min(0, r_shift)), (1 + max(0, c_shift)):(nc + min(0, c_shift))] <- + neighbors[(1 + max(0, r_shift)):(nr + min(0, r_shift)), (1 + max(0, c_shift)):(nc + min(0, c_shift))] + + is_occupied[(1 - min(0, r_shift)):(nr - max(0, r_shift)), (1 - min(0, c_shift)):(nc - max(0, c_shift))] + } + } + } + neighbors +} + +part1 <- function(input_lines) { + diagram <- parse_input(input_lines) + neighbors <- count_neighbors(diagram) + is_movable <- (diagram == "@") & (neighbors < 4) + sum(is_movable) +} + +test_part1 <- function() { + stopifnot(part1(test_input) == 13) +} + +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") +} + +main()