Advent of Code Day 6: Doing Cephalopods' Math
11 Dec 2025 #csharpOn Day 6 of Advent of Code, we’re helping some cephalopods with their kids’ math homework…while they open a trash compactor.
First, I define a problem and function to solve it. A problem is an operation and some numbers.
static int Perform(Problem p)
=> p.Operation switch
{
Operation.Add => p.Numbers.Sum(),
Operation.Multiply => p.Numbers.Aggregate((a, b) => a*b),
_ => throw new NotImplementedException()
};
abstract record Operation
{
public record Add : Operation;
public record Multiply : Operation;
}
record Problem(Operation Operation, IEnumerable<int> Numbers);
Cephalopods’ math is weird with operations and operands organized vertically, not horizontally. Parsing is the trick part: the input is a 2D array where each column is a problem. Numbers above and operator at the bottom.
I loop through the array of arrays column-wise. The last element becomes the operation, and the rest become the numbers,
static IEnumerable<Problem> Parse(string[][] problems)
{
var parsedProblems = new List<Problem>();
for (int j = 0; j < problems[0].Length; j++)
{
var numbers = Enumerable.Range(0, problems.Length - 1)
.Select(i => int.Parse(problems[i][j]))
.ToList();
Operation op = problems[^1][j] == "+"
? new Operation.Add()
: new Operation.Multiply();
parsedProblems.Add(new Problem(op, numbers));
}
return parsedProblems;
}
With the parsing in place, Day 6 is almost done. Here’s my final solution,
var problems = new[]
{
new[]{ "123", "328", "51", "64" },
new[]{ "45", "64", "387", "23" },
new[]{ "6", "98", "215", "314" },
new[]{ "*", "+", "*", "+" }
};
var total = Parse(problems).Sum(Perform);
Console.WriteLine(total);
Console.ReadKey();
static IEnumerable<Problem> Parse(string[][] problems)
{
var parsedProblems = new List<Problem>();
for (int j = 0; j < problems[0].Length; j++)
{
var numbers = Enumerable.Range(0, problems.Length - 1)
.Select(i => int.Parse(problems[i][j]))
.ToList();
Operation op = problems[^1][j] == "+"
? new Operation.Add()
: new Operation.Multiply();
parsedProblems.Add(new Problem(op, numbers));
}
return parsedProblems;
}
static int Perform(Problem p)
=> p.Operation switch
{
Operation.Add => p.Numbers.Sum(),
Operation.Multiply => p.Numbers.Aggregate((a, b) => a*b),
_ => throw new NotImplementedException()
};
abstract record Operation
{
public record Add : Operation;
public record Multiply : Operation;
}
record Problem(Operation Operation, IEnumerable<int> Numbers);
Two one-off bugs got me this time, but I finally cracked it. The parsing part isn’t that functional in style, but “it works on my machine.” Et voilà!
Advent of Code sharpens your coding skills. But coding is more than typing symbols fast. It’s also about teamwork, collaboration, and many skills I share in my book, Street-Smart Coding: 30 Ways to Get Better at Coding. That’s the roadmap I wish I’d known from day one.