-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge10.cpp
More file actions
96 lines (80 loc) · 3.28 KB
/
challenge10.cpp
File metadata and controls
96 lines (80 loc) · 3.28 KB
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
#include "challenge10.hpp"
#include "helper.hpp"
#include "print.hpp"
#include <algorithm>
#include <ranges>
#include <stack>
#include <unordered_set>
namespace {
using Position = Coordinate<std::int64_t>;
auto calcScore(Position startPosition, MapView map) noexcept {
struct ToCheck {
Position Pos;
int Distance;
};
std::stack<ToCheck> toCheck;
std::unordered_set<Position> uniqueEndPositions;
toCheck.push({startPosition, 0});
while ( !toCheck.empty() ) {
auto [position, distance] = toCheck.top();
toCheck.pop();
if ( distance == 9 ) {
uniqueEndPositions.insert(position);
continue;
} //if ( distance == 9 )
const auto nextDistance = distance + 1;
const char expectedField = static_cast<char>('0' + nextDistance);
std::ranges::for_each(
position.validNeighbors() | std::views::filter([expectedField, map](Position nextPosition) noexcept {
return map[nextPosition] == expectedField;
}),
[&toCheck, nextDistance](Position nextPosition) noexcept { toCheck.push({nextPosition, nextDistance}); });
} //while ( !toCheck.empty() )
return uniqueEndPositions.size();
}
auto calcRating(Position startPosition, MapView map) noexcept {
struct ToCheck {
Position Pos;
int Distance;
};
std::stack<ToCheck> toCheck;
toCheck.push({startPosition, 0});
std::int64_t rating = 0;
while ( !toCheck.empty() ) {
auto [position, distance] = toCheck.top();
toCheck.pop();
if ( distance == 9 ) {
++rating;
continue;
} //if ( distance == 9 )
const auto nextDistance = distance + 1;
const char expectedField = static_cast<char>('0' + nextDistance);
std::ranges::for_each(
position.validNeighbors() | std::views::filter([expectedField, map](Position nextPosition) noexcept {
return map[nextPosition] == expectedField;
}),
[&toCheck, nextDistance](Position nextPosition) noexcept { toCheck.push({nextPosition, nextDistance}); });
} //while ( !toCheck.empty() )
return rating;
}
} //namespace
bool challenge10(const std::vector<std::string_view>& input) {
MapView map{input};
Position::setMaxFromMap(map);
auto allStartPositions =
Position::allPositions() | std::views::filter([map](Position pos) noexcept { return map[pos] == '0'; });
const auto sum1 =
std::ranges::fold_left(allStartPositions | std::views::transform([map](Position startPosition) noexcept {
return calcScore(startPosition, map);
}),
0, std::plus<>{});
myPrint(" == Result of Part 1: {:d} ==\n", sum1);
auto sum2 =
std::ranges::fold_left(allStartPositions | std::views::transform([map](Position startPosition) noexcept {
return calcRating(startPosition, map);
}),
0, std::plus<>{});
;
myPrint(" == Result of Part 2: {:d} ==\n", sum2);
return sum1 == 538 && sum2 == 1110;
}