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/day17.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/day17.cpp')
| -rw-r--r-- | 2017/src/day17.cpp | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/2017/src/day17.cpp b/2017/src/day17.cpp new file mode 100644 index 0000000..6cf498b --- /dev/null +++ b/2017/src/day17.cpp | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <vector> | ||
| 3 | |||
| 4 | using namespace std; | ||
| 5 | |||
| 6 | namespace { | ||
| 7 | |||
| 8 | void | ||
| 9 | part1(size_t steps, size_t iterations) | ||
| 10 | { | ||
| 11 | vector<size_t> buffer{ 0 }; | ||
| 12 | size_t pos = 0; | ||
| 13 | |||
| 14 | for ( size_t i = 1; i <= iterations; ++i ) { | ||
| 15 | pos = (pos + steps) % i + 1; | ||
| 16 | buffer.insert(buffer.begin() + static_cast<vector<size_t>::difference_type>(pos), i); | ||
| 17 | } | ||
| 18 | |||
| 19 | cout << "Part1: " << buffer[(pos + 1) % buffer.size()] << '\n'; | ||
| 20 | } | ||
| 21 | |||
| 22 | void | ||
| 23 | part2(size_t steps, size_t iterations) | ||
| 24 | { | ||
| 25 | size_t pos = 0; | ||
| 26 | size_t value = 0; | ||
| 27 | |||
| 28 | for ( size_t i = 1; i <= iterations; ++i ) { | ||
| 29 | pos = (pos + steps) % i; | ||
| 30 | if ( pos == 0 ) { | ||
| 31 | value = i; | ||
| 32 | } | ||
| 33 | pos = (pos + 1) % (i + 1); | ||
| 34 | } | ||
| 35 | |||
| 36 | cout << "Part2: " << value << '\n'; | ||
| 37 | } | ||
| 38 | |||
| 39 | } // namespace | ||
| 40 | |||
| 41 | int | ||
| 42 | main() | ||
| 43 | { | ||
| 44 | const auto input = 371; | ||
| 45 | part1(input, 2017); | ||
| 46 | part2(input, 50'000'000); | ||
| 47 | } | ||
