use a counter
This commit is contained in:
parent
410790fd73
commit
d66b3d3561
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
#include <aocpp/Parsing.hpp>
|
#include <aocpp/Parsing.hpp>
|
||||||
#include <aocpp/Startup.hpp>
|
#include <aocpp/Startup.hpp>
|
||||||
|
#include <aocpp/Counter.hpp>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
@ -47,11 +48,10 @@ auto CountWins(std::vector<Card>& cards) -> std::vector<std::size_t>
|
||||||
|
|
||||||
for (auto& card : cards)
|
for (auto& card : cards)
|
||||||
{
|
{
|
||||||
std::vector<unsigned long long> xs;
|
|
||||||
std::sort(card.mine.begin(), card.mine.end());
|
std::sort(card.mine.begin(), card.mine.end());
|
||||||
std::sort(card.winners.begin(), card.winners.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));
|
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(xs.size());
|
result.push_back(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
30
lib/include/aocpp/Counter.hpp
Normal file
30
lib/include/aocpp/Counter.hpp
Normal 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>);
|
Loading…
Reference in New Issue
Block a user