-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
73 lines (64 loc) · 1.71 KB
/
Program.cs
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
using AdventOfCode._2021_22;
using AdventOfCode.Common;
var cuboids = Resources.GetInputFileLines()
.Select(line => line.Split(" "))
.Select(parts =>
( parts[0] == "on",
parts[1]
.Split(",")
.Select(d => d.Substring(2))
.Select(d => d.Split("..").Select(int.Parse).ToArray()).ToArray()
))
.Select(parts =>
(
IsOn: parts.Item1,
Cube: new Cuboid(
new(parts.Item2[0][0], parts.Item2[0][1]),
new(parts.Item2[1][0], parts.Item2[1][1]),
new(parts.Item2[2][0], parts.Item2[2][1]))
))
.ToList();
var allCuboids = new List<(bool IsOn, Cuboid Cube)>();
foreach (var cuboid in cuboids)
{
var count = allCuboids.Count;
for (var i = 0; i < count; i++)
{
var otherCuboid = allCuboids[i];
if (cuboid.Cube.TryIntersect(otherCuboid.Cube, out Cuboid? intersection))
{
allCuboids.Add((!otherCuboid.IsOn, intersection));
}
}
if (cuboid.IsOn)
{
allCuboids.Add(cuboid);
}
}
ulong initializationVolume = 0;
ulong volume = 0;
var limitation = new Cuboid(new Dim(-50, 50), new Dim(-50, 50), new Dim(-50, 50));
foreach (var cuboid in allCuboids)
{
if (cuboid.Cube.TryIntersect(limitation, out Cuboid? intersection))
{
if (cuboid.IsOn)
{
initializationVolume += intersection.Volume;
}
else
{
initializationVolume -= intersection.Volume;
}
}
if (cuboid.IsOn)
{
volume += cuboid.Cube.Volume;
}
else
{
volume -= cuboid.Cube.Volume;
}
}
Console.WriteLine($"Part 1: {initializationVolume}");
Console.WriteLine($"Part 2: {volume}");