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 d2c86eae09a49a747e13e8f050b8fcbc163a44b2
parent d3860d3b6c58fb2fd8fa8ffc3b2760c6cd32195b
Author: Eamon Caddigan <ec@eamoncaddigan.net>
Date:   Wed,  3 Dec 2025 09:30:59 -0800

Solve day 02, part 1

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

diff --git a/src/day03.R b/src/day03.R @@ -0,0 +1,48 @@ +#!/usr/bin/env Rscript + +source("src/utils.R") + +test_input <- c( + "987654321111111", + "811111111111119", + "234234234234278", + "818181911112111" +) +puzzle_input <- aoc_input(3) + +parse_input <- function(input_lines) { + input_lines |> + strsplit("", fixed = TRUE) |> + lapply(as.numeric) +} + +choose_batteries <- function(battery_bank) { + first_battery_loc <- which.max(battery_bank[1:(length(battery_bank) - 1)]) + second_battery_loc <- first_battery_loc + + which.max(battery_bank[(first_battery_loc + 1):length(battery_bank)]) + c(first_battery_loc, second_battery_loc) +} + +use_batteries <- function(battery_bank, battery_choice) { + battery_bank[battery_choice] |> + paste(collapse = "") |> + as.numeric() +} + +part1 <- function(input_string) { + input_string |> + parse_input() |> + (\(x) mapply(use_batteries, x, lapply(x, choose_batteries)))() |> + sum() +} + +test_part1 <- function() { + stopifnot(part1(test_input) == 357) +} + +main <- function() { + test_part1() + cat("Part 1 solution: ", part1(puzzle_input), "\n") +} + +main()