-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge18.cpp
158 lines (130 loc) · 5.44 KB
/
challenge18.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
#include "challenge18.hpp"
#include "helper.hpp"
#include "print.hpp"
#include "3rdParty/ctre/include/ctre.hpp"
#include <algorithm>
#include <ranges>
using namespace std::string_view_literals;
namespace {
enum class Direction : std::int8_t { Up, Left, Down, Right };
Direction turnLeft(Direction d) noexcept {
return static_cast<Direction>((static_cast<int>(d) + 4 - 1) % 4);
}
Direction turnRight(Direction d) noexcept {
return static_cast<Direction>((static_cast<int>(d) + 1) % 4);
}
Direction turnAround(Direction d) noexcept {
return static_cast<Direction>((static_cast<int>(d) + 2) % 4);
}
bool isUpDown(Direction d) noexcept {
using enum Direction;
return d == Up || d == Down;
}
template<typename T>
struct Coordinate {
T Row;
T Column;
constexpr bool operator==(const Coordinate&) const noexcept = default;
constexpr auto 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};
}
Coordinate& move(Direction direction, T distance = 1) noexcept {
switch ( direction ) {
using enum Direction;
case Left : Column -= distance; break;
case Up : Row -= distance; break;
case Right : Column += distance; break;
case Down : Row += distance; break;
} //switch ( direction )
return *this;
}
Coordinate moved(Direction direction, T distance = 1) const noexcept {
auto ret = *this;
ret.move(direction, distance);
return ret;
}
};
} //namespace
namespace std {
template<typename T>
struct hash<Coordinate<T>> {
size_t operator()(const Coordinate<T>& c) const noexcept {
constexpr auto bits = std::numeric_limits<T>::digits / 2;
constexpr auto mask = ((T{1} << bits) - 1);
return std::hash<T>{}((c.Row << bits) | (c.Column & mask));
}
};
} //namespace std
namespace {
struct DigAction {
::Direction Direction;
std::int64_t Length;
};
using ParseResult = std::vector<DigAction>;
template<bool UseColor>
ParseResult parse(const std::vector<std::string_view>& input) {
ParseResult ret;
ret.reserve(input.size());
for ( const auto line : input ) {
auto match = ctre::match<R"(([ULRD]) (\d+) \(#([0-9a-fA-F]{5})([0-3])\))">(line);
throwIfInvalid(match);
auto [direction, length] = [&match](void) {
if constexpr ( UseColor ) {
return std::pair{[](const char c) noexcept {
return static_cast<Direction>(3 - (c - '0'));
}(static_cast<std::string_view>(match.get<4>()).front()),
static_cast<std::size_t>(convert<16>(match.get<3>()))};
} //if constexpr ( UseColor )
else {
return std::pair{[](const char c) noexcept {
switch ( c ) {
using enum Direction;
case 'U' : return Up;
case 'D' : return Down;
case 'L' : return Left;
default : return Right;
} //switch ( c )
}(static_cast<std::string_view>(match.get<1>()).front()),
static_cast<std::size_t>(convert(match.get<2>()))};
} //else -> if constexpr( UseColor )
}();
ret.emplace_back(direction, length);
} //for ( const auto line : input )
return ret;
}
std::int64_t dig(const ParseResult& input) noexcept {
const auto coordinateAndMove = [current = Coordinate<std::int64_t>{0, 0}](const DigAction& dig) mutable noexcept {
return current.move(dig.Direction, dig.Length);
};
const auto calcDeterminante = [](const Coordinate<std::int64_t>& left,
const Coordinate<std::int64_t>& right) noexcept {
return (left.Row + right.Row) * (left.Column - right.Column);
};
std::vector<Coordinate<std::int64_t>> coordinates;
coordinates.resize(input.size());
std::ranges::transform(input, coordinates.begin(), coordinateAndMove);
const auto innerArea = std::ranges::fold_left(std::views::zip_transform(calcDeterminante, coordinates,
coordinates | std::views::drop(1)),
0, std::plus<>{}) /
2;
const auto perimeter = std::ranges::fold_left(input | std::views::transform(&DigAction::Length), 0, std::plus<>{});
return innerArea + perimeter / 2 + 1;
}
} //namespace
bool challenge18(const std::vector<std::string_view>& rawInput) {
auto area1 = dig(parse<false>(rawInput));
myPrint(" == Result of Part 1: {:d} ==\n", area1);
auto area2 = dig(parse<true>(rawInput));
myPrint(" == Result of Part 2: {:d} ==\n", area2);
return area1 == 40714 && area2 == 129'849'166'997'110;
}