diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
| commit | 756f22d58bb198b8f34589c112e1003614ccdcd6 (patch) | |
| tree | 4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day11.cpp | |
| parent | fcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff) | |
| download | advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2 advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip | |
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day11.cpp')
| -rw-r--r-- | 2017/src/day11.cpp | 104 |
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 | |||
| 11 | using namespace std; | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | string | ||
| 16 | read_file(const filesystem::path& filename) | ||
| 17 | { | ||
| 18 | ifstream file{ filename }; | ||
| 19 | string line; | ||
| 20 | getline(file, line); | ||
| 21 | return line; | ||
| 22 | } | ||
| 23 | |||
| 24 | vector<string> | ||
| 25 | split(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 | |||
| 37 | void | ||
| 38 | part1(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 | |||
| 64 | void | ||
| 65 | part2(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 | |||
| 97 | int | ||
| 98 | main() | ||
| 99 | { | ||
| 100 | auto line = read_file("data/day11.txt"); | ||
| 101 | auto steps = split(line); | ||
| 102 | part1(steps); | ||
| 103 | part2(steps); | ||
| 104 | } | ||
