use a counter

This commit is contained in:
Eric Mertens 2023-12-04 15:45:17 -08:00
parent 410790fd73
commit d66b3d3561
2 changed files with 33 additions and 3 deletions

View File

@ -12,6 +12,7 @@
#include <aocpp/Parsing.hpp>
#include <aocpp/Startup.hpp>
#include <aocpp/Counter.hpp>
namespace {
@ -47,11 +48,10 @@ auto CountWins(std::vector<Card>& cards) -> std::vector<std::size_t>
for (auto& card : cards)
{
std::vector<unsigned long long> xs;
std::sort(card.mine.begin(), card.mine.end());
std::sort(card.winners.begin(), card.winners.end());
std::set_intersection(card.mine.begin(), card.mine.end(), card.winners.begin(), card.winners.end(), std::back_inserter(xs));
result.push_back(xs.size());
auto const c = std::set_intersection(card.mine.begin(), card.mine.end(), card.winners.begin(), card.winners.end(), Counter<std::size_t>{});
result.push_back(c);
}
return result;

View File

@ -0,0 +1,30 @@
#pragma once
#include <cstdint>
#include <iterator>
template <typename T>
struct Counter
{
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator_category = std::output_iterator_tag;
struct EmptyRef {
auto operator=(T const&) const -> void { }
};
std::size_t n;
auto operator++() -> Counter& { n++; return *this; }
auto operator++(int) -> Counter { Counter temp; n++; return temp; }
auto operator*() const -> EmptyRef { return EmptyRef{}; }
auto operator-(Counter const& rhs) const -> std::ptrdiff_t {
return std::ptrdiff_t{n} - std::ptrdiff_t{rhs.n};
}
operator std::size_t() const { return n; }
};
static_assert(std::output_iterator<Counter<int>, int>);