-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathYear2021Day22.cs
199 lines (166 loc) · 6.39 KB
/
Year2021Day22.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Numerics;
using System.Text.RegularExpressions;
using AdventOfCode.Common;
using JetBrains.Annotations;
namespace AdventOfCode.Solutions._2021._22;
[UsedImplicitly]
public class Year2021Day22 : ISolution
{
public object Part1(IEnumerable<string> input)
{
var grid = new Dictionary<Vector3, bool>();
var regex = new Regex(
@"(?<state>\w+) x=(?<from_x>-?\d+)\.\.(?<to_x>-?\d+),y=(?<from_y>-?\d+)\.\.(?<to_y>-?\d+),z=(?<from_z>-?\d+)\.\.(?<to_z>-?\d+)");
foreach (var line in input)
{
var match = regex.Match(line);
var state = match.Groups["state"].Value == "on";
var fromX = int.Parse(match.Groups["from_x"].Value);
var fromY = int.Parse(match.Groups["from_y"].Value);
var fromZ = int.Parse(match.Groups["from_z"].Value);
var toX = int.Parse(match.Groups["to_x"].Value);
var toY = int.Parse(match.Groups["to_y"].Value);
var toZ = int.Parse(match.Groups["to_z"].Value);
for (var x = fromX; x <= toX; x++)
{
if (x is < -50 or > 50)
continue;
for (var y = fromY; y <= toY; y++)
{
if (y is < -50 or > 50)
continue;
for (var z = fromZ; z <= toZ; z++)
{
if (z is < -50 or > 50)
continue;
grid[new Vector3(x, y, z)] = state;
}
}
}
}
return grid.Count(x => x.Value);
}
public object Part2(IEnumerable<string> input)
{
var regex = new Regex(
@"(?<state>\w+) x=(?<from_x>-?\d+)\.\.(?<to_x>-?\d+),y=(?<from_y>-?\d+)\.\.(?<to_y>-?\d+),z=(?<from_z>-?\d+)\.\.(?<to_z>-?\d+)");
var commands = new List<Command>();
foreach (var line in input)
{
var match = regex.Match(line);
var state = Enum.Parse<Action>(match.Groups["state"].Value, true);
var fromX = int.Parse(match.Groups["from_x"].Value);
var fromY = int.Parse(match.Groups["from_y"].Value);
var fromZ = int.Parse(match.Groups["from_z"].Value);
var toX = int.Parse(match.Groups["to_x"].Value);
var toY = int.Parse(match.Groups["to_y"].Value);
var toZ = int.Parse(match.Groups["to_z"].Value);
var x = new Interval(fromX, toX);
var y = new Interval(fromY, toY);
var z = new Interval(fromZ, toZ);
commands.Add(new Command(state, new Cube(x, y, z)));
}
return ExecuteCommands(commands);
}
long ExecuteCommands(IEnumerable<Command> commands)
{
var regionsTurnedOn = ImmutableList.Create<Cube>();
foreach (var cmd in commands)
{
switch (cmd.Action)
{
case Action.On:
var regionsToEnable = ImmutableList.Create(cmd.Cube);
foreach (var existing in regionsTurnedOn)
{
regionsToEnable = regionsToEnable.SelectMany(a => a.Subtract(existing))
.Where(a => !a.IsEmpty)
.ToImmutableList();
}
regionsTurnedOn = regionsTurnedOn.AddRange(regionsToEnable);
break;
case Action.Off:
regionsTurnedOn = regionsTurnedOn.SelectMany(a => a.Subtract(cmd.Cube))
.Where(a => !a.IsEmpty)
.ToImmutableList();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return regionsTurnedOn.Sum(r => r.Volume);
}
private record struct Interval(long Begin, long End)
{
public static readonly Interval Empty = new(0, -1);
public bool IsEmpty => Size == 0L;
public long Size => Math.Max(0, End - Begin + 1);
public Interval Intersect(Interval other)
{
var newBegin = Math.Max(Begin, other.Begin);
var newEnd = Math.Min(End, other.End);
return newBegin <= newEnd
? new Interval(newBegin, newEnd)
: Empty;
}
public IEnumerable<Interval> Split(Interval other)
{
var intersect = Intersect(other);
if (intersect.IsEmpty)
{
yield return this;
}
else
{
if (intersect.Begin > Begin)
yield return this with { End = intersect.Begin - 1 };
yield return intersect;
if (intersect.End < End)
yield return this with { Begin = intersect.End + 1 };
}
}
}
private record struct Cube(Interval X, Interval Y, Interval Z)
{
public long Volume
=> X.Size * Y.Size * Z.Size;
public bool IsEmpty => X.IsEmpty || Y.IsEmpty || Z.IsEmpty;
private Cube Intersect(Cube other) => new(X.Intersect(other.X), Y.Intersect(other.Y), Z.Intersect(other.Z));
private IEnumerable<Cube> SplitX(Interval interval)
{
var (y, z) = (Y, Z);
return X.Split(interval).Select(x => new Cube(x, y, z));
}
private IEnumerable<Cube> SplitY(Interval interval)
{
var (x, z) = (X, Z);
return Y.Split(interval).Select(y => new Cube(x, y, z));
}
private IEnumerable<Cube> SplitZ(Interval interval)
{
var (x, y) = (X, Y);
return Z.Split(interval).Select(z => new Cube(x, y, z));
}
public IEnumerable<Cube> Subtract(Cube other)
{
var intersection = Intersect(other);
if (intersection.IsEmpty)
return Enumerable.Repeat(this, 1);
if (intersection == this)
return Enumerable.Empty<Cube>();
return SplitX(intersection.X).SelectMany(a => a.SplitY(intersection.Y))
.SelectMany(a => a.SplitZ(intersection.Z))
.Where(a => a != intersection);
}
}
private record Command(Action Action, Cube Cube);
private enum Action
{
On,
Off
}
}