-
Notifications
You must be signed in to change notification settings - Fork 0
/
08.cpp
52 lines (48 loc) · 1.25 KB
/
08.cpp
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
#include "aoc.hpp"
#include "ndvec.hpp"
#include "std.hpp"
using Vec2 = ndvec::vec2<int>;
using Trees = std::unordered_map<Vec2, int>;
auto search(const Trees& trees) {
int part1{};
int part2{};
for (auto [center, center_height] : trees) {
bool any_visible{false};
int scenic_score{1};
for (Vec2 direction : Vec2(0, 0).adjacent()) {
int score{};
bool visible{true};
for (Vec2 t{center + direction}; trees.contains(t); t += direction) {
++score;
if (trees.at(t) >= center_height) {
visible = false;
break;
}
}
any_visible |= visible;
scenic_score *= score;
}
part1 += int{any_visible};
part2 = std::max(part2, scenic_score);
}
return std::pair{part1, part2};
}
Trees parse_trees(std::string_view path) {
Trees trees;
for (Vec2 p; const std::string& line : aoc::slurp_lines(path)) {
for (p.x() = 0; char ch : line) {
if (not aoc::is_digit(ch)) {
throw std::runtime_error("all non-whitespace input must be digits");
}
trees[p] = ch - '0';
p.x() += 1;
}
p.y() += 1;
}
return trees;
}
int main() {
const auto [part1, part2]{search(parse_trees("/dev/stdin"))};
std::println("{} {}", part1, part2);
return 0;
}