diff options
Diffstat (limited to '2017/src/day25.cpp')
| -rw-r--r-- | 2017/src/day25.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/2017/src/day25.cpp b/2017/src/day25.cpp new file mode 100644 index 0000000..36b7dd0 --- /dev/null +++ b/2017/src/day25.cpp | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | #include <array> | ||
| 2 | #include <filesystem> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <iterator> | ||
| 6 | #include <map> | ||
| 7 | #include <string> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | using namespace std; | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | vector<string> | ||
| 15 | split(const string& line, const string& delimiter) | ||
| 16 | { | ||
| 17 | vector<string> result; | ||
| 18 | |||
| 19 | size_t start = 0; | ||
| 20 | size_t end = 0; | ||
| 21 | |||
| 22 | while ( (end = line.find(delimiter, start)) != string::npos ) { | ||
| 23 | if ( end != start ) { | ||
| 24 | result.emplace_back(line.substr(start, end - start)); | ||
| 25 | } | ||
| 26 | |||
| 27 | start = end + delimiter.length(); | ||
| 28 | } | ||
| 29 | |||
| 30 | if ( start != line.size() ) { | ||
| 31 | result.emplace_back(line.substr(start)); | ||
| 32 | } | ||
| 33 | |||
| 34 | return result; | ||
| 35 | } | ||
| 36 | |||
| 37 | vector<vector<vector<string>>> | ||
| 38 | read_file(const filesystem::path& filename) | ||
| 39 | { | ||
| 40 | ifstream file{ filename }; | ||
| 41 | string content{ istreambuf_iterator<char>{ file }, {} }; | ||
| 42 | |||
| 43 | vector<vector<vector<string>>> result; | ||
| 44 | |||
| 45 | auto blocks = split(content, "\n\n"); | ||
| 46 | for ( const auto& block: blocks ) { | ||
| 47 | vector<vector<string>> data; | ||
| 48 | |||
| 49 | auto lines = split(block, "\n"); | ||
| 50 | for ( auto line: lines ) { | ||
| 51 | line.pop_back(); | ||
| 52 | auto words = split(line, " "); | ||
| 53 | data.emplace_back(words); | ||
| 54 | } | ||
| 55 | |||
| 56 | result.emplace_back(data); | ||
| 57 | } | ||
| 58 | |||
| 59 | return result; | ||
| 60 | } | ||
| 61 | |||
| 62 | using action_type = tuple<int, int, string>; // value (0, 1), direction (-1, 1), next_state) | ||
| 63 | using condition_type = array<action_type, 2>; // [0] -> action, [1] -> action | ||
| 64 | using rule_type = map<string, condition_type>; // "A" -> condition | ||
| 65 | using puzzle_type = tuple<string, int, rule_type>; // (start_state, steps, rules) | ||
| 66 | |||
| 67 | puzzle_type | ||
| 68 | parse_input(const vector<vector<vector<string>>>& input) | ||
| 69 | { | ||
| 70 | rule_type rules{}; | ||
| 71 | |||
| 72 | for ( size_t idx = 1; idx < input.size(); ++idx ) { | ||
| 73 | const auto& block = input.at(idx); | ||
| 74 | |||
| 75 | condition_type condition{}; | ||
| 76 | |||
| 77 | for ( size_t i = 0; i != 2; ++i ) { | ||
| 78 | const auto base = i * 4; | ||
| 79 | |||
| 80 | auto read_val = stoul(block.at(base + 1).back()); | ||
| 81 | auto write_val = stoi(block.at(base + 2).back()); | ||
| 82 | auto dir = block.at(base + 3).back() == "left" ? -1 : 1; | ||
| 83 | auto next_state = block.at(base + 4).back(); | ||
| 84 | |||
| 85 | condition.at(read_val) = { write_val, dir, next_state }; | ||
| 86 | } | ||
| 87 | |||
| 88 | rules[block.at(0).back()] = condition; | ||
| 89 | } | ||
| 90 | |||
| 91 | auto start_state = input[0][0][3]; | ||
| 92 | auto steps = stoi(input[0][1][5]); | ||
| 93 | |||
| 94 | return puzzle_type{ start_state, steps, rules }; | ||
| 95 | } | ||
| 96 | |||
| 97 | void | ||
| 98 | part1(const puzzle_type& puzzle) | ||
| 99 | { | ||
| 100 | auto [state, steps, rules] = puzzle; | ||
| 101 | |||
| 102 | map<int, int> tape; | ||
| 103 | |||
| 104 | int pos = 0; | ||
| 105 | |||
| 106 | while ( steps-- > 0 ) { | ||
| 107 | auto value = tape[pos]; | ||
| 108 | const auto& [write, move, next_state] = rules.at(state).at(size_t(value)); | ||
| 109 | state = next_state; | ||
| 110 | tape[pos] = write; | ||
| 111 | pos += move; | ||
| 112 | } | ||
| 113 | |||
| 114 | int sum = 0; | ||
| 115 | for ( const auto& [key, value]: tape ) { | ||
| 116 | sum += value; | ||
| 117 | } | ||
| 118 | cout << "Part1: " << sum << '\n'; | ||
| 119 | } | ||
| 120 | |||
| 121 | } // namespace | ||
| 122 | |||
| 123 | int | ||
| 124 | main() | ||
| 125 | { | ||
| 126 | auto input = read_file("data/day25.txt"); | ||
| 127 | part1(parse_input(input)); | ||
| 128 | } | ||
