diff --git a/2017/puzzle-17-01.cc b/2017/puzzle-17-01.cc new file mode 100644 index 0000000..e94fd0a --- /dev/null +++ b/2017/puzzle-17-01.cc @@ -0,0 +1,36 @@ +#include +#include +#include + +using UIntVector = std::vector; + +auto rotate(UIntVector const& vec, unsigned long amt) +{ + amt %= vec.size(); + UIntVector result; + result.reserve(vec.size()); + auto inserter{std::back_inserter(result)}; + std::copy(vec.begin() + amt, vec.end(), inserter); + std::copy(vec.begin(), vec.begin() + amt, inserter); + return result; +} + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return 1; + } + + auto step{std::stoul(line)}; + + UIntVector result; + result.push_back(0); + for (unsigned i{1}; i < 2018; ++i) { + result = rotate(result, step); + result.push_back(i); + } + std::cout << "Result " << result.front() << '\n'; + return 0; +} diff --git a/2017/puzzle-17-02.cc b/2017/puzzle-17-02.cc new file mode 100644 index 0000000..bae8185 --- /dev/null +++ b/2017/puzzle-17-02.cc @@ -0,0 +1,32 @@ +#include +#include +#include + +using UIntVector = std::vector; + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line.\n"; + return 1; + } + + auto step{std::stoul(line)}; + + unsigned pos{0}; + unsigned after_zero{0}; + for (unsigned i{1}; i < 50'000'001; ++i) { + /* Currently we have at position pos of a circular list of length i. + * We want to move `step` steps round the circular list. + */ + pos = (pos + step) % i; + if (pos == 0) { + after_zero = i; + } + /* Current position is moved on one. */ + ++pos; + } + std::cout << "Result " << after_zero << '\n'; + return 0; +}