56 lines
1.2 KiB
C++
56 lines
1.2 KiB
C++
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include <doctest.h>
|
|
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
namespace {
|
|
|
|
auto Captcha(std::string const& input, std::size_t offset) -> std::int64_t {
|
|
std::int64_t acc = 0;
|
|
std::size_t const n = input.size();
|
|
for (std::size_t i = 0; i < n; ++i) {
|
|
if (input[i] == input[(i+offset)%n]) {
|
|
acc += input[i] - '0';
|
|
}
|
|
}
|
|
return acc;
|
|
}
|
|
|
|
auto Part1(std::string const& input) -> std::int64_t {
|
|
return Captcha(input, 1);
|
|
}
|
|
|
|
auto Part2(std::string const& input) -> std::int64_t {
|
|
return Captcha(input, input.size() / 2);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_SUITE("2017-01 examples") {
|
|
TEST_CASE("part 1") {
|
|
CHECK(Part1("1122") == 3);
|
|
CHECK(Part1("1111") == 4);
|
|
CHECK(Part1("1234") == 0);
|
|
CHECK(Part1("91212129") == 9);
|
|
}
|
|
TEST_CASE("part 2") {
|
|
CHECK(Part2("1212") == 6);
|
|
CHECK(Part2("1221") == 0);
|
|
CHECK(Part2("123425") == 4);
|
|
CHECK(Part2("123123") == 12);
|
|
CHECK(Part2("12131415") == 4);
|
|
}
|
|
}
|
|
|
|
auto Main(std::istream & in, std::ostream & out) -> void
|
|
{
|
|
std::string input;
|
|
std::getline(in, input);
|
|
out << "Part 1: " << Part1(input) << std::endl;
|
|
out << "Part 2: " << Part2(input) << std::endl;
|
|
}
|