2017-05
This commit is contained in:
parent
4f6e7e6e2d
commit
002307a00f
64
2017/05.cpp
Normal file
64
2017/05.cpp
Normal file
|
@ -0,0 +1,64 @@
|
|||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
namespace {
|
||||
|
||||
auto Parse(std::istream & in) -> std::vector<std::int64_t> {
|
||||
std::vector<std::int64_t> result;
|
||||
std::int64_t x;
|
||||
while (in >> x) {
|
||||
result.push_back(x);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
auto Run1(std::vector<std::int64_t> 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<std::int64_t> 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;
|
||||
}
|
|
@ -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<N> & 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user