diff options
Diffstat (limited to '2016/src/day01.cpp')
| -rw-r--r-- | 2016/src/day01.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/2016/src/day01.cpp b/2016/src/day01.cpp new file mode 100644 index 0000000..f5d74a5 --- /dev/null +++ b/2016/src/day01.cpp | |||
| @@ -0,0 +1,122 @@ | |||
| 1 | #include <array> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <set> | ||
| 5 | #include <string> | ||
| 6 | #include <tuple> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | vector<string> | ||
| 12 | split(string_view line, string_view delimiter) | ||
| 13 | { | ||
| 14 | size_t pos_start = 0; | ||
| 15 | size_t pos_end = 0; | ||
| 16 | |||
| 17 | vector<string> res; | ||
| 18 | |||
| 19 | while ( (pos_end = line.find(delimiter, pos_start)) != string::npos ) { | ||
| 20 | auto token = line.substr(pos_start, pos_end - pos_start); | ||
| 21 | pos_start = pos_end + delimiter.length(); | ||
| 22 | |||
| 23 | res.emplace_back(token); | ||
| 24 | } | ||
| 25 | |||
| 26 | if ( pos_start != line.size() ) { | ||
| 27 | res.emplace_back(line.substr(pos_start)); | ||
| 28 | } | ||
| 29 | return res; | ||
| 30 | } | ||
| 31 | |||
| 32 | vector<tuple<char, long>> | ||
| 33 | read_file(string_view filename) | ||
| 34 | { | ||
| 35 | fstream input{ filename }; | ||
| 36 | string content{ istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} }; | ||
| 37 | |||
| 38 | vector<tuple<char, long>> result; | ||
| 39 | for ( const auto& part: split(content, ", ") ) { | ||
| 40 | result.emplace_back(part[0], stol(part.substr(1))); | ||
| 41 | } | ||
| 42 | return result; | ||
| 43 | } | ||
| 44 | |||
| 45 | void | ||
| 46 | part1(const vector<tuple<char, long>>& input) | ||
| 47 | { | ||
| 48 | array<tuple<long, long>, 4> dirs = { | ||
| 49 | make_tuple(1L, 0L), | ||
| 50 | make_tuple(0L, 1L), | ||
| 51 | make_tuple(-1L, 0L), | ||
| 52 | make_tuple(0L, -1L) | ||
| 53 | }; | ||
| 54 | |||
| 55 | size_t dir = 0; | ||
| 56 | long pos_x = 0; | ||
| 57 | long pos_y = 0; | ||
| 58 | |||
| 59 | for ( const auto& [change, length]: input ) { | ||
| 60 | if ( change == 'L' ) { | ||
| 61 | dir = (dir + 1) % 4; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | dir = (dir + 3) % 4; | ||
| 65 | } | ||
| 66 | |||
| 67 | auto [delta_x, delta_y] = dirs.at(dir); | ||
| 68 | |||
| 69 | pos_x += length * delta_x; | ||
| 70 | pos_y += length * delta_y; | ||
| 71 | } | ||
| 72 | |||
| 73 | cout << abs(pos_x) + abs(pos_y) << endl; | ||
| 74 | } | ||
| 75 | |||
| 76 | void | ||
| 77 | part2(const vector<tuple<char, long>>& input) | ||
| 78 | { | ||
| 79 | array<tuple<long, long>, 4> dirs = { | ||
| 80 | make_tuple(1L, 0L), | ||
| 81 | make_tuple(0L, 1L), | ||
| 82 | make_tuple(-1L, 0L), | ||
| 83 | make_tuple(0L, -1L) | ||
| 84 | }; | ||
| 85 | |||
| 86 | size_t dir = 0; | ||
| 87 | long pos_x = 0; | ||
| 88 | long pos_y = 0; | ||
| 89 | |||
| 90 | set<tuple<long, long>> visited; | ||
| 91 | |||
| 92 | for ( auto [change, length]: input ) { | ||
| 93 | if ( change == 'L' ) { | ||
| 94 | dir = (dir + 1) % 4; | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | dir = (dir + 3) % 4; | ||
| 98 | } | ||
| 99 | |||
| 100 | auto [delta_x, delta_y] = dirs.at(dir); | ||
| 101 | |||
| 102 | while ( length-- > 0 ) { | ||
| 103 | pos_x += delta_x; | ||
| 104 | pos_y += delta_y; | ||
| 105 | |||
| 106 | if ( visited.contains({ pos_x, pos_y }) ) { | ||
| 107 | cout << abs(pos_x) + abs(pos_y) << endl; | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | visited.emplace(pos_x, pos_y); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | int | ||
| 117 | main() | ||
| 118 | { | ||
| 119 | auto input = read_file("data/day01.txt"); | ||
| 120 | part1(input); | ||
| 121 | part2(input); | ||
| 122 | } | ||
