-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC2017_11.cpp
69 lines (58 loc) 路 1.75 KB
/
AoC2017_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 <assert.h>
#include <algorithm>
#include <climits>
#include <string>
#include <unordered_map>
#include <vector>
#include "../../aoc/aoc.hpp"
#include "../../aoc/geometry3d/geometry3d.hpp"
#include "../../aocd/aocd.hpp"
using namespace std;
const Position3D ORIGIN = Position3D(0, 0, 0);
// clang-format off
const unordered_map<string, Vector3D> HEADING = {
{"n", Vector3D( 0, -1, 1)},
{"ne", Vector3D( 1, -1, 0)},
{"se", Vector3D( 1, 0, -1)},
{"s", Vector3D( 0, 1, -1)},
{"sw", Vector3D( -1, 1, 0)},
{"nw", Vector3D( -1, 0, 1)}
};
// clang-format on
vector<Vector3D> parse(const vector<string>& input) {
assert(input.size() == 1);
const auto& splits = aoc::split(input[0], ",");
vector<Vector3D> path;
transform(splits.begin(), splits.end(), back_inserter(path),
[](const string s) { return HEADING.at(s); });
return path;
}
vector<Position3D> positions(const vector<Vector3D>& path) {
vector<Position3D> positions = {ORIGIN};
for (const Vector3D& v : path) {
positions.push_back(positions.back().translate(v));
}
return positions;
}
inline int steps(const Position3D& p) {
return p.manhattanDistance(ORIGIN) / 2;
}
int part1(const vector<string>& input) {
const vector<Vector3D>& path = parse(input);
return steps(positions(path).back());
}
int part2(const vector<string>& input) {
const vector<Vector3D>& path = parse(input);
int ans = -INT_MAX;
for (const Position3D& p : positions(path)) {
ans = max(ans, steps(p));
}
return ans;
}
void samples() {
assert(part1({"ne,ne,ne"}) == 3);
assert(part1({"ne,ne,sw,sw"}) == 0);
assert(part1({"ne,ne,s,s"}) == 2);
assert(part1({"se,sw,se,sw,sw"}) == 3);
}
MAIN(2017, 11)