-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
130 lines (118 loc) · 3.4 KB
/
main.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
#include <complex>
#include <iostream>
#include "boost/fusion/adapted.hpp"
#include "boost/spirit/home/x3.hpp"
#include "utils.h"
enum class Op {
N,
S,
E,
W,
L,
R,
F,
};
struct Op_ : boost::spirit::x3::symbols<Op> {
Op_() { add("N", Op::N)("S", Op::S)("E", Op::E)("W", Op::W)("L", Op::L)("R", Op::R)("F", Op::F); }
};
std::vector<std::pair<Op, int>> parseFile()
{
using namespace boost::spirit::x3;
const auto buffer = AOC::readFile("input.txt");
std::vector<std::pair<Op, int>> values;
auto row_ = Op_() >> int_;
auto result = parse(buffer.begin(), buffer.end(), row_ % eol, values);
assert(result);
std::cout << "Read " << values.size() << " entries" << std::endl;
return values;
}
struct Ferry {
int headingDegrees;
double x, y;
};
double toRadian(double value)
{
const double pi = std::acos(-1);
return pi * value / 180.0;
};
double f1(const std::vector<std::pair<Op, int>> &data)
{
Ferry ferry {};
for (auto [op, value] : data) {
switch (op) {
case Op::N: ferry.y += value; break;
case Op::S: ferry.y -= value; break;
case Op::E: ferry.x += value; break;
case Op::W: ferry.x -= value; break;
case Op::L:
ferry.headingDegrees += value;
ferry.headingDegrees %= 360;
break;
case Op::R:
ferry.headingDegrees += 360 - value;
ferry.headingDegrees %= 360;
break;
case Op::F:
ferry.x += value * std::cos(toRadian(ferry.headingDegrees));
ferry.y += value * std::sin(toRadian(ferry.headingDegrees));
break;
}
}
return std::abs(ferry.x) + std::abs(ferry.y);
}
double f2(const std::vector<std::pair<Op, int>> &data)
{
std::complex<double> ferry { 0, 0 };
std::complex<double> point { 10, 1 };
for (auto [op, value] : data) {
switch (op) {
case Op::N: {
auto offset = std::complex<double> { 0, static_cast<double>(value) };
point += offset;
break;
}
case Op::S: {
auto offset = std::complex<double> { 0, -static_cast<double>(value) };
point += offset;
break;
}
case Op::E: {
auto offset = std::complex<double> { static_cast<double>(value), 0 };
point += offset;
break;
}
case Op::W: {
auto offset = std::complex<double> { -static_cast<double>(value), 0 };
point += offset;
break;
}
case Op::L: {
auto temp = point - ferry;
auto radian = toRadian(value);
temp *= std::complex { std::cos(radian), std::sin(radian) };
point = temp + ferry;
break;
}
case Op::R: {
auto temp = point - ferry;
auto radian = toRadian(360 - value);
temp *= std::complex { std::cos(radian), std::sin(radian) };
point = temp + ferry;
break;
}
case Op::F: {
auto diff = point - ferry;
ferry += diff * static_cast<double>(value);
point += diff * static_cast<double>(value);
break;
}
}
}
return std::abs(ferry.real()) + std::abs(ferry.imag());
}
int main()
{
const auto data = parseFile();
std::cout << f1(data) << std::endl;
std::cout << f2(data) << std::endl;
}