-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDay22.cs
More file actions
161 lines (135 loc) · 5.18 KB
/
Day22.cs
File metadata and controls
161 lines (135 loc) · 5.18 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
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
using System;
using System.Numerics;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2023.Solvers;
public class Day22 : ISolver
{
public readonly record struct Brick(byte X0, byte Y0, short Z0, byte X1, byte Y1, short Z1) : IComparable<Brick>
{
public readonly int CompareTo(Brick other) => Z0.CompareTo(other.Z0);
}
// Assumptions:
// - There are 1500 bricks or less
// - 0 <= x < 10
// - 0 <= y < 10
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
const int width = 10;
const int depth = 10;
var bricksArray = new Brick[1500]; // max number of bricks I support
var brickCount = 1; // leave first brick empty
while (!input.IsEmpty)
bricksArray[brickCount++] = ParseBrick(ref input);
Array.Sort(bricksArray, 1, brickCount - 1);
var xyPlane = new (short Height, short BrickId)[width * depth];
Array.Fill(xyPlane, ((short)0, (short)-1));
var dominators = new short[brickCount];
var brickSupportedCounts = new short[brickCount];
var bricksThatWillCauseFalls = new ulong[(brickCount - 1) / 64 + 1];
var bricksOnTopOf = new short[width * depth];
var part2 = 0;
for (var i = 1; i < brickCount; i++)
{
var brick = bricksArray[i];
var numBricksOnTopOf = 0;
var maxHeight = 1;
for (int y = brick.Y0; y <= brick.Y1; y++)
{
var offset = y * 10;
for (int x = brick.X0; x <= brick.X1; x++)
{
(var height, var brickId) = xyPlane[offset + x];
if (height > maxHeight)
{
bricksOnTopOf[0] = brickId;
numBricksOnTopOf = 1;
maxHeight = height;
}
else if (height == maxHeight && Array.IndexOf(bricksOnTopOf, brickId, 0, numBricksOnTopOf) < 0)
{
bricksOnTopOf[numBricksOnTopOf++] = brickId;
}
}
}
if (numBricksOnTopOf > 0)
{
var dominator = bricksOnTopOf[0];
if (numBricksOnTopOf == 1)
{
bricksThatWillCauseFalls[dominator / 64] |= 1UL << dominator;
}
else
{
while (true)
{
var nextDominator = bricksOnTopOf[0];
while (nextDominator > dominator)
nextDominator = dominators[nextDominator];
bricksOnTopOf[0] = nextDominator;
var allSame = true;
dominator = nextDominator;
for (var j = 1; j < numBricksOnTopOf; j++)
{
nextDominator = bricksOnTopOf[j];
while (nextDominator > dominator)
nextDominator = dominators[nextDominator];
bricksOnTopOf[j] = nextDominator;
if (nextDominator < dominator)
{
allSame = false;
dominator = nextDominator;
break;
}
}
if (allSame)
break;
}
}
dominators[i] = dominator;
var numSupporting = brickSupportedCounts[dominator];
part2 += numSupporting;
brickSupportedCounts[i] = (short)(numSupporting + 1);
}
else
{
brickSupportedCounts[i] = 1;
}
var planeValue = ((short)(maxHeight + brick.Z1 - brick.Z0 + 1), (short)i);
for (int y = brick.Y0; y <= brick.Y1; y++)
{
var offset = y * 10;
for (int x = brick.X0; x <= brick.X1; x++)
xyPlane[offset + x] = planeValue;
}
}
var part1 = brickCount - 1 - CountBits(bricksThatWillCauseFalls);
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static Brick ParseBrick(ref ReadOnlySpan<byte> input)
{
byte c;
var x0 = input[0] - '0';
var y0 = input[2] - '0';
var z0 = input[4] - '0';
var i = 5;
while ((c = input[i++]) != '~')
z0 = 10 * z0 + c - '0';
input = input[i..];
var x1 = input[0] - '0';
var y1 = input[2] - '0';
var z1 = input[4] - '0';
i = 5;
while ((c = input[i++]) != '\n')
z1 = 10 * z1 + c - '0';
input = input[i..];
return new Brick((byte)x0, (byte)y0, (short)z0, (byte)x1, (byte)y1, (short)z1);
}
private static int CountBits(Span<ulong> bitset)
{
var total = 0;
for (var i = 0; i < bitset.Length; i++)
total += BitOperations.PopCount(bitset[i]);
return total;
}
}