-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.cs
161 lines (135 loc) · 4.01 KB
/
Day9.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
namespace AdventOfCode2021;
public static class Day9
{
public static void Part1()
{
string[] inputs = InputHelper.GetInput(9);
int width = inputs.Length;
int length = inputs[0].Length;
int[,] heightmap = new int[width, length];
BuildHeightmap(heightmap, inputs);
int totalRiskLevel = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < length; y++)
{
if (IsHeightLowerThanAdjacentNeighbors(heightmap, x, y))
{
totalRiskLevel += heightmap[x, y] + 1;
}
}
}
Console.WriteLine(totalRiskLevel);
}
private static void BuildHeightmap(int[,] heightmap, string[] inputs)
{
int width = inputs.Length;
int length = inputs[0].Length;
for (int x = 0; x < width; x++)
{
string input = inputs[x];
for (int y = 0; y < length; y++)
{
heightmap[x, y] = input[y] - '0';
}
}
}
private static bool IsHeightLowerThanAdjacentNeighbors(int[,] heightmap, int x, int y)
{
int height = heightmap[x, y];
if (x + 1 < heightmap.GetLength(0)
&& heightmap[x + 1, y] <= height)
{
return false;
}
if (x - 1 >= 0
&& heightmap[x - 1, y] <= height)
{
return false;
}
if (y + 1 < heightmap.GetLength(1)
&& heightmap[x, y + 1] <= height)
{
return false;
}
if (y - 1 >= 0
&& heightmap[x, y - 1] <= height)
{
return false;
}
return true;
}
public static void Part2()
{
string[] inputs = InputHelper.GetInput(9);
int width = inputs.Length;
int length = inputs[0].Length;
int[,] heightmap = new int[width, length];
bool[,] flags = new bool[width, length];
BuildHeightmap(heightmap, flags, inputs);
List<int> basinSizes = new List<int>(capacity: 3);
for (int x = 0; x < width; x++)
{
for (int y = 0; y < length; y++)
{
if (flags[x, y])
{
continue;
}
int basinSize = 0;
Queue<(int x, int y)> queue = new Queue<(int x, int y)>(capacity: width);
queue.Enqueue((x, y));
while (queue.TryDequeue(out (int x, int y) pos))
{
if (flags[pos.x, pos.y])
{
continue;
}
flags[pos.x, pos.y] = true;
basinSize++;
EnqueueNeighbors(queue, pos.x, pos.y, width, length);
}
basinSizes.Add(basinSize);
}
}
int productOfLargestThreeSizes = 1;
foreach(int size in basinSizes.OrderByDescending(x => x).Take(3))
{
productOfLargestThreeSizes *= size;
}
Console.WriteLine(productOfLargestThreeSizes);
}
private static void EnqueueNeighbors(Queue<(int x, int y)> queue, int x, int y, int width, int length)
{
if (x - 1 >= 0)
{
queue.Enqueue((x - 1, y));
}
if (x + 1 < width)
{
queue.Enqueue((x + 1, y));
}
if (y - 1 >= 0)
{
queue.Enqueue((x, y - 1));
}
if (y + 1 < length)
{
queue.Enqueue((x, y + 1));
}
}
private static void BuildHeightmap(int[,] heightmap, bool[,] flags, string[] inputs)
{
int width = inputs.Length;
int length = inputs[0].Length;
for (int x = 0; x < width; x++)
{
string input = inputs[x];
for (int y = 0; y < length; y++)
{
heightmap[x, y] = input[y] - '0';
flags[x, y] = heightmap[x, y] == 9;
}
}
}
}