aocpp/2022/06.cpp

67 lines
1.6 KiB
C++
Raw Permalink Normal View History

2023-01-23 19:37:02 -08:00
#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <stdexcept>
#include <string>
#include <limits>
2023-01-25 13:48:54 -08:00
#include <boost/range/irange.hpp>
2023-01-23 19:37:02 -08:00
#include <doctest.h>
#include <aocpp/Startup.hpp>
namespace {
auto Solve(std::string const& input, std::size_t n) -> std::size_t
{
std::array<std::size_t, 256> counts {};
std::size_t uniques {0};
auto const inc = [&](char c) {
2023-01-24 09:01:42 -08:00
uniques += 1 == ++counts[static_cast<std::uint8_t>(c)];
2023-01-23 19:37:02 -08:00
};
auto const dec = [&](char c) {
2023-01-24 09:01:42 -08:00
uniques -= 0 == --counts[static_cast<std::uint8_t>(c)];
2023-01-23 19:37:02 -08:00
};
2023-01-25 13:48:54 -08:00
for (auto const i : boost::irange(n)) {
2023-01-23 19:37:02 -08:00
inc(input[i]);
}
2023-01-25 13:48:54 -08:00
for (auto const i : boost::irange(n, input.size())) {
2023-01-23 19:37:02 -08:00
dec(input[i-n]);
inc(input[i]);
if (uniques == n) { return i+1; }
}
throw std::runtime_error{"bad input"};
}
} // namespace
TEST_SUITE("2022-06 examples") {
TEST_CASE("examples") {
CHECK(Solve("bvwbjplbgvbhsrlpgdmjqwftvncz", 4) == 5);
CHECK(Solve("nppdvjthqldpwncqszvftbrmjlhg", 4) == 6);
CHECK(Solve("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 4) == 10);
CHECK(Solve("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 4) == 11);
CHECK(Solve("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 14) == 19);
CHECK(Solve("bvwbjplbgvbhsrlpgdmjqwftvncz", 14) == 23);
CHECK(Solve("nppdvjthqldpwncqszvftbrmjlhg", 14) == 23);
CHECK(Solve("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 14) == 29);
CHECK(Solve("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 14) == 26);
}
}
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
{
2023-01-23 19:37:02 -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: " << Solve(input, 4) << std::endl;
out << "Part 2: " << Solve(input, 14) << std::endl;
2023-01-23 19:37:02 -08:00
}