-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path11.cpp
69 lines (59 loc) · 1.67 KB
/
11.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
#include <algorithm>
#include <chrono>
#include <iostream>
#include <string_view>
// see http://ondras.github.io/rot.js/manual/#hex/indexing
inline int distance_from_start(int x, int y) {
return std::max(abs(x), abs(y) + (abs(x) / 2));
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
std::string input;
std::getline(std::cin, input);
// see https://www.redblobgames.com/grids/hexagons/#coordinates-offset
int x = 0;
int y = 0;
int max_distance = 0;
auto it = input.begin();
const auto eof = input.end();
while (it < eof) {
const auto end = std::find(it, eof, ',');
const std::string_view op = std::string_view(it, end);
const bool isodd = x & 1;
if (op == "n") {
x += 0;
y += -1;
} else if (op == "ne") {
x += 1;
y += isodd ? 0 : -1;
} else if (op == "se") {
x += 1;
y += isodd ? 1 : 0;
} else if (op == "s") {
x += 0;
y += 1;
} else if (op == "sw") {
x += -1;
y += isodd ? 1 : 0;
} else if (op == "nw") {
x += -1;
y += isodd ? 0 : -1;
}
max_distance = std::max(max_distance, distance_from_start(x, y));
it = end + 1;
}
pt1 = distance_from_start(x, y);
pt2 = max_distance;
std::cout << "--- Day 11: Hex Ed ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}