diff --git a/2017/05.cpp b/2017/05.cpp new file mode 100644 index 0000000..be2b288 --- /dev/null +++ b/2017/05.cpp @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include + +namespace { + +auto Parse(std::istream & in) -> std::vector { + std::vector result; + std::int64_t x; + while (in >> x) { + result.push_back(x); + } + return result; +} + +auto Run1(std::vector program) -> std::size_t { + std::int64_t cursor = 0; + std::size_t n = 0; + + while (0 <= cursor && cursor < program.size()) { + cursor += program[cursor]++; + n++; + } + + return n; +} + +auto Run2(std::vector program) -> std::size_t { + std::int64_t cursor = 0; + std::size_t n = 0; + + while (0 <= cursor && cursor < program.size()) { + auto & p = program[cursor]; + cursor += p; + p += p >= 3 ? -1 : 1; + n++; + } + + return n; +} + +} // namespace + +TEST_SUITE("2017-05 examples") { + TEST_CASE("example") { + std::istringstream in { "0\n3\n0\n1\n-3\n" }; + auto input = Parse(in); + CHECK(Run1(input) == 5); + CHECK(Run2(input) == 10); + } +} + +auto main(int argc, char** argv) -> int { + auto input = Parse(*aocpp::Startup(argc, argv)); + std::cout << "Part 1: " << Run1(input) << std::endl; + std::cout << "Part 2: " << Run2(std::move(input)) << std::endl; +} diff --git a/2017/06.cpp b/2017/06.cpp index e868b3a..f4db489 100644 --- a/2017/06.cpp +++ b/2017/06.cpp @@ -22,7 +22,7 @@ auto Parse(std::istream & in) -> Banks<16> { throw std::runtime_error{"bad input"}; } } - + return result; } @@ -58,7 +58,7 @@ auto CycleSearch(Banks & banks) TEST_SUITE("2017-06 examples") { TEST_CASE("example") { Banks<4> banks {0, 2, 7, 0}; - + SUBCASE("single step") { Redistribute(banks); REQUIRE(banks == Banks<4>{2, 4, 1, 2}); @@ -85,4 +85,4 @@ auto main(int argc, char** argv) -> int { auto answer = CycleSearch(input); std::cout << "Part 1: " << answer.first << std::endl; std::cout << "Part 2: " << answer.second << std::endl; -} \ No newline at end of file +} diff --git a/2017/CMakeLists.txt b/2017/CMakeLists.txt index 8bd0168..50d0749 100644 --- a/2017/CMakeLists.txt +++ b/2017/CMakeLists.txt @@ -1,3 +1,6 @@ +add_executable(2017_05 05.cpp) +target_link_libraries(2017_05 aocpp) + add_executable(2017_06 06.cpp) target_link_libraries(2017_06 aocpp)