84 lines
3.1 KiB
C#
84 lines
3.1 KiB
C#
using System.Collections.Generic;
|
|
using System.Data.Common;
|
|
using System.IO;
|
|
using System.Linq;
|
|
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> operation, long opBaseValue)[] operands, long[][] numbers)
|
|
{
|
|
return operands
|
|
.Select((op, i) => numbers[i].Aggregate(op.opBaseValue, (acc, next) => op.operation(acc, next)))
|
|
.ToArray();
|
|
}
|
|
|
|
private (Func<long, long, long> operation, long opBaseValue)[] ParseOperands(string[] operands)
|
|
{
|
|
var parsedOperands = operands
|
|
.Select(ParseOperand)
|
|
.ToArray();
|
|
return parsedOperands;
|
|
}
|
|
|
|
private static long[][] ParseNumbersByWhitespace(string[] numbers)
|
|
{
|
|
var parsedNumbers = numbers
|
|
.Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries))
|
|
.SelectMany(line => line.Select(long.Parse).Index())
|
|
.GroupBy(indexedNumber => indexedNumber.Index)
|
|
.Select(group => group.Select(indexedNumber => indexedNumber.Item).ToArray())
|
|
.ToArray();
|
|
return parsedNumbers;
|
|
}
|
|
|
|
private static long[][] ParseNumbersByColumn(string[] numberLines)
|
|
{
|
|
var (numberColumns, lastColumn) = Enumerable.Range(1, numberLines[0].Length)
|
|
.Select(i => numberLines.Aggregate("", (acc, numberLine) => acc + numberLine[^i]))
|
|
.Aggregate((columns: (List<long[]>)[[]], column: (long[])[]),
|
|
(acc, number) => string.IsNullOrWhiteSpace(number)
|
|
? ([..acc.columns, acc.column], [])
|
|
: (acc.columns, [..acc.column, long.Parse(number)]));
|
|
return [lastColumn, ..Enumerable.Reverse(numberColumns)];
|
|
}
|
|
|
|
private (Func<long, long, long> operation, long opBaseValue) ParseOperand(string operand) => operand switch
|
|
{
|
|
"+" => ((long a, long b) => a + b, 0),
|
|
"*" => ((long a, long b) => a * b, 1),
|
|
_ => throw new ArgumentOutOfRangeException(nameof(operand))
|
|
};
|
|
} |