-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.cs
132 lines (107 loc) · 4.72 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace cs
{
class Day9
{
public static void Part1()
{
List<List<int>> height_map = new List<List<int>>();
List<List<bool>> lowest_truths = new List<List<bool>>();
File.ReadLines(@"../../../../../data/day9.txt").ToList().ForEach(row =>
{
if (row.Trim() != "")
{
height_map.Add(new List<int>());
lowest_truths.Add(new List<bool>());
foreach (char num in row)
{
height_map.Last().Add(num - '0');
lowest_truths.Last().Add(false);
}
}
});
for (int r = 0; r < height_map.Count; r++)
for (int c = 0; c < height_map[r].Count; c++)
{
List<bool> truths = new List<bool>();
if (r >= 1)
truths.Add(height_map[r][c] < height_map[r - 1][c]);
if (r + 1 < height_map.Count)
truths.Add(height_map[r][c] < height_map[r + 1][c]);
if (c >= 1)
truths.Add(height_map[r][c] < height_map[r][c - 1]);
if (c + 1 < height_map[r].Count)
truths.Add(height_map[r][c] < height_map[r][c + 1]);
lowest_truths[r][c] = truths.Contains(false) == false;
}
int result = 0;
for (int r = 0; r < height_map.Count; r++)
for (int c = 0; c < height_map[r].Count; c++)
if (lowest_truths[r][c])
result += height_map[r][c] + 1;
Console.WriteLine("Day: 9 | Part: 1 | Result: " + result);
}
private static int BacktrackBasin(ref List<List<int>> height_map, int r, int c, int counter, ref List<Tuple<int, int>> visited)
{
if (visited.Contains(new Tuple<int, int>(r, c)))
return counter;
visited.Add(new Tuple<int, int>(r, c));
if (height_map[r][c] == 9)
return counter;
counter++;
if (r >= 1)
counter = BacktrackBasin(ref height_map, r - 1, c, counter, ref visited);
if (r + 1 < height_map.Count)
counter = BacktrackBasin(ref height_map, r + 1, c, counter, ref visited);
if (c >= 1)
counter = BacktrackBasin(ref height_map, r, c - 1, counter, ref visited);
if (c + 1 < height_map[r].Count)
counter = BacktrackBasin(ref height_map, r, c + 1, counter, ref visited);
return counter;
}
public static void Part2()
{
List<List<int>> height_map = new List<List<int>>();
List<List<bool>> lowest_truths = new List<List<bool>>();
File.ReadLines(@"../../../../../data/day9.txt").ToList().ForEach(row =>
{
if (row.Trim() != "")
{
height_map.Add(new List<int>());
lowest_truths.Add(new List<bool>());
foreach (char num in row)
{
height_map.Last().Add(num - '0');
lowest_truths.Last().Add(false);
}
}
});
for (int r = 0; r < height_map.Count; r++)
for (int c = 0; c < height_map[r].Count; c++)
{
List<bool> truths = new List<bool>();
if (r >= 1)
truths.Add(height_map[r][c] < height_map[r - 1][c]);
if (r + 1 < height_map.Count)
truths.Add(height_map[r][c] < height_map[r + 1][c]);
if (c >= 1)
truths.Add(height_map[r][c] < height_map[r][c - 1]);
if (c + 1 < height_map[r].Count)
truths.Add(height_map[r][c] < height_map[r][c + 1]);
lowest_truths[r][c] = truths.Contains(false) == false;
}
List<int> basins = new List<int>();
List<Tuple<int, int>> visited = new List<Tuple<int, int>>();
for (int r = 0; r < height_map.Count; r++)
for (int c = 0; c < height_map[r].Count; c++)
if (lowest_truths[r][c])
basins.Add(BacktrackBasin(ref height_map, r, c, 0, ref visited));
basins.Sort();
int result = basins[^1] * basins[^2] * basins[^3];
Console.WriteLine("Day: 9 | Part: 1 | Result: " + result);
}
}
}