feat: Add day 8

This commit is contained in:
Roman Spring 2025-12-08 08:29:23 +01:00
parent b37717d05f
commit 96f50957a4
3 changed files with 1185 additions and 0 deletions

View file

@ -0,0 +1,20 @@
162,817,812
57,618,57
906,360,560
592,479,940
352,342,300
466,668,158
542,29,236
431,825,988
739,650,466
52,470,668
216,146,977
819,987,18
117,168,530
805,96,715
346,949,466
970,615,88
941,993,340
862,61,35
984,92,344
425,690,689

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,165 @@
namespace AdventOfCode2025.Solutions;
public class Day8() : Solution(8)
{
public override string SolvePart1()
{
List<JunctionBox> boxes = Input
.Lines
.Select(line => line.Split(','))
.Select(parts => new JunctionBox(long.Parse(parts[0]), long.Parse(parts[1]), long.Parse(parts[2])))
.ToList();
IOrderedEnumerable<(JunctionBox Box1, JunctionBox Box2, long Distance)> allPairs = CreateAllPairs(boxes).OrderBy(pair => pair.Distance);
const int numberOfConnections = 1000;
var countConnections = 0;
foreach (var (box1, box2, _) in allPairs)
{
if (countConnections >= numberOfConnections)
{
break;
}
box1.ConnectTo(box2);
countConnections++;
}
IEnumerable<int> networkSizes = boxes
.Select(b => b.Network)
.Where(n => n is not null)
.Distinct()
.Select(n => n!.Boxes.Count)
.OrderByDescending(size => size);
return networkSizes
.Take(3)
.Aggregate(1, (a, b) => a * b)
.ToString();
}
public override string SolvePart2()
{
List<JunctionBox> boxes = Input
.Lines
.Select(line => line.Split(','))
.Select(parts => new JunctionBox(long.Parse(parts[0]), long.Parse(parts[1]), long.Parse(parts[2])))
.ToList();
IOrderedEnumerable<(JunctionBox Box1, JunctionBox Box2, long Distance)> allPairs = CreateAllPairs(boxes).OrderBy(pair => pair.Distance);
JunctionBox? lastBox1 = null;
JunctionBox? lastBox2 = null;
foreach (var (box1, box2, _) in allPairs)
{
if (!box1.ConnectTo(box2))
{
continue;
}
lastBox1 = box1;
lastBox2 = box2;
var isolatedBoxes = boxes.Count(b => b.Network is null);
var distinctNetworks = boxes
.Select(b => b.Network)
.Where(n => n is not null)
.Distinct()
.Count();
var totalCircuits = isolatedBoxes + distinctNetworks;
if (totalCircuits == 1)
{
break;
}
}
return (lastBox1!.X * lastBox2!.X).ToString();
}
private static IEnumerable<(JunctionBox Box1, JunctionBox Box2, long Distance)> CreateAllPairs(List<JunctionBox> boxes)
{
List<(JunctionBox Box1, JunctionBox Box2, long Distance)> pairs = [];
for (var i = 0; i < boxes.Count; i++)
{
for (var j = i + 1; j < boxes.Count; j++)
{
var box1 = boxes[i];
var box2 = boxes[j];
var distance = box1.DistanceTo(box2);
pairs.Add((box1, box2, distance));
}
}
return pairs;
}
private sealed class Network
{
public readonly List<JunctionBox> Boxes = [];
}
private sealed record JunctionBox(long X, long Y, long Z)
{
public Network? Network { get; set; }
public long DistanceTo(JunctionBox other)
{
var dx = X - other.X;
var dy = Y - other.Y;
var dz = Z - other.Z;
return dx * dx + dy * dy + dz * dz;
}
public bool ConnectTo(JunctionBox other)
{
if (BothNotInNetwork())
{
var newNetwork = new Network();
newNetwork.Boxes.Add(this);
newNetwork.Boxes.Add(other);
Network = newNetwork;
other.Network = newNetwork;
}
else if (OnlyOtherInNetwork())
{
other.Network!.Boxes.Add(this);
Network = other.Network;
}
else if (OnlyThisInNetwork())
{
Network!.Boxes.Add(other);
other.Network = Network;
}
else if (NotInSameNetwork())
{
var target = Network!.Boxes.Count >= other.Network!.Boxes.Count ? Network : other.Network!;
var source = target == Network ? other.Network! : Network;
var sourceBoxes = new List<JunctionBox>(source.Boxes);
source.Boxes.Clear();
foreach (var box in sourceBoxes)
{
box.Network = target;
target.Boxes.Add(box);
}
}
else
{
// Already connected
return false;
}
return true;
bool BothNotInNetwork() => Network is null && other.Network is null;
bool OnlyThisInNetwork() => Network is not null && other.Network is null;
bool OnlyOtherInNetwork() => Network is null && other.Network is not null;
bool BothInNetwork() => Network is not null && other.Network is not null;
bool NotInSameNetwork() => BothInNetwork() && Network != other.Network;
}
}
}