-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge16.cpp
198 lines (161 loc) · 6.99 KB
/
challenge16.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "challenge16.hpp"
#include "helper.hpp"
#include "print.hpp"
#include <algorithm>
#include <ranges>
#include <unordered_set>
using namespace std::string_view_literals;
namespace {
enum class Direction { Up, Down, Left, Right };
struct Coordinate {
std::size_t Row;
std::size_t Column;
constexpr bool operator==(const Coordinate&) const noexcept = default;
Coordinate left(void) const noexcept {
return {Row, Column - 1};
}
Coordinate right(void) const noexcept {
return {Row, Column + 1};
}
Coordinate up(void) const noexcept {
return {Row - 1, Column};
}
Coordinate down(void) const noexcept {
return {Row + 1, Column};
}
};
using CoordinateAndDirection = std::pair<Coordinate, Direction>;
} //namespace
namespace std {
template<>
struct hash<Coordinate> {
size_t operator()(const Coordinate& c) const noexcept {
std::hash<std::size_t> h;
return h(c.Row << 8) ^ h(c.Column);
}
};
template<>
struct hash<CoordinateAndDirection> {
size_t operator()(const CoordinateAndDirection& cd) const noexcept {
std::hash<Coordinate> c;
std::hash<Direction> d;
return (c(cd.first) << 8) ^ d(cd.second);
}
};
} //namespace std
namespace {
struct Energizer {
const std::vector<std::string_view>& Map;
const std::size_t RowCount;
const std::size_t ColumnCount;
std::unordered_set<Coordinate> AlreadyEnergized;
std::unordered_set<CoordinateAndDirection> AlreadyVisited;
Energizer(const std::vector<std::string_view>& map) noexcept :
Map{map}, RowCount{map.size()}, ColumnCount{map.front().size()} {
return;
}
std::int64_t startEnergize(Coordinate pos = {0, 0}, Direction dir = Direction::Right) noexcept {
AlreadyVisited.clear();
AlreadyEnergized.clear();
return energize(pos, dir);
}
std::int64_t energize(Coordinate pos, Direction dir) noexcept {
if ( !AlreadyVisited.insert({pos, dir}).second ) {
return 0;
} //if ( !AlreadyVisited.insert({pos, dir}).second )
std::int64_t res = AlreadyEnergized.insert(pos).second ? 1 : 0;
switch ( dir ) {
case Direction::Left : res += moveLeft(pos); break;
case Direction::Right : res += moveRight(pos); break;
case Direction::Up : res += moveUp(pos); break;
case Direction::Down : res += moveDown(pos); break;
} //switch ( dir )
// Cache.emplace(CoordinateAndDirection{pos, dir}, res);
return res;
}
std::int64_t checkValidAndEnergize(Coordinate pos, Direction dir) noexcept {
if ( pos.Row >= RowCount || pos.Column >= ColumnCount ) {
return 0;
} //if ( pos.Row >= RowCount || pos.Column >= ColumnCount )
return energize(pos, dir);
}
char mapEntry(Coordinate pos) noexcept {
return Map[pos.Row][pos.Column];
}
std::int64_t moveLeft(Coordinate pos) noexcept {
switch ( mapEntry(pos) ) {
case '|' : {
return checkValidAndEnergize(pos.up(), Direction::Up) +
checkValidAndEnergize(pos.down(), Direction::Down);
} //case '|'
case '-' :
case '.' : return checkValidAndEnergize(pos.left(), Direction::Left);
case '\\' : return checkValidAndEnergize(pos.up(), Direction::Up);
case '/' : return checkValidAndEnergize(pos.down(), Direction::Down);
} //switch ( mapEntry(pos) )
throwIfInvalid(false);
return 0;
}
std::int64_t moveRight(Coordinate pos) noexcept {
switch ( mapEntry(pos) ) {
case '|' : {
return checkValidAndEnergize(pos.up(), Direction::Up) +
checkValidAndEnergize(pos.down(), Direction::Down);
} //case '|'
case '-' :
case '.' : return checkValidAndEnergize(pos.right(), Direction::Right);
case '/' : return checkValidAndEnergize(pos.up(), Direction::Up);
case '\\' : return checkValidAndEnergize(pos.down(), Direction::Down);
} //switch ( mapEntry(pos) )
throwIfInvalid(false);
return 0;
}
std::int64_t moveUp(Coordinate pos) noexcept {
switch ( mapEntry(pos) ) {
case '-' : {
return checkValidAndEnergize(pos.left(), Direction::Left) +
checkValidAndEnergize(pos.right(), Direction::Right);
} //case '-'
case '|' :
case '.' : return checkValidAndEnergize(pos.up(), Direction::Up);
case '/' : return checkValidAndEnergize(pos.right(), Direction::Right);
case '\\' : return checkValidAndEnergize(pos.left(), Direction::Left);
} //switch ( mapEntry(pos) )
throwIfInvalid(false);
return 0;
}
std::int64_t moveDown(Coordinate pos) noexcept {
switch ( mapEntry(pos) ) {
case '-' : {
return checkValidAndEnergize(pos.left(), Direction::Left) +
checkValidAndEnergize(pos.right(), Direction::Right);
} //case '-'
case '|' :
case '.' : return checkValidAndEnergize(pos.down(), Direction::Down);
case '\\' : return checkValidAndEnergize(pos.right(), Direction::Right);
case '/' : return checkValidAndEnergize(pos.left(), Direction::Left);
} //switch ( mapEntry(pos) )
throwIfInvalid(false);
return 0;
}
};
} //namespace
bool challenge16(const std::vector<std::string_view>& input) {
throwIfInvalid(!input.empty());
Energizer energizer{input};
std::int64_t sum1 = energizer.startEnergize();
myPrint(" == Result of Part 1: {:d} ==\n", sum1);
std::int64_t sum2 = std::ranges::max(
std::views::iota(0uz, energizer.ColumnCount) | std::views::transform([&energizer](std::size_t column) noexcept {
return std::max(energizer.startEnergize({0, column}, Direction::Down),
energizer.startEnergize({energizer.RowCount - 1, column}, Direction::Up));
}));
sum2 = std::max(sum2, std::ranges::max(std::views::iota(0uz, energizer.RowCount) |
std::views::transform([&energizer](std::size_t row) noexcept {
return std::max(energizer.startEnergize({row, 0}, Direction::Right),
energizer.startEnergize({row, energizer.ColumnCount - 1},
Direction::Left));
})));
myPrint(" == Result of Part 2: {:d} ==\n", sum2);
return sum1 == 8021 && sum2 == 8216;
}