95 lines
3.4 KiB
C#
95 lines
3.4 KiB
C#
using AdvenOfCode.Contracts;
|
|
|
|
namespace AoC_2025;
|
|
|
|
public class Day06 : IPuzzleSolver<long>
|
|
{
|
|
private (string[] operands, string[] numberLines) ParsePuzzleInput(string path)
|
|
{
|
|
var puzzleInput = File.ReadAllLines(path)
|
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
|
.ToArray();
|
|
var operands = puzzleInput[^1]
|
|
.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
var numbers = puzzleInput[..^1];
|
|
return (operands, numbers);
|
|
}
|
|
|
|
public long SolvePart1(string pathToPuzzleInput)
|
|
{
|
|
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
|
var numbers = ParseNumbersByWhitespace(calculations.numberLines);
|
|
var operands = ParseOperands(calculations.operands);
|
|
var results = CalculateResults(operands, numbers);
|
|
return results.Sum();
|
|
}
|
|
|
|
public long SolvePart2(string pathToPuzzleInput)
|
|
{
|
|
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
|
var operands = ParseOperands(calculations.operands);
|
|
var numbers = ParseNumbersByColumn(calculations.numberLines);
|
|
var results = CalculateResults(operands, numbers);
|
|
|
|
return results.Sum();
|
|
}
|
|
|
|
private static long[] CalculateResults(Func<long, long, long>[] operands, long[][] numbers)
|
|
{
|
|
return operands
|
|
.Select((operand, i) => numbers[i].Aggregate((acc, next) => operand(acc, next)))
|
|
.ToArray();
|
|
}
|
|
|
|
private Func<long, long, long>[] ParseOperands(string[] operands)
|
|
{
|
|
var parsedOperands = operands
|
|
.Select(ParseOperand)
|
|
.ToArray();
|
|
return parsedOperands;
|
|
}
|
|
|
|
private static long[][] ParseNumbersByWhitespace(string[] numbers)
|
|
{
|
|
var splittedNUmbers = numbers
|
|
.Select(line => ParseLineByWhitespace(line))
|
|
.ToArray();
|
|
var parsedNumbers = splittedNUmbers
|
|
.Aggregate(Enumerable.Repeat(Array.Empty<long>(), splittedNUmbers[0].Length),
|
|
(acc, line) => CombineLists(acc, line))
|
|
.ToArray();
|
|
return parsedNumbers;
|
|
}
|
|
|
|
private static long[] ParseLineByWhitespace(string line)
|
|
{
|
|
return line.Split(' ', StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(str => long.Parse(str))
|
|
.ToArray();
|
|
}
|
|
|
|
private static IEnumerable<long[]> CombineLists(IEnumerable<IEnumerable<long>> acc, IEnumerable<long> line)
|
|
{
|
|
return acc.Zip(line, (left, right) => (long[])[..left, right]);
|
|
}
|
|
|
|
private static long[][] ParseNumbersByColumn(string[] numberLines)
|
|
{
|
|
var combinedNumbers = numberLines
|
|
.Aggregate(Enumerable.Repeat(string.Empty, numberLines[0].Length),
|
|
(acc, line) => acc.Zip(line, (left, right) => left + right));
|
|
var (numberColumns, lastColumn) = combinedNumbers
|
|
.Aggregate((columns: new List<long[]>(), column: Array.Empty<long>()),
|
|
(acc, number) => string.IsNullOrWhiteSpace(number)
|
|
? ([..acc.columns, acc.column], [])
|
|
: (acc.columns, [..acc.column, long.Parse(number)]));
|
|
return [..numberColumns, lastColumn];
|
|
}
|
|
|
|
private Func<long, long, long> ParseOperand(string operand) => operand switch
|
|
{
|
|
"+" => (long a, long b) => a + b,
|
|
"*" => (long a, long b) => a * b,
|
|
_ => throw new ArgumentOutOfRangeException(nameof(operand))
|
|
};
|
|
} |