diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-10-19 20:40:19 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-10-19 20:40:19 +0200 |
| commit | 40db239347e4ca59c4dda24bed7177fdbf1b7ff7 (patch) | |
| tree | 44deb2b2537529fd4d9fe79084d32a397fd0fd07 /2016/src/day17.cpp | |
| parent | 91f2886f6334e7d58eab3ac24886dea06d8d77b5 (diff) | |
| download | advent-of-code-40db239347e4ca59c4dda24bed7177fdbf1b7ff7.tar.gz advent-of-code-40db239347e4ca59c4dda24bed7177fdbf1b7ff7.tar.bz2 advent-of-code-40db239347e4ca59c4dda24bed7177fdbf1b7ff7.zip | |
day 17, aoc 2016
Diffstat (limited to '2016/src/day17.cpp')
| -rw-r--r-- | 2016/src/day17.cpp | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/2016/src/day17.cpp b/2016/src/day17.cpp new file mode 100644 index 0000000..757a8cb --- /dev/null +++ b/2016/src/day17.cpp | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | // Standard C++ | ||
| 2 | #include <algorithm> | ||
| 3 | #include <array> | ||
| 4 | #include <iostream> | ||
| 5 | #include <map> | ||
| 6 | #include <queue> | ||
| 7 | #include <string> | ||
| 8 | #include <tuple> | ||
| 9 | |||
| 10 | // System | ||
| 11 | #include <md5.h> | ||
| 12 | |||
| 13 | using namespace std; | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | using pos_type = tuple<int, int, string>; | ||
| 18 | |||
| 19 | string | ||
| 20 | md5(string_view input) | ||
| 21 | { | ||
| 22 | array<char, MD5_DIGEST_STRING_LENGTH> digest{}; | ||
| 23 | MD5Data(input.data(), input.size(), digest.data()); | ||
| 24 | return digest.data(); | ||
| 25 | } | ||
| 26 | |||
| 27 | void | ||
| 28 | part1(const string& passcode) | ||
| 29 | { | ||
| 30 | static const map<size_t, tuple<char, int, int>> dirs{ | ||
| 31 | { 0, { 'U', 0, -1 } }, | ||
| 32 | { 1, { 'D', 0, 1 } }, | ||
| 33 | { 2, { 'L', -1, 0 } }, | ||
| 34 | { 3, { 'R', 1, 0 } } | ||
| 35 | }; | ||
| 36 | |||
| 37 | auto is_open = [](auto chr) { | ||
| 38 | return strchr("bcdef", chr) != nullptr; | ||
| 39 | }; | ||
| 40 | |||
| 41 | queue<pos_type> queue; | ||
| 42 | queue.emplace(0, 0, ""); | ||
| 43 | |||
| 44 | while ( !queue.empty() ) { | ||
| 45 | auto [x, y, path] = queue.front(); | ||
| 46 | queue.pop(); | ||
| 47 | |||
| 48 | if ( x == 3 && y == 3 ) { | ||
| 49 | cout << path << '\n'; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | auto hash = md5(passcode + path); | ||
| 54 | |||
| 55 | for ( size_t i = 0; i != 4; ++i ) { | ||
| 56 | if ( !is_open(hash.at(i)) ) { | ||
| 57 | continue; | ||
| 58 | } | ||
| 59 | |||
| 60 | auto [chr, dx, dy] = dirs.at(i); | ||
| 61 | |||
| 62 | auto new_x = x + dx; | ||
| 63 | auto new_y = y + dy; | ||
| 64 | |||
| 65 | if ( new_x < 0 || new_y < 0 || new_x >= 4 || new_y >= 4 ) { | ||
| 66 | continue; | ||
| 67 | } | ||
| 68 | |||
| 69 | queue.emplace(new_x, new_y, path + chr); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | void | ||
| 75 | part2(const string& passcode) | ||
| 76 | { | ||
| 77 | static const map<size_t, tuple<char, int, int>> dirs{ | ||
| 78 | { 0, { 'U', 0, -1 } }, | ||
| 79 | { 1, { 'D', 0, 1 } }, | ||
| 80 | { 2, { 'L', -1, 0 } }, | ||
| 81 | { 3, { 'R', 1, 0 } } | ||
| 82 | }; | ||
| 83 | |||
| 84 | auto is_open = [](auto chr) { | ||
| 85 | return strchr("bcdef", chr) != nullptr; | ||
| 86 | }; | ||
| 87 | |||
| 88 | size_t max_length = 0; | ||
| 89 | |||
| 90 | queue<pos_type> queue; | ||
| 91 | queue.emplace(0, 0, ""); | ||
| 92 | |||
| 93 | while ( !queue.empty() ) { | ||
| 94 | auto [x, y, path] = queue.front(); | ||
| 95 | queue.pop(); | ||
| 96 | |||
| 97 | if ( x == 3 && y == 3 ) { | ||
| 98 | max_length = max(max_length, path.size()); | ||
| 99 | continue; | ||
| 100 | } | ||
| 101 | |||
| 102 | auto hash = md5(passcode + path); | ||
| 103 | |||
| 104 | for ( size_t i = 0; i != 4; ++i ) { | ||
| 105 | if ( !is_open(hash.at(i)) ) { | ||
| 106 | continue; | ||
| 107 | } | ||
| 108 | |||
| 109 | auto [chr, dx, dy] = dirs.at(i); | ||
| 110 | |||
| 111 | auto new_x = x + dx; | ||
| 112 | auto new_y = y + dy; | ||
| 113 | |||
| 114 | if ( new_x < 0 || new_y < 0 || new_x >= 4 || new_y >= 4 ) { | ||
| 115 | continue; | ||
| 116 | } | ||
| 117 | |||
| 118 | queue.emplace(new_x, new_y, path + chr); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | cout << max_length << '\n'; | ||
| 123 | } | ||
| 124 | |||
| 125 | } // namespace | ||
| 126 | |||
| 127 | int | ||
| 128 | main() | ||
| 129 | { | ||
| 130 | part1("awrkjxxr"); | ||
| 131 | part2("awrkjxxr"); | ||
| 132 | } | ||
