-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day08.cs
105 lines (86 loc) · 2.84 KB
/
Day08.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
namespace AdventOfCode2022;
internal class Day08
{
private readonly int[,] _input;
public Day08(IEnumerable<string> input)
{
var data = input.Select(line => line.Select(c => (int)char.GetNumericValue(c)).ToArray()).ToArray();
_input = Matrix.Create(data);
}
public int PartOne()
{
var visible = 0;
for (var x = 0; x < _input.Width(); x++)
{
for (var y = 0; y < _input.Height(); y++)
{
if (IsVisible(x, y))
{
visible++;
}
}
}
return visible;
}
public int PartTwo()
{
var maxScore = 0;
for (var x = 0; x < _input.Width(); x++)
{
for (var y = 0; y < _input.Height(); y++)
{
var score = GetScenicScore(x, y);
if (score > maxScore)
{
maxScore = score;
}
}
}
return maxScore;
}
private bool IsVisible(int x, int y)
{
if (y == 0 || y == _input.Height() - 1) return true;
if (x == 0 || x == _input.Width() - 1) return true;
var height = _input[x, y];
var row = _input.GetRow(y);
if (row.Take(x).All(h => h < height)) return true;
if (row.Skip(x + 1).All(h => h < height)) return true;
var col = _input.GetColumn(x);
if (col.Take(y).All(h => h < height)) return true;
if (col.Skip(y + 1).All(h => h < height)) return true;
return false;
}
private int GetScenicScore(int x, int y)
{
var height = _input[x, y];
var row = _input.GetRow(y);
var col = _input.GetColumn(x);
var left = row.Take(x).Reverse().TakeUntilIncluding(h => h >= height);
var right = row.Skip(x + 1).TakeUntilIncluding(h => h >= height);
var up = col.Take(y).Reverse().TakeUntilIncluding(h => h >= height);
var down = col.Skip(y + 1).TakeUntilIncluding(h => h >= height);
return left.Count() * right.Count() * up.Count() * down.Count();
}
}
public class Day08Test
{
private const string InputFile = "Day08.Input.txt";
private const string ExampleFile = "Day08.Example.txt";
[Theory]
[FileData(ExampleFile, FileContents.StringPerLine, 21)]
[FileData(InputFile, FileContents.StringPerLine, 1733)]
public void TestPartOne(IEnumerable<string> input, int expected)
{
var solution = new Day08(input);
Assert.Equal(expected, solution.PartOne());
}
[Theory]
[FileData(ExampleFile, FileContents.StringPerLine, 8)]
[FileData(InputFile, FileContents.StringPerLine, 284648)]
public void TestPartTwo(IEnumerable<string> input, int expected)
{
var solution = new Day08(input);
Assert.Equal(expected, solution.PartTwo());
}
}