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 73884c8c0620108bf6ceb73f153bcaf59eaa8309
parent 1ea5d8f5fc6c45d8830061bf700a9c9a79785550
Author: Eamon Caddigan <ec@eamoncaddigan.net>
Date:   Sat,  6 Dec 2025 22:07:43 -0800

Solve day 07, part 1

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

diff --git a/src/day07.R b/src/day07.R @@ -0,0 +1,74 @@ +#!/usr/bin/env Rscript + +source("src/utils.R") + +test_input <- c( + ".......S.......", + "...............", + ".......^.......", + "...............", + "......^.^......", + "...............", + ".....^.^.^.....", + "...............", + "....^.^...^....", + "...............", + "...^.^...^.^...", + "...............", + "..^...^.....^..", + "...............", + ".^.^.^.^.^...^.", + "..............." +) +puzzle_input <- aoc_input(7) + +parse_input <- function(input_lines) { + input_lines |> + strsplit("", fixed = TRUE) |> + (\(x) do.call(rbind, x))() +} + +lshift <- function(x, fill = vector(mode = class(x), length = 1)) { + c(x[2:length(x)], fill) +} + +rshift <- function(x, fill = vector(mode = class(x), length = 1)) { + c(fill, x[1:(length(x) - 1)]) +} + +trace_beams_one_step <- function(manifold_row, beams_in) { + # This returns the number of splits as well as the updated beams in order to + # solve part 1 + splits <- manifold_row == "^" & beams_in + list( + num_splits = sum(splits), + beams_out = manifold_row == "S" | manifold_row == "." & beams_in | + lshift(splits) | rshift(splits) + ) +} + +count_splits <- function(manifold) { + num_splits <- 0 + beams <- rep(FALSE, ncol(manifold)) + for (i in seq_len(nrow(manifold))) { + res <- trace_beams_one_step(manifold[i, ], beams) + num_splits <- num_splits + res$num_splits + beams <- res$beams_out + } + num_splits +} + +part1 <- function(input_lines) { + count_splits(parse_input(input_lines)) +} + +test_part1 <- function() { + stopifnot(part1(test_input) == 21) +} + +main <- function() { + test_part1() + cat("Part 1 solution: ", part1(puzzle_input), "\n") +} + +main()