diff options
Diffstat (limited to '2016/src')
| -rw-r--r-- | 2016/src/day02.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/2016/src/day02.cpp b/2016/src/day02.cpp new file mode 100644 index 0000000..ad625d5 --- /dev/null +++ b/2016/src/day02.cpp | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <string> | ||
| 5 | #include <tuple> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | vector<string> | ||
| 11 | read_file(string_view filename) | ||
| 12 | { | ||
| 13 | fstream input{ filename }; | ||
| 14 | vector<string> lines; | ||
| 15 | |||
| 16 | vector<tuple<char, long>> result; | ||
| 17 | for ( string line; getline(input, line); ) { | ||
| 18 | lines.emplace_back(line); | ||
| 19 | } | ||
| 20 | return lines; | ||
| 21 | } | ||
| 22 | |||
| 23 | void | ||
| 24 | part1(const vector<string>& lines) | ||
| 25 | { | ||
| 26 | int pos_x = 1; | ||
| 27 | int pos_y = 1; | ||
| 28 | |||
| 29 | for ( const auto& line: lines ) { | ||
| 30 | for ( const auto chr: line ) { | ||
| 31 | switch ( chr ) { | ||
| 32 | case 'U': | ||
| 33 | --pos_y; | ||
| 34 | break; | ||
| 35 | case 'R': | ||
| 36 | ++pos_x; | ||
| 37 | break; | ||
| 38 | case 'L': | ||
| 39 | --pos_x; | ||
| 40 | break; | ||
| 41 | case 'D': | ||
| 42 | ++pos_y; | ||
| 43 | break; | ||
| 44 | default: | ||
| 45 | cerr << "unknown char: " << chr << endl; | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | pos_x = clamp(pos_x, 0, 2); | ||
| 50 | pos_y = clamp(pos_y, 0, 2); | ||
| 51 | } | ||
| 52 | cout << pos_y * 3 + pos_x + 1; | ||
| 53 | } | ||
| 54 | cout << endl; | ||
| 55 | } | ||
| 56 | |||
| 57 | void | ||
| 58 | part2(const vector<string>& lines) | ||
| 59 | { | ||
| 60 | const map<tuple<int, int>, char> keypad = { | ||
| 61 | { { 2, 0 }, '1' }, | ||
| 62 | { { 1, 1 }, '2' }, | ||
| 63 | { { 2, 1 }, '3' }, | ||
| 64 | { { 3, 1 }, '4' }, | ||
| 65 | { { 0, 2 }, '5' }, | ||
| 66 | { { 1, 2 }, '6' }, | ||
| 67 | { { 2, 2 }, '7' }, | ||
| 68 | { { 3, 2 }, '8' }, | ||
| 69 | { { 4, 2 }, '9' }, | ||
| 70 | { { 1, 3 }, 'A' }, | ||
| 71 | { { 2, 3 }, 'B' }, | ||
| 72 | { { 3, 3 }, 'C' }, | ||
| 73 | { { 2, 4 }, 'D' } | ||
| 74 | }; | ||
| 75 | |||
| 76 | tuple<int, int> pos = { 0, 2 }; | ||
| 77 | |||
| 78 | for ( const auto& line: lines ) { | ||
| 79 | for ( const auto chr: line ) { | ||
| 80 | auto pos2 = pos; | ||
| 81 | switch ( chr ) { | ||
| 82 | case 'U': | ||
| 83 | --get<1>(pos2); | ||
| 84 | break; | ||
| 85 | case 'R': | ||
| 86 | ++get<0>(pos2); | ||
| 87 | break; | ||
| 88 | case 'L': | ||
| 89 | --get<0>(pos2); | ||
| 90 | break; | ||
| 91 | case 'D': | ||
| 92 | ++get<1>(pos2); | ||
| 93 | break; | ||
| 94 | default: | ||
| 95 | cerr << "unknown char: " << chr << endl; | ||
| 96 | return; | ||
| 97 | } | ||
| 98 | |||
| 99 | if ( keypad.contains(pos2) ) { | ||
| 100 | pos = pos2; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | cout << keypad.at(pos); | ||
| 104 | } | ||
| 105 | cout << endl; | ||
| 106 | } | ||
| 107 | |||
| 108 | int | ||
| 109 | main() | ||
| 110 | { | ||
| 111 | auto lines = read_file("data/day02.txt"); | ||
| 112 | part1(lines); | ||
| 113 | part2(lines); | ||
| 114 | } | ||
