Back Original

Advent of Code 2025 Day 3 in C#

DISCLOSURE: If you buy through affiliate links, I may earn a small commission. (disclosures)

In this post we'll walk through my solutions to AOC 2025 Day 3 in C#.

Part 1

I'm trying more for a functionalish approach but some of these calcs just didn't have a great way to chain so I fell back to some imperative.

/* 
Part 1: 
* Battery joltage in a line
Goal: 
* Find the max joltage of each battery
    * Largest number made by two digits in the line
* Sum them

Subproblems: 
* A: How to find max of each battery line
    * 1: Find max number in 0, n-1 then find first version of that, then biggest to right O(3*length*n)
        * Works cause find most important digit first
*/
public static long Day3Part1(string[] lines)
{
    return lines.Aggregate(0, (acc, line) =>
    {
        var largestDigit = line.Substring(0, line.Length - 1).Max();
        var firstIndexOfLargestDigit = line.IndexOf(largestDigit);
        var secondDigit = line.Substring(firstIndexOfLargestDigit + 1, line.Length - firstIndexOfLargestDigit - 1).Max();

        var batteryJoltage = int.Parse($"{largestDigit}{secondDigit}");

        return acc + batteryJoltage;
    });
}

Part 2

/*
Part 2: 
* Same as part 1 but batteries are made of 12 now

Subproblems: 
* A: Finding the battery joltage
    * 1: Keep track of the spaces available, look for max in there
        * if length is 15 and we have 12
        * First: Can be between 0 and 3
*/
public static long Day3Part2(string[] lines)
{
    return lines.Aggregate(0L, (acc, line) =>
    {
        return acc + ParseHighestBatteryJoltage12(line);
    });
}

public static long ParseHighestBatteryJoltage12(string joltageString)
{
    var stringLength = joltageString.Length;
    if(stringLength <= 12)
    {
        return int.Parse(joltageString);
    }

    var spotsAvailable = stringLength - 12 + 1;
    var usedIndex = 0;
    var digits = Enumerable.Range(0, 12)
        .Select(i =>
        {
            var searchString = joltageString
                .Substring(usedIndex, spotsAvailable);
            var bestDigit = searchString.Max();
            var bestDigitIndex = searchString.IndexOf(bestDigit);
            spotsAvailable -= bestDigitIndex;
            usedIndex += bestDigitIndex + 1;

            return bestDigit;
        })
        .ToList();

    return digits
        .Pipe(d => string.Join("", d))
        .Pipe(long.Parse);
}

Next

HAMINIONs Members get access to the full source code (Github Repo) and that of dozens of other example projects.

If you liked this post you might also like: