diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-10-18 10:38:00 +0200 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-10-18 10:38:00 +0200 |
| commit | 22f96da42562d1f9ab3e5fd1715a6158ebebf3a2 (patch) | |
| tree | c2f3a8f52f4cb68dc7dbdf4b83dd7fa09f5602a1 /2016/src/day15.cpp | |
| parent | 1d47028997c893535a2fd9064812a4a7cddaf5f6 (diff) | |
| download | advent-of-code-22f96da42562d1f9ab3e5fd1715a6158ebebf3a2.tar.gz advent-of-code-22f96da42562d1f9ab3e5fd1715a6158ebebf3a2.tar.bz2 advent-of-code-22f96da42562d1f9ab3e5fd1715a6158ebebf3a2.zip | |
day 15, aoc 2016
Diffstat (limited to '2016/src/day15.cpp')
| -rw-r--r-- | 2016/src/day15.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/2016/src/day15.cpp b/2016/src/day15.cpp new file mode 100644 index 0000000..ced6102 --- /dev/null +++ b/2016/src/day15.cpp | |||
| @@ -0,0 +1,89 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <filesystem> | ||
| 3 | #include <fstream> | ||
| 4 | #include <iostream> | ||
| 5 | #include <regex> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | struct Disc { | ||
| 14 | [[nodiscard]] | ||
| 15 | bool zeroAtTime(int time) const | ||
| 16 | { | ||
| 17 | return (time + num + phase) % positions == 0; | ||
| 18 | } | ||
| 19 | |||
| 20 | int num; | ||
| 21 | int phase; | ||
| 22 | int positions; | ||
| 23 | }; | ||
| 24 | |||
| 25 | vector<int> | ||
| 26 | extractNumbers(const string& text) | ||
| 27 | { | ||
| 28 | static const regex number_regex("\\d+"); | ||
| 29 | |||
| 30 | vector<int> numbers; | ||
| 31 | smatch match; | ||
| 32 | |||
| 33 | auto searchStart = text.cbegin(); | ||
| 34 | while ( regex_search(searchStart, text.cend(), match, number_regex) ) { | ||
| 35 | numbers.emplace_back(stoi(match.str())); | ||
| 36 | searchStart = match.suffix().first; | ||
| 37 | } | ||
| 38 | |||
| 39 | return numbers; | ||
| 40 | } | ||
| 41 | |||
| 42 | vector<Disc> | ||
| 43 | readFile(const filesystem::path& filename) | ||
| 44 | { | ||
| 45 | ifstream file{ filename }; | ||
| 46 | vector<Disc> discs; | ||
| 47 | |||
| 48 | for ( string line; getline(file, line); ) { | ||
| 49 | auto numbers = extractNumbers(line); | ||
| 50 | discs.emplace_back(numbers.at(0), numbers.at(3), numbers.at(1)); | ||
| 51 | } | ||
| 52 | |||
| 53 | return discs; | ||
| 54 | } | ||
| 55 | |||
| 56 | void | ||
| 57 | solve(const vector<Disc>& discs) | ||
| 58 | { | ||
| 59 | for ( int time = 0;; ++time ) { | ||
| 60 | auto found = ranges::all_of(discs, [&time](const auto& disc) { return disc.zeroAtTime(time); }); | ||
| 61 | if ( found ) { | ||
| 62 | cout << time << '\n'; | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | void | ||
| 69 | part1(const vector<Disc>& discs) | ||
| 70 | { | ||
| 71 | solve(discs); | ||
| 72 | } | ||
| 73 | |||
| 74 | void | ||
| 75 | part2(vector<Disc> discs) | ||
| 76 | { | ||
| 77 | discs.emplace_back(discs.size() + 1, 0, 11); | ||
| 78 | solve(discs); | ||
| 79 | } | ||
| 80 | |||
| 81 | } // namespace | ||
| 82 | |||
| 83 | int | ||
| 84 | main() | ||
| 85 | { | ||
| 86 | auto discs = readFile("data/day15.txt"); | ||
| 87 | part1(discs); | ||
| 88 | part2(discs); | ||
| 89 | } | ||
