aocpp/2015/10.cpp

54 lines
1.2 KiB
C++
Raw Permalink Normal View History

2024-08-27 14:51:52 -07:00
#include <aocpp/Startup.hpp>
#include <doctest.h>
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <stdexcept>
#include <string_view>
auto seesay(std::string_view const input) -> std::string
{
std::string output;
for (auto cursor = begin(input); cursor != end(input);)
{
auto const c = *cursor;
auto const cursor_ = std::find_if(std::next(cursor), end(input), [c](auto const x) { return x != c; });
auto const count = std::distance(cursor, cursor_);
(output += std::to_string(count)) += c;
cursor = cursor_;
}
return output;
}
TEST_SUITE("documented examples") {
using namespace std::string_view_literals;
TEST_CASE("unit tests") {
REQUIRE_EQ(seesay("1"sv), "11"sv);
REQUIRE_EQ(seesay("11"sv), "21"sv);
REQUIRE_EQ(seesay("21"sv), "1211"sv);
REQUIRE_EQ(seesay("1211"sv), "111221"sv);
REQUIRE_EQ(seesay("111221"sv), "312211"sv);
}
}
auto Main(std::istream &in, std::ostream &out) -> void
{
std::string line;
std::size_t part1 = 0, part2 = 0;
std::getline(in, line);
for (int i = 0; i < 40; i++) {
line = seesay(line);
}
out << "Part 1: " << line.size() << std::endl;
for (int i = 0; i < 10; i++) {
line = seesay(line);
}
out << "Part 2: " << line.size() << std::endl;
}