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 e32ec9ee60192eb5a49b48830318adfaedf58cd3
parent 3aa4adb519229ec0d4751a15e3e77467a55d4324
Author: Eamon Caddigan <ec@eamoncaddigan.net>
Date:   Sat,  6 Dec 2025 20:53:12 -0800

Solve day 06, part 1

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

diff --git a/src/day06.R b/src/day06.R @@ -0,0 +1,48 @@ +#!/usr/bin/env Rscript + +source("src/utils.R") + +test_input <- c( + "123 328 51 64 ", + " 45 64 387 23 ", + " 6 98 215 314", + "* + * + " +) +puzzle_input <- aoc_input(6) + +parse_input <- function(input_lines) { + split_lines <- strsplit(trimws(input_lines), "\\s+") + list( + numbers = split_lines[1:(length(split_lines) - 1)] |> + unlist() |> + as.numeric() |> + matrix(nrow = length(split_lines) - 1, byrow = TRUE), + operators = split_lines[[length(split_lines)]] + ) +} + +solve_problems <- function(numbers, operators) { + sum(c( + apply(numbers[, operators == "+"], 2, sum), + apply(numbers[, operators == "*"], 2, prod) + )) +} + +part1 <- function(input_lines) { + input_parsed <- parse_input(input_lines) + solve_problems( + input_parsed$numbers, + input_parsed$operators + ) +} + +test_part1 <- function() { + stopifnot(part1(test_input) == 4277556) +} + +main <- function() { + test_part1() + cat(sprintf("Part 1 solution: %0.0f\n", part1(puzzle_input))) +} + +main()