Refactor Day06 to improve readability
This commit is contained in:
parent
1edbfab132
commit
90be974d36
|
|
@ -2,13 +2,12 @@
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using AdvenOfCode.Contracts;
|
using AdvenOfCode.Contracts;
|
||||||
using Coordinate = (int x, int y);
|
|
||||||
|
|
||||||
namespace AoC_2025;
|
namespace AoC_2025;
|
||||||
|
|
||||||
public class Day06 : IPuzzleSolver<long>
|
public class Day06 : IPuzzleSolver<long>
|
||||||
{
|
{
|
||||||
private (string[] operands, string[] numbers) ParsePuzzleInput(string path)
|
private (string[] operands, string[] numberLines) ParsePuzzleInput(string path)
|
||||||
{
|
{
|
||||||
var puzzleInput = File.ReadAllLines(path)
|
var puzzleInput = File.ReadAllLines(path)
|
||||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||||
|
|
@ -22,49 +21,70 @@ public class Day06 : IPuzzleSolver<long>
|
||||||
public long SolvePart1(string pathToPuzzleInput)
|
public long SolvePart1(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var numbers = calculations.numbers
|
var numbers = ParseNumbersByWhitespace(calculations.numberLines);
|
||||||
.Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries))
|
var operands = ParseOperands(calculations.operands);
|
||||||
.Select(line => line.Index())
|
var results = CalculateResults(operands, numbers);
|
||||||
.SelectMany(tuples => tuples.Select(tuple => (tuple.Index, long.Parse(tuple.Item))))
|
|
||||||
.ToArray();
|
|
||||||
var pivotedNumbers = numbers.GroupBy(tuple => tuple.Index);
|
|
||||||
var indexedOperands = calculations.operands
|
|
||||||
.Index()
|
|
||||||
.Select(tuple => (tuple.Index, ParseOperand(tuple.Item)))
|
|
||||||
.ToDictionary();
|
|
||||||
var results = pivotedNumbers.Select(group =>
|
|
||||||
group
|
|
||||||
.Select(tuple => tuple.Item2)
|
|
||||||
.Aggregate(indexedOperands[group.Key].opBaseValue,
|
|
||||||
(acc, number) => indexedOperands[group.Key].operation(acc, number)));
|
|
||||||
return results.Sum();
|
return results.Sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public long SolvePart2(string pathToPuzzleInput)
|
public long SolvePart2(string pathToPuzzleInput)
|
||||||
{
|
{
|
||||||
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
var operands = calculations.operands.Select(ParseOperand).Reverse().ToArray();
|
var operands = ParseOperands(calculations.operands);
|
||||||
List<List<long>> numberColumns = [];
|
var numbers = ParseNumbersByColumn(calculations.numberLines);
|
||||||
List<long> currentColumn = [];
|
var results = CalculateResults(operands, numbers);
|
||||||
for (var i = 1; i <= calculations.numbers[0].Length; i++)
|
|
||||||
{
|
|
||||||
var current = calculations.numbers.Aggregate("", (current1, number) => current1 + number[^i]);
|
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(current))
|
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)
|
||||||
|
{
|
||||||
|
List<long[]> numberColumns = [];
|
||||||
|
List<long> currentColumn = [];
|
||||||
|
for (var i = 1; i <= numberLines[0].Length; i++)
|
||||||
|
{
|
||||||
|
var nextNumber = numberLines.Aggregate("", (acc, numberLine) => acc + numberLine[^i]);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(nextNumber))
|
||||||
{
|
{
|
||||||
currentColumn.Add(long.Parse(current));
|
currentColumn.Add(long.Parse(nextNumber));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
numberColumns.Add(currentColumn);
|
numberColumns.Add(currentColumn.ToArray());
|
||||||
currentColumn = [];
|
currentColumn = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
numberColumns.Add(currentColumn);
|
numberColumns.Add(currentColumn.ToArray());
|
||||||
|
|
||||||
return numberColumns
|
numberColumns.Reverse();
|
||||||
.Select((column, colIndex) => column.Aggregate(operands[colIndex].opBaseValue, (acc, next) => operands[colIndex].operation(acc, next)))
|
return numberColumns.ToArray();
|
||||||
.Sum();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private (Func<long, long, long> operation, long opBaseValue) ParseOperand(string operand) => operand switch
|
private (Func<long, long, long> operation, long opBaseValue) ParseOperand(string operand) => operand switch
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue