blob: f46fdc6d82b04600342850d2431498440fb0114c (
plain)
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
|
#include <cstddef>
#include <fstream>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
vector<string>
read_file(string_view filename)
{
fstream input{ filename };
vector<string> data;
for ( string line; getline(input, line); ) {
data.emplace_back(line);
}
return data;
}
void
part1(const vector<string>& data)
{
set<tuple<size_t, size_t>> positions;
char max_so_far = 0;
auto update = [&](size_t row, size_t col) {
if ( data[row][col] > max_so_far ) {
positions.emplace(row, col);
max_so_far = data[row][col];
}
};
for ( size_t row = 0; row != data.size(); ++row ) {
max_so_far = 0;
for ( size_t col = 0; col != data[row].size(); ++col ) {
update(row, col);
}
max_so_far = 0;
for ( size_t col = data[row].size(); col-- > 0; ) {
update(row, col);
}
}
for ( size_t col = 0; col != data[0].size(); ++col ) {
max_so_far = 0;
for ( size_t row = 0; row != data.size(); ++row ) {
update(row, col);
}
max_so_far = 0;
for ( size_t row = data.size(); row-- > 0; ) {
update(row, col);
}
}
cout << positions.size() << endl;
}
int
main()
{
const auto grid = read_file("data/day08.txt");
part1(grid);
}
|