aocpp/2017/01.cpp

56 lines
1.2 KiB
C++
Raw Normal View History

2022-11-28 09:30:34 -08:00
#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);
}
}
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
2022-11-28 09:30:34 -08:00
std::string input;
2023-01-31 08:58:42 -08:00
std::getline(in, input);
2023-01-31 09:15:15 -08:00
out << "Part 1: " << Part1(input) << std::endl;
out << "Part 2: " << Part2(input) << std::endl;
2022-11-28 09:30:34 -08:00
}