aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day11.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2017/src/day11.cpp')
-rw-r--r--2017/src/day11.cpp104
1 files changed, 104 insertions, 0 deletions
diff --git a/2017/src/day11.cpp b/2017/src/day11.cpp
new file mode 100644
index 0000000..ea945c1
--- /dev/null
+++ b/2017/src/day11.cpp
@@ -0,0 +1,104 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <map>
5#include <sstream>
6#include <string>
7#include <vector>
8
9// see: https://www.redblobgames.com/grids/hexagons/
10
11using namespace std;
12
13namespace {
14
15string
16read_file(const filesystem::path& filename)
17{
18 ifstream file{ filename };
19 string line;
20 getline(file, line);
21 return line;
22}
23
24vector<string>
25split(const string& line)
26{
27 stringstream strm{ line };
28 vector<string> data;
29
30 for ( string word; getline(strm, word, ','); ) {
31 data.emplace_back(word);
32 }
33
34 return data;
35}
36
37void
38part1(const vector<string>& steps)
39{
40 // string => (q, s, r)
41 map<string, tuple<int, int, int>> dirs{
42 { "n", { 0, 1, -1 } },
43 { "nw", { -1, 1, 0 } },
44 { "sw", { -1, 0, 1 } },
45 { "s", { 0, -1, 1 } },
46 { "se", { 1, -1, 0 } },
47 { "ne", { 1, 0, -1 } }
48 };
49
50 tuple<int, int, int> current = { 0, 0, 0 };
51 for ( const auto& step: steps ) {
52 const auto [dq, ds, dr] = dirs.at(step);
53
54 get<0>(current) += dq;
55 get<1>(current) += ds;
56 get<2>(current) += dr;
57 }
58
59 auto dist = (abs(get<0>(current)) + abs(get<1>(current)) + abs(get<2>(current))) / 2;
60
61 cout << "Part1: " << dist << '\n';
62}
63
64void
65part2(const vector<string>& steps)
66{
67 // string => (q, s, r)
68 map<string, tuple<int, int, int>> dirs{
69 { "n", { 0, 1, -1 } },
70 { "nw", { -1, 1, 0 } },
71 { "sw", { -1, 0, 1 } },
72 { "s", { 0, -1, 1 } },
73 { "se", { 1, -1, 0 } },
74 { "ne", { 1, 0, -1 } }
75 };
76
77 int max_dist = 0;
78
79 tuple<int, int, int> current = { 0, 0, 0 };
80 for ( const auto& step: steps ) {
81 const auto [dq, ds, dr] = dirs.at(step);
82
83 get<0>(current) += dq;
84 get<1>(current) += ds;
85 get<2>(current) += dr;
86
87 auto dist = (abs(get<0>(current)) + abs(get<1>(current)) + abs(get<2>(current))) / 2;
88
89 max_dist = max(max_dist, dist);
90 }
91
92 cout << "Part2: " << max_dist << '\n';
93}
94
95} // namespace
96
97int
98main()
99{
100 auto line = read_file("data/day11.txt");
101 auto steps = split(line);
102 part1(steps);
103 part2(steps);
104}