-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay08.cs
More file actions
101 lines (87 loc) · 3.25 KB
/
Day08.cs
File metadata and controls
101 lines (87 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Collections.Immutable;
namespace _2025._08
{
public sealed class Day08 : Base
{
public Day08(bool example) : base(example)
{
Day = "08";
}
private static double GetDistance((int X, int Y, int Z) a, (int X, int Y, int Z) 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)
);
}
private object SolvePuzzle(int part)
{
List<HashSet<(int, int, int)>> circuits = [];
var input = ReadInput()
.Select(x =>{
var split = x.Split(',').Select(int.Parse).ToArray();
return (X: split[0], Y: split[1], Z: split[2]);
})
.ToImmutableArray();
var junctionBoxDistances = input
.Enumerate()
.SelectMany(junctionBox =>
input
.Skip(junctionBox.index+1)
.Select(junctionBox2 =>
(
Distance: GetDistance(junctionBox.item, junctionBox2),
JunctionBox: junctionBox.item,
JunctionBox2: junctionBox2
)
)
)
.ToImmutableSortedSet()
.Take(part == 1 ? (Example ? 10 : 1000) : int.MaxValue);
foreach(var currConnection in junctionBoxDistances)
{
var filtered = circuits
.Select((circuit, index) => new { circuit, index })
.Where(x =>
x.circuit.Any(y => y == currConnection.JunctionBox || y == currConnection.JunctionBox2 )
)
.OrderByDescending(x => x.index)
.ToList();
if (filtered.Count == 0)
{
circuits.Add([currConnection.JunctionBox, currConnection.JunctionBox2]);
continue;
}
filtered[0].circuit.Add(currConnection.JunctionBox);
filtered[0].circuit.Add(currConnection.JunctionBox2);
for(int i = filtered.Count - 1; i >= 1; i--)
{
filtered[0].circuit.UnionWith(filtered[i].circuit);
circuits.RemoveAt(filtered[i].index);
}
if (part == 1)
{
continue;
}
if (filtered[0].circuit.Count == input.Length)
{
return currConnection.JunctionBox.X * currConnection.JunctionBox2.X;
}
}
return circuits
.Select(x => x.Count)
.OrderByDescending(x => x)
.Take(3)
.Aggregate((x, y) => x * y);
}
public override object PartOne()
{
return SolvePuzzle(1);
}
public override object PartTwo()
{
return SolvePuzzle(2);
}
}
}