Refactor Day08 to update data types, optimize method parameters, and improve test logic
This commit is contained in:
parent
9d8a611f76
commit
1b1c64b103
|
|
@ -4,19 +4,19 @@ namespace AoC_2025.Tests;
|
|||
|
||||
public class Day08Test
|
||||
{
|
||||
private readonly Day08 _sut;
|
||||
private IPuzzleSolver<long> _sut;
|
||||
private static readonly string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
|
||||
private readonly string TestInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Test\day08.txt";
|
||||
private readonly string ProdInputPath = @$"{rootPath}\AoC-PuzzleInputs\2025\Prod\day08.txt";
|
||||
|
||||
public Day08Test()
|
||||
{
|
||||
_sut = new Day08();
|
||||
_sut = new Day08(10);
|
||||
}
|
||||
[Fact]
|
||||
public void Part01_Test_equals_40()
|
||||
{
|
||||
var actual = _sut.SolvePart1(TestInputPath, 10);
|
||||
var actual = _sut.SolvePart1(TestInputPath);
|
||||
|
||||
Assert.Equal(40, actual);
|
||||
}
|
||||
|
|
@ -24,7 +24,9 @@ public class Day08Test
|
|||
[Fact]
|
||||
public void Part01_Prod_equals_42840()
|
||||
{
|
||||
var actual = _sut.SolvePart1(ProdInputPath, 1000);
|
||||
_sut = new Day08(1000);
|
||||
|
||||
var actual = _sut.SolvePart1(ProdInputPath);
|
||||
|
||||
Assert.Equal(42840, actual);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,33 +4,35 @@ using System.Data.Common;
|
|||
using System.IO;
|
||||
using System.Linq;
|
||||
using AdvenOfCode.Contracts;
|
||||
using Coordinate = (int x, int y, int z);
|
||||
using Coordinate = (long x, long y, long z);
|
||||
|
||||
namespace AoC_2025;
|
||||
|
||||
public class Day08
|
||||
public class Day08 : IPuzzleSolver<long>
|
||||
{
|
||||
private readonly int _amountToConnect;
|
||||
public Day08(int amountToConnect)
|
||||
{
|
||||
_amountToConnect = amountToConnect;
|
||||
}
|
||||
private Coordinate[] ParsePuzzleInput(string path)
|
||||
{
|
||||
var puzzleInput = File.ReadAllLines(path)
|
||||
.Where(line => !string.IsNullOrWhiteSpace(line))
|
||||
.Select(line => line.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(str => int.Parse(str)).ToArray())
|
||||
.Select(line => line.Split(',', StringSplitOptions.RemoveEmptyEntries).Select(str => long.Parse(str)).ToArray())
|
||||
.Select(numbers => (numbers[0], numbers[1], numbers[2]))
|
||||
.ToArray();
|
||||
return puzzleInput;
|
||||
}
|
||||
|
||||
public long SolvePart1(string pathToPuzzleInput, int amountToConnect)
|
||||
public long SolvePart1(string pathToPuzzleInput)
|
||||
{
|
||||
var jBoxes = ParsePuzzleInput(pathToPuzzleInput);
|
||||
var jBoxPairs = GetAllCombinationsSortedByDistance(jBoxes);
|
||||
var circuits = CreateCircuits(jBoxes);
|
||||
circuits = CombineCircuits(circuits, jBoxPairs, amountToConnect);
|
||||
return circuits
|
||||
.Select(circuit => circuit.Count)
|
||||
.OrderDescending()
|
||||
.Take(3)
|
||||
.Aggregate((acc, next) => acc * next);
|
||||
circuits = CombineCircuits(circuits, jBoxPairs, _amountToConnect);
|
||||
var checksum = MultiplyLargestCircuitLength(circuits, 3);
|
||||
return checksum;
|
||||
}
|
||||
|
||||
public long SolvePart2(string pathToPuzzleInput)
|
||||
|
|
@ -42,13 +44,13 @@ public class Day08
|
|||
return lastConnected.a.x * lastConnected.b.x;
|
||||
}
|
||||
|
||||
private List<List<Coordinate>> CreateCircuits(Coordinate[] jBoxes)
|
||||
private List<List<Coordinate>> CreateCircuits(IEnumerable<Coordinate> jBoxes)
|
||||
{
|
||||
return jBoxes.Select(p => new List<Coordinate> { p }).ToList();
|
||||
}
|
||||
|
||||
private List<List<Coordinate>> CombineCircuits(List<List<Coordinate>> circuits,
|
||||
(Coordinate boxA, Coordinate boxB)[] jBoxPairs, int amountToConnect)
|
||||
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs, int amountToConnect)
|
||||
{
|
||||
circuits = jBoxPairs
|
||||
.Take(amountToConnect)
|
||||
|
|
@ -57,7 +59,7 @@ public class Day08
|
|||
}
|
||||
|
||||
private (Coordinate a, Coordinate b) LastCombinedConnectAllCircuits(List<List<Coordinate>> circuits,
|
||||
(Coordinate boxA, Coordinate boxB)[] jBoxPairs)
|
||||
IEnumerable<(Coordinate boxA, Coordinate boxB)> jBoxPairs)
|
||||
{
|
||||
foreach (var jBoxPair in jBoxPairs)
|
||||
{
|
||||
|
|
@ -65,12 +67,12 @@ public class Day08
|
|||
if(circuits.Count <= 1)
|
||||
return jBoxPair;
|
||||
}
|
||||
return jBoxPairs[^1];
|
||||
return ((0,0,0), (0,0,0));
|
||||
}
|
||||
|
||||
private double GetDistance(Coordinate a, Coordinate b)
|
||||
private long GetDistance(Coordinate a, Coordinate b)
|
||||
{
|
||||
return Math.Sqrt(Math.Pow(a.x - b.x, 2) + Math.Pow(a.y - b.y, 2) + Math.Pow(a.z - b.z, 2));
|
||||
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z);
|
||||
}
|
||||
|
||||
private List<List<Coordinate>> ConnectJBoxes(Coordinate a, Coordinate b, List<List<Coordinate>> circuits)
|
||||
|
|
@ -88,11 +90,10 @@ public class Day08
|
|||
return circuits;
|
||||
}
|
||||
|
||||
private (Coordinate boxA, Coordinate boxB)[] GetAllCombinationsSortedByDistance(Coordinate[] jBoxes)
|
||||
private IEnumerable<(Coordinate boxA, Coordinate boxB)> GetAllCombinationsSortedByDistance(IEnumerable<Coordinate> jBoxes)
|
||||
{
|
||||
var combinations = Combinations(jBoxes)
|
||||
.OrderBy(x => GetDistance(x.a, x.b))
|
||||
.ToArray();
|
||||
.OrderBy(x => GetDistance(x.a, x.b));
|
||||
return combinations;
|
||||
}
|
||||
|
||||
|
|
@ -106,4 +107,13 @@ public class Day08
|
|||
select (a.Item, b.Item);
|
||||
return pairs;
|
||||
}
|
||||
|
||||
private long MultiplyLargestCircuitLength(List<List<Coordinate>> circuits, int amountMultiply)
|
||||
{
|
||||
return circuits
|
||||
.Select(circuit => (long)circuit.Count)
|
||||
.OrderDescending()
|
||||
.Take(amountMultiply)
|
||||
.Aggregate((acc, next) => acc * next);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue