-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
50 lines (38 loc) · 1.22 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
using AdventOfCode._2021_09;
using AdventOfCode.Common;
var heatMap = Resources.GetInputFileLines().ParseAsJaggedArray(c => c - '0');
static long Part1(int[][] heatMap)
{
long result = 0;
for (int row = 0; row < heatMap.Length; row++)
{
for (int column = 0; column < heatMap[row].Length; column++)
{
int current = heatMap[row][column];
if (row > 0 && heatMap[row - 1][column] <= current)
{
continue;
}
if (column > 0 && heatMap[row][column - 1] <= current)
{
continue;
}
if (column < heatMap[row].Length - 1 && heatMap[row][column + 1] <= current)
{
continue;
}
if (row < heatMap.Length - 1 && heatMap[row + 1][column] <= current)
{
continue;
}
result += 1 + current;
}
}
return result;
}
static long Part2(int[][] heatMap)
{
return new BasinMap(heatMap).FindBasins().Select(b => (long)b.Size).OrderByDescending(b => b).Take(3).Aggregate((acc, b) => acc * b);
}
Console.WriteLine($"Part 1: {Part1(heatMap)}");
Console.WriteLine($"Part 2: {Part2(heatMap)}");