diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/day08.cpp | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/src/day08.cpp b/src/day08.cpp new file mode 100644 index 0000000..3e1b6c9 --- /dev/null +++ b/src/day08.cpp | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | #include <array> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <regex> | ||
| 6 | #include <numeric> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | struct Generator { | ||
| 10 | explicit Generator(string_view init) | ||
| 11 | : values_(init) | ||
| 12 | , iter_(values_.begin()) | ||
| 13 | |||
| 14 | { | ||
| 15 | } | ||
| 16 | |||
| 17 | size_t next() | ||
| 18 | { | ||
| 19 | size_t gen = (*iter_ == 'L') ? 0 : 1; | ||
| 20 | |||
| 21 | ++iter_; | ||
| 22 | if ( iter_ == values_.end() ) { | ||
| 23 | iter_ = values_.begin(); | ||
| 24 | } | ||
| 25 | |||
| 26 | return gen; | ||
| 27 | } | ||
| 28 | |||
| 29 | string values_; | ||
| 30 | string::iterator iter_; | ||
| 31 | }; | ||
| 32 | |||
| 33 | void | ||
| 34 | part1() | ||
| 35 | { | ||
| 36 | fstream input{ "data/day08.txt" }; | ||
| 37 | |||
| 38 | string line; | ||
| 39 | getline(input, line); | ||
| 40 | |||
| 41 | Generator generator{ line }; | ||
| 42 | |||
| 43 | map<string, array<string, 2>> puzzle; | ||
| 44 | |||
| 45 | const regex pattern{ R"((.*) = \((.*), (.*)\))" }; | ||
| 46 | while ( getline(input, line) ) { | ||
| 47 | if ( line.empty() ) { | ||
| 48 | continue; | ||
| 49 | } | ||
| 50 | |||
| 51 | smatch sub_match; | ||
| 52 | if ( regex_search(line, sub_match, pattern) ) { | ||
| 53 | puzzle[sub_match[1]] = { sub_match[2], sub_match[3] }; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | auto counter = 0; | ||
| 58 | for ( string str{ "AAA" }; str != "ZZZ"; str = puzzle[str][generator.next()] ) { | ||
| 59 | ++counter; | ||
| 60 | } | ||
| 61 | cout << counter << endl; | ||
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | part2() | ||
| 66 | { | ||
| 67 | fstream input{ "data/day08.txt" }; | ||
| 68 | |||
| 69 | string firstline; | ||
| 70 | getline(input, firstline); | ||
| 71 | |||
| 72 | map<string, array<string, 2>> puzzle; | ||
| 73 | |||
| 74 | const regex pattern{ R"((.*) = \((.*), (.*)\))" }; | ||
| 75 | for ( string line; getline(input, line); ) { | ||
| 76 | if ( line.empty() ) { | ||
| 77 | continue; | ||
| 78 | } | ||
| 79 | |||
| 80 | smatch sub_match; | ||
| 81 | if ( regex_search(line, sub_match, pattern) ) { | ||
| 82 | puzzle[sub_match[1]] = { sub_match[2], sub_match[3] }; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | auto value = 1L; | ||
| 87 | for ( const auto& entry: puzzle ) { | ||
| 88 | if ( entry.first[2] != 'A' ) { | ||
| 89 | continue; | ||
| 90 | } | ||
| 91 | |||
| 92 | Generator generator{ firstline }; | ||
| 93 | |||
| 94 | auto counter = 0L; | ||
| 95 | |||
| 96 | for ( string str{ entry.first }; str[2] != 'Z'; str = puzzle[str][generator.next()] ) { | ||
| 97 | ++counter; | ||
| 98 | } | ||
| 99 | |||
| 100 | value = lcm(value, counter); | ||
| 101 | } | ||
| 102 | cout << value << endl; | ||
| 103 | } | ||
| 104 | |||
| 105 | int | ||
| 106 | main() | ||
| 107 | { | ||
| 108 | part1(); | ||
| 109 | part2(); | ||
| 110 | } | ||
