diff options
| author | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2025-11-02 21:41:58 +0100 |
| commit | 756f22d58bb198b8f34589c112e1003614ccdcd6 (patch) | |
| tree | 4cdeba335f1ea67f7ead9e9f763ba1c3c206fc4d /2017/src/day06.cpp | |
| parent | fcbb722bd4c5ca2bd34a7cf9c0ba48f2f955da13 (diff) | |
| download | advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.gz advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.tar.bz2 advent-of-code-756f22d58bb198b8f34589c112e1003614ccdcd6.zip | |
aoc 2017, days 1-20
Diffstat (limited to '2017/src/day06.cpp')
| -rw-r--r-- | 2017/src/day06.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/2017/src/day06.cpp b/2017/src/day06.cpp new file mode 100644 index 0000000..596ca2c --- /dev/null +++ b/2017/src/day06.cpp | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | #include <filesystem> | ||
| 2 | #include <fstream> | ||
| 3 | #include <iostream> | ||
| 4 | #include <map> | ||
| 5 | #include <set> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | namespace { | ||
| 11 | |||
| 12 | vector<int> | ||
| 13 | read_lines(const filesystem::path& filename) | ||
| 14 | { | ||
| 15 | ifstream file{ filename }; | ||
| 16 | vector<int> lines; | ||
| 17 | |||
| 18 | for ( int value{}; file >> value; ) { | ||
| 19 | lines.emplace_back(value); | ||
| 20 | } | ||
| 21 | |||
| 22 | return lines; | ||
| 23 | } | ||
| 24 | |||
| 25 | void | ||
| 26 | distribute(vector<int>& values) | ||
| 27 | { | ||
| 28 | auto iter = ranges::max_element(values); | ||
| 29 | auto max_value = *iter; | ||
| 30 | |||
| 31 | *iter = 0; | ||
| 32 | |||
| 33 | while ( max_value-- > 0 ) { | ||
| 34 | ++iter; | ||
| 35 | if ( iter == values.end() ) { | ||
| 36 | iter = values.begin(); | ||
| 37 | } | ||
| 38 | *iter += 1; | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | void | ||
| 43 | part1(vector<int> values) | ||
| 44 | { | ||
| 45 | set<vector<int>> seen; | ||
| 46 | for ( int step = 0;; ++step ) { | ||
| 47 | if ( seen.contains(values) ) { | ||
| 48 | cout << "Part1: " << step << '\n'; | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | seen.insert(values); | ||
| 53 | |||
| 54 | distribute(values); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | void | ||
| 59 | part2(vector<int> values) | ||
| 60 | { | ||
| 61 | map<vector<int>, int> seen; | ||
| 62 | for ( int step = 0;; ++step ) { | ||
| 63 | if ( seen.contains(values) ) { | ||
| 64 | cout << "Part2: " << step - seen.at(values) << '\n'; | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | seen.emplace(values, step); | ||
| 69 | |||
| 70 | distribute(values); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | } // namespace | ||
| 75 | |||
| 76 | int | ||
| 77 | main() | ||
| 78 | { | ||
| 79 | auto values = read_lines("data/day06.txt"); | ||
| 80 | part1(values); | ||
| 81 | part2(values); | ||
| 82 | } | ||
