Add Day 06 solution, needs cleanup later
This commit is contained in:
parent
fd417a6e97
commit
47f468ff68
|
|
@ -33,3 +33,9 @@ string day05Path = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day05.txt";
|
||||||
Console.WriteLine(day05.SolvePart1(day05Path));
|
Console.WriteLine(day05.SolvePart1(day05Path));
|
||||||
Console.WriteLine(day05.SolvePart2(day05Path));
|
Console.WriteLine(day05.SolvePart2(day05Path));
|
||||||
Console.WriteLine();
|
Console.WriteLine();
|
||||||
|
|
||||||
|
var day06 = new Day06();
|
||||||
|
string day06Path = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day06.txt";
|
||||||
|
Console.WriteLine(day06.SolvePart1(day06Path));
|
||||||
|
Console.WriteLine(day06.SolvePart2(day06Path));
|
||||||
|
Console.WriteLine();
|
||||||
|
|
@ -0,0 +1,47 @@
|
||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
|
||||||
|
namespace AoC_2025.Tests;
|
||||||
|
|
||||||
|
public class Day06Test
|
||||||
|
{
|
||||||
|
private readonly IPuzzleSolver<long> _sut;
|
||||||
|
private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||||||
|
private readonly string TestInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day06.txt";
|
||||||
|
private readonly string ProdInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day06.txt";
|
||||||
|
|
||||||
|
public Day06Test()
|
||||||
|
{
|
||||||
|
_sut = new Day06();
|
||||||
|
}
|
||||||
|
[Fact]
|
||||||
|
public void Part01_Test_equals_4277556()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart1(TestInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(4277556, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Part01_Prod_equals_6299564383938()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart1(ProdInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(6299564383938, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Part02_Test_equals_3263827()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart2(TestInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(3263827, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Part02_Prod_equals_11950004808442()
|
||||||
|
{
|
||||||
|
var actual = _sut.SolvePart2(ProdInputPath);
|
||||||
|
|
||||||
|
Assert.Equal(11950004808442, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -99,15 +99,14 @@ public class Day05 : IPuzzleSolver<long>
|
||||||
var optimizedRanges = new List<Range>(ranges.Length) { ranges[0] };
|
var optimizedRanges = new List<Range>(ranges.Length) { ranges[0] };
|
||||||
foreach (var range in ranges)
|
foreach (var range in ranges)
|
||||||
{
|
{
|
||||||
var lastRange = optimizedRanges[^1];
|
if (TryCombineRanges(optimizedRanges[^1], range, out var combined))
|
||||||
if (range.from > lastRange.to)
|
|
||||||
{
|
|
||||||
optimizedRanges.Add(range);
|
|
||||||
}
|
|
||||||
else if (TryCombineRanges(lastRange, range, out var combined))
|
|
||||||
{
|
{
|
||||||
optimizedRanges[^1] = combined;
|
optimizedRanges[^1] = combined;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
optimizedRanges.Add(range);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return optimizedRanges;
|
return optimizedRanges;
|
||||||
|
|
@ -115,11 +114,18 @@ public class Day05 : IPuzzleSolver<long>
|
||||||
|
|
||||||
private bool TryCombineRanges(Range first, Range second, out Range combined)
|
private bool TryCombineRanges(Range first, Range second, out Range combined)
|
||||||
{
|
{
|
||||||
if (IsIdInRange(second.from, first) && !IsIdInRange(second.to, first))
|
if (IsIdInRange(second.from, first))
|
||||||
{
|
{
|
||||||
combined = (first.from, second.to);
|
var isEndInRange = IsIdInRange(second.to, first);
|
||||||
|
combined = isEndInRange ? first : (first.from, second.to);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
// if (IsIdInRange(first.from, second))
|
||||||
|
// {
|
||||||
|
// var isEndInRange = IsIdInRange(first.to, second);
|
||||||
|
// combined = isEndInRange ? second : (second.from, first.to);
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
|
||||||
combined = first;
|
combined = first;
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,76 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using AdvenOfCode.Contracts;
|
||||||
|
using Coordinate = (int x, int y);
|
||||||
|
|
||||||
|
namespace AoC_2025;
|
||||||
|
|
||||||
|
public class Day06 : IPuzzleSolver<long>
|
||||||
|
{
|
||||||
|
private (string[] operands, string[] numbers) 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 = calculations.numbers
|
||||||
|
.Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries))
|
||||||
|
.Select(line => line.Index().ToArray())
|
||||||
|
.SelectMany(tuples => tuples.Select(tuple => (tuple.Index, long.Parse(tuple.Item))).ToArray())
|
||||||
|
.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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public long SolvePart2(string pathToPuzzleInput)
|
||||||
|
{
|
||||||
|
var calculations = ParsePuzzleInput(pathToPuzzleInput);
|
||||||
|
var operands = calculations.operands.Select(ParseOperand).Reverse().ToArray();
|
||||||
|
List<List<long>> numberColumns = [];
|
||||||
|
List<long> currentColumn = [];
|
||||||
|
for (var i = 1; i <= calculations.numbers[0].Length; i++)
|
||||||
|
{
|
||||||
|
var current = calculations.numbers.Aggregate("", (current1, number) => current1 + number[^i]);
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(current))
|
||||||
|
{
|
||||||
|
currentColumn.Add(long.Parse(current));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
numberColumns.Add(currentColumn);
|
||||||
|
currentColumn = [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
numberColumns.Add(currentColumn);
|
||||||
|
|
||||||
|
return numberColumns
|
||||||
|
.Select((column, colIndex) => column.Aggregate(operands[colIndex].opBaseValue, (acc, next) => operands[colIndex].operation(acc, next)))
|
||||||
|
.Sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
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))
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue