no idea what i'm doing
This commit is contained in:
@@ -9,49 +9,42 @@ namespace AdventOfCode.Problems.AOC2025.Day8;
|
|||||||
[ProblemInfo(2025, 8, "Playground")]
|
[ProblemInfo(2025, 8, "Playground")]
|
||||||
internal class Playground : Problem<long, long>
|
internal class Playground : Problem<long, long>
|
||||||
{
|
{
|
||||||
private Vec3<int>[] _boxPositions= [];
|
private Vec3i[] _boxPositions= [];
|
||||||
|
|
||||||
public override void CalculatePart1()
|
public override void CalculatePart1()
|
||||||
{
|
{
|
||||||
var boxes = _boxPositions.Select((p, i) => new JunctionBox(i, p, [])).ToArray();
|
var networks = new List<Network>();
|
||||||
var closest = GetClosestPairs(_boxPositions, 1000);
|
var closest = GetClosestPairs(_boxPositions, 1000);
|
||||||
foreach (var (a, b)in closest)
|
foreach (var (a, b)in closest)
|
||||||
{
|
{
|
||||||
var boxA = boxes.First(box => box.Pos == a);
|
var existingNetworkA = networks.FirstOrDefault(n => n.Members.Contains(a));
|
||||||
var boxB = boxes.First(box => box.Pos == b);
|
var existingNetworkB = networks.FirstOrDefault(n => n.Members.Contains(b));
|
||||||
boxA.Connections.Add(boxB.Id);
|
if ((existingNetworkA != null && existingNetworkB == null))
|
||||||
boxB.Connections.Add(boxA.Id);
|
existingNetworkA.AddConnection(a, b);
|
||||||
|
else if (existingNetworkB != null && existingNetworkA == null)
|
||||||
|
existingNetworkB.AddConnection(a, b);
|
||||||
|
else if (existingNetworkA != null && existingNetworkB != null && existingNetworkA != existingNetworkB)
|
||||||
|
{
|
||||||
|
existingNetworkA.AddConnection(a, b);
|
||||||
|
existingNetworkA.MergeWith(existingNetworkB);
|
||||||
|
networks.Remove(existingNetworkB);
|
||||||
|
}
|
||||||
|
else if(existingNetworkA == null && existingNetworkB == null)
|
||||||
|
{
|
||||||
|
var newNetwork = new Network().AddConnection(a, b);
|
||||||
|
networks.Add(newNetwork);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var networks = TraceNetworks(boxes);
|
Console.WriteLine($"Networks: {networks.Count}");
|
||||||
Part1 = networks.Select(n => (long)n.Count).OrderDescending().Take(3).Aggregate((a, b) => a * b);
|
Part1 = networks.Select(n => n.Members.Count)
|
||||||
|
.OrderDescending()
|
||||||
|
.Take(3)
|
||||||
|
.Aggregate((a, b) => a * b);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<HashSet<int>> TraceNetworks(JunctionBox[] junctionBoxes)
|
private static List<(Vec3i a , Vec3i b)> GetClosestPairs(Vec3i[] boxes, int count = 10)
|
||||||
{
|
{
|
||||||
var networks = new List<HashSet<int>>();
|
var distances = new Dictionary<(Vec3i a, Vec3i b), double>();
|
||||||
foreach (var box in junctionBoxes)
|
|
||||||
{
|
|
||||||
if (networks.Any(n => n.Contains(box.Id)))
|
|
||||||
continue;
|
|
||||||
var net = new HashSet<int>() { box.Id };
|
|
||||||
TraceJunction(box, junctionBoxes, net);
|
|
||||||
networks.Add(net);
|
|
||||||
}
|
|
||||||
return networks;//.Where(n => n.Count > 0).ToList();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void TraceJunction(JunctionBox box, JunctionBox[] boxes, HashSet<int> result)
|
|
||||||
{
|
|
||||||
foreach (var connection in box.Connections)
|
|
||||||
{
|
|
||||||
if (result.Add(connection))
|
|
||||||
TraceJunction(boxes[connection], boxes, result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<(Vec3<int> a , Vec3<int> b)> GetClosestPairs(Vec3<int>[] boxes, int count = 10)
|
|
||||||
{
|
|
||||||
var distances = new Dictionary<(Vec3<int> a, Vec3<int> b), double>();
|
|
||||||
|
|
||||||
for (int i = 0; i < boxes.Length; i++)
|
for (int i = 0; i < boxes.Length; i++)
|
||||||
{
|
{
|
||||||
@@ -74,11 +67,63 @@ internal class Playground : Problem<long, long>
|
|||||||
|
|
||||||
public override void LoadInput()
|
public override void LoadInput()
|
||||||
{
|
{
|
||||||
_boxPositions = ReadInputLines("input.txt")
|
_boxPositions = ReadInputLines("sample.txt")
|
||||||
.Select(l => l.Split(',').Select(int.Parse))
|
.Select(l => l.Split(',').Select(int.Parse))
|
||||||
.Select(c => new Vec3<int>(c.First(), c.Skip(1).First(), c.Last()))
|
.Select(c => new Vec3i(c.First(), c.Skip(1).First(), c.Last()))
|
||||||
.ToArray();
|
.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private record class JunctionBox(int Id, Vec3<int> Pos, HashSet<int> Connections);
|
private record class JunctionBox(Vec3i Pos, HashSet<Vec3i> Connections);
|
||||||
|
|
||||||
|
private class Network
|
||||||
|
{
|
||||||
|
public HashSet<Vec3i> Members { get; private set; } = [];
|
||||||
|
public List<JunctionBox> Boxes { get; private set; } = [];
|
||||||
|
|
||||||
|
public bool IsConnectedTo(Vec3i pos)
|
||||||
|
{
|
||||||
|
return Members.Contains(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IntersectsWith(Network other)
|
||||||
|
{
|
||||||
|
return Members.Intersect(other.Members).Any();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Network AddConnection(Vec3i a, Vec3i b)
|
||||||
|
{
|
||||||
|
if (Members.Contains(a) && Members.Contains(b))
|
||||||
|
return this;
|
||||||
|
Members.Add(a);
|
||||||
|
Members.Add(b);
|
||||||
|
var boxA = GetOrAddBox(a);
|
||||||
|
var boxB = GetOrAddBox(b);
|
||||||
|
boxA.Connections.Add(b);
|
||||||
|
boxB.Connections.Add(a);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JunctionBox GetOrAddBox(Vec3i pos)
|
||||||
|
{
|
||||||
|
var box = Boxes.FirstOrDefault(box => box.Pos == pos);
|
||||||
|
if(box == null)
|
||||||
|
{
|
||||||
|
box = new JunctionBox(pos, []);
|
||||||
|
Boxes.Add(box);
|
||||||
|
}
|
||||||
|
return box;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Network MergeWith(Network other)
|
||||||
|
{
|
||||||
|
foreach (var box in other.Boxes)
|
||||||
|
{
|
||||||
|
foreach (var connection in box.Connections)
|
||||||
|
{
|
||||||
|
AddConnection(box.Pos, connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
using System.Numerics;
|
global using Vec3i = AdventOfCode.Utils.Models.Vec3<int>;
|
||||||
|
global using Vec2i = AdventOfCode.Utils.Models.Vec2<int>;
|
||||||
|
global using Vec3l = AdventOfCode.Utils.Models.Vec3<long>;
|
||||||
|
global using Vec2l = AdventOfCode.Utils.Models.Vec2<long>;
|
||||||
|
global using Vec3f = AdventOfCode.Utils.Models.Vec3<float>;
|
||||||
|
global using Vec2f = AdventOfCode.Utils.Models.Vec2<float>;
|
||||||
|
using System.Numerics;
|
||||||
|
|
||||||
namespace AdventOfCode.Utils.Models;
|
namespace AdventOfCode.Utils.Models;
|
||||||
|
|
||||||
@@ -65,7 +71,7 @@ public record struct Vec3<T>(T X, T Y, T Z) where T : INumber<T>
|
|||||||
var a = other.X - this.X;
|
var a = other.X - this.X;
|
||||||
var b = other.Y - this.Y;
|
var b = other.Y - this.Y;
|
||||||
var c = other.Z - this.Z;
|
var c = other.Z - this.Z;
|
||||||
return (a * a) + (b * b) + (c * c);
|
return T.Abs((a * a) + (b * b) + (c * c));
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly Vec3<T> Min(Vec3<T> other) => new(T.Min(X, other.X), T.Min(Y, other.Y), T.Min(Z, other.Z));
|
public readonly Vec3<T> Min(Vec3<T> other) => new(T.Min(X, other.X), T.Min(Y, other.Y), T.Min(Z, other.Z));
|
||||||
|
|||||||
Reference in New Issue
Block a user