-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day18.cs
131 lines (117 loc) · 3.48 KB
/
Day18.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
using Draco18s.AoCLib;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventofCode2022 {
internal static class DayEighteen {
internal static long Part1(string input) {
string[] lines = input.Split('\n');
int sum = 0;
//Grid3D lava = new Grid3D();
List<Vector3> cubes = new List<Vector3>();
foreach (string lin in lines)
{
string[] parts = lin.Split(',');
Vector3 p = new Vector3(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]));
cubes.Add(p);
}
int minX = cubes.Min(x => x.x);
int minY = cubes.Min(x => x.y);
int minZ = cubes.Min(x => x.z);
int maxX = cubes.Max(x => x.x);
int maxY = cubes.Max(x => x.y);
int maxZ = cubes.Max(x => x.z);
Grid3D lava = new Grid3D(maxX - minX + 3, maxY - minY + 3, maxZ - minZ + 3, minX-1, minY-1, minZ-1);
foreach(Vector3 p in cubes)
{
lava[p.x, p.y, p.z] = 1;
}
for(int x=lava.MinX; x < lava.MaxX; x++)
{
for (int y = lava.MinY; y < lava.MaxY; y++)
{
for (int z = lava.MinZ; z < lava.MaxZ; z++)
{
sum += CountSides(lava, x, y, z);
}
}
}
return sum;
}
private static int CountSides(Grid3D lava, int cx, int cy, int cz)
{
if (lava[cx, cy, cz, true] == 0) return 0;
int count = 0;
if (lava[cx + 1, cy, cz] == 0) count++;
if (lava[cx - 1, cy, cz] == 0) count++;
if (lava[cx, cy + 1, cz] == 0) count++;
if (lava[cx, cy - 1, cz] == 0) count++;
if (lava[cx, cy, cz + 1] == 0) count++;
if (lava[cx, cy, cz - 1] == 0) count++;
return count;
}
internal static long Part2(string input) {
string[] lines = input.Split('\n');
int sum = 0;
//Grid3D lava = new Grid3D();
List<Vector3> cubes = new List<Vector3>();
foreach (string lin in lines)
{
string[] parts = lin.Split(',');
Vector3 p = new Vector3(int.Parse(parts[0]), int.Parse(parts[1]), int.Parse(parts[2]));
cubes.Add(p);
}
int minX = cubes.Min(x => x.x);
int minY = cubes.Min(x => x.y);
int minZ = cubes.Min(x => x.z);
int maxX = cubes.Max(x => x.x);
int maxY = cubes.Max(x => x.y);
int maxZ = cubes.Max(x => x.z);
Grid3D lava = new Grid3D(maxX - minX + 3, maxY - minY + 3, maxZ - minZ + 3, minX - 1, minY - 1, minZ - 1);
foreach (Vector3 p in cubes)
{
lava[p.x, p.y, p.z] = 1;
}
FloodFill(lava, 0, 0, 0);
for (int x = lava.MinX; x < lava.MaxX; x++)
{
for (int y = lava.MinY; y < lava.MaxY; y++)
{
for (int z = lava.MinZ; z < lava.MaxZ; z++)
{
sum += CountExternalSides(lava, x, y, z);
}
}
}
return sum;
}
private static void FloodFill(Grid3D lava, int x, int y, int z)
{
if (x < lava.MinX || x >= lava.MaxX) return;
if (y < lava.MinY || y >= lava.MaxY) return;
if (z < lava.MinZ || z >= lava.MaxZ) return;
if (lava[x, y, z, true] == 0)
{
lava[x, y, z, true] = 2;
FloodFill(lava, x + 1, y, z);
FloodFill(lava, x - 1, y, z);
FloodFill(lava, x, y + 1, z);
FloodFill(lava, x, y - 1, z);
FloodFill(lava, x, y, z + 1);
FloodFill(lava, x, y, z - 1);
}
}
private static int CountExternalSides(Grid3D lava, int cx, int cy, int cz)
{
if (lava[cx, cy, cz, true] == 0 || lava[cx, cy, cz, true] == 2) return 0;
int count = 0;
if (lava[cx + 1, cy, cz] == 2) count++;
if (lava[cx - 1, cy, cz] == 2) count++;
if (lava[cx, cy + 1, cz] == 2) count++;
if (lava[cx, cy - 1, cz] == 2) count++;
if (lava[cx, cy, cz + 1] == 2) count++;
if (lava[cx, cy, cz - 1] == 2) count++;
return count;
}
}
}