-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 8.txt
135 lines (125 loc) · 4.82 KB
/
Day 8.txt
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
internal class Program
{
static void Main(string[] args)
{
string[] input = File.ReadAllLines(@"C:/Users/pette/Documents/Aoc8.txt");
//string inp = "30373\r\n25512\r\n65332\r\n33549\r\n35390";
//string[] input = inp.Split("\r\n");
Tree[,] forest = new Tree[input.GetLength(0), input.Count<string>()];
for (int i = 0; i < forest.GetLength(0); i++)
{
for (int j = 0; j < forest.GetLength(1); j++)
{
forest[i, j] = new Tree((int)input[i][j] - 48);
}
}
// Switch outer trees to visible
for (int i = 0; i < forest.GetLength(0); i++)
{
forest[i, 0].visible = true;
forest[i, forest.GetLength(1) - 1].visible = true;
forest[0, i].visible = true;
forest[forest.GetLength(0) - 1, i].visible = true;
}
for (int i = 1; i < forest.GetLength(0) - 1; i++)
{
for (int j = 1; j < forest.GetLength(1) - 1; j++)
{
// Check for higher or equal trees in the west
for (int k = 0; k < forest.GetLength(0); k++)
{
if (k < j)
{
if (forest[i, k].height >= forest[i, j].height)
{
forest[i, j].westFound = true;
}
}
}
// Check for higher or equal trees in the east
for (int k = forest.GetLength(0) - 1; k >= 0; k--)
{
if (k > j)
{
if (forest[i, k].height >= forest[i, j].height)
{
forest[i, j].eastFound = true;
}
}
}
// Check for higher or equal trees in the north
for (int k = 0; k < forest.GetLength(1); k++)
{
if (k < i)
{
if (forest[k, j].height >= forest[i, j].height)
{
forest[i, j].northFound = true;
}
}
}
// Check for higher or equal trees in the south
for (int k = forest.GetLength(1) - 1; k >= 0; k--)
{
if (k > i)
{
if (forest[k, j].height >= forest[i, j].height)
{
forest[i, j].southFound = true;
}
}
}
}
}
// If one of the directions is false, the tree is visible.
for (int i = 0; i < forest.GetLength(0) - 1; i++)
{
for (int j = 0; j < forest.GetLength(1) - 1; j++)
{
if (forest[i, j].eastFound && forest[i, j].westFound && forest[i, j].northFound && forest[i, j].southFound)
{
forest[i, j].visible = false;
}
}
}
int treeCounter = 0;
foreach (Tree tree in forest)
{
if (tree.visible)
{
treeCounter++;
}
}
//for (int i = 0; i < forest.GetLength(0); i++)
//{
// for (int j = 0; j < forest.GetLength(1); j++)
// {
// if (forest[i, j].visible)
// {
// treeCounter++;
// }
// }
//}
//Console.WriteLine($"{forest[1,3].eastFound}\n{forest[1,3].westFound}\n{forest[1,3].northFound}\n{forest[1,3].southFound}");
//for (int i = 0; i < 99; i++)
//{
// Console.WriteLine($"{forest[5,i].visible} north {forest[5,i].northFound} " +
// $"south {forest[5, i].southFound} east {forest[5, i].eastFound} west {forest[5, i].westFound}");
//}
Console.WriteLine(treeCounter);
Console.ReadKey();
}
}
public class Tree
{
public bool visible = true;
public int height;
public bool westFound = false;
public bool eastFound = false;
public bool northFound = false;
public bool southFound = false;
public Tree(int height)
{
this.height = height;
}
}