-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.cpp
138 lines (123 loc) · 3.08 KB
/
12.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
#include "aoc.hpp"
#include "ndvec.hpp"
#include "std.hpp"
using Vec2 = ndvec::vec2<double>;
struct Action {
enum class Type : char {
north = 'N',
south = 'S',
east = 'E',
west = 'W',
left = 'L',
right = 'R',
forward = 'F',
} type{};
int value{};
};
auto deg2rad(int deg) {
return deg * std::numbers::pi_v<double> / 180.0;
}
auto rotate(const Vec2& p, int deg) {
const auto cos{std::round(std::cos(deg2rad(deg)))};
const auto sin{std::round(std::sin(deg2rad(deg)))};
return Vec2(p.x() * cos - p.y() * sin, p.x() * sin + p.y() * cos);
}
auto find_part1(const auto& actions) {
using Type = Action::Type;
Vec2 pos(0, 0);
Vec2 dir(1, 0);
for (const Action& a : actions) {
switch (a.type) {
case Type::north: {
pos.y() += a.value;
} break;
case Type::south: {
pos.y() -= a.value;
} break;
case Type::east: {
pos.x() += a.value;
} break;
case Type::west: {
pos.x() -= a.value;
} break;
case Type::left: {
dir = rotate(dir, a.value);
} break;
case Type::right: {
dir = rotate(dir, -a.value);
} break;
case Type::forward: {
pos.x() += dir.x() * a.value;
pos.y() += dir.y() * a.value;
} break;
}
}
return pos.distance(Vec2());
}
auto find_part2(const auto& actions) {
using Type = Action::Type;
Vec2 pos(0, 0);
Vec2 wpos(10, 1);
for (const Action& a : actions) {
switch (a.type) {
case Type::north: {
wpos.y() += a.value;
} break;
case Type::south: {
wpos.y() -= a.value;
} break;
case Type::east: {
wpos.x() += a.value;
} break;
case Type::west: {
wpos.x() -= a.value;
} break;
case Type::left: {
wpos = rotate(wpos, a.value);
} break;
case Type::right: {
wpos = rotate(wpos, -a.value);
} break;
case Type::forward: {
pos.x() += wpos.x() * a.value;
pos.y() += wpos.y() * a.value;
} break;
}
}
return pos.distance(Vec2());
}
std::istream& operator>>(std::istream& is, Action::Type& type) {
using Type = Action::Type;
if (std::underlying_type_t<Type> ch{}; is >> ch) {
switch (ch) {
case std::to_underlying(Type::north):
case std::to_underlying(Type::south):
case std::to_underlying(Type::east):
case std::to_underlying(Type::west):
case std::to_underlying(Type::left):
case std::to_underlying(Type::right):
case std::to_underlying(Type::forward): {
type = {ch};
} break;
default: {
throw std::runtime_error(std::format("unknown action type {}", ch));
}
}
}
return is;
}
std::istream& operator>>(std::istream& is, Action& action) {
if (Action::Type type{}; is >> type) {
if (int value{}; is >> value) {
action = {type, value};
}
}
return is;
}
int main() {
const auto actions{aoc::parse_items<Action>("/dev/stdin")};
const auto part1{find_part1(actions)};
const auto part2{find_part2(actions)};
std::println("{} {}", part1, part2);
return 0;
}