Alias RangeSpec in part 1

master
Nick Krichevsky 2020-12-16 01:34:43 -05:00
parent 8857dcac5c
commit 3bc3d81cca
1 changed files with 12 additions and 13 deletions

View File

@ -12,17 +12,20 @@ constexpr auto NEARBY_TICKETS_HEADER = "nearby tickets:";
constexpr auto YOUR_TICKET_HEADER = "your ticket:"; constexpr auto YOUR_TICKET_HEADER = "your ticket:";
constexpr auto RANGE_PATTERN = R"((\d+)-(\d+) or (\d+)-(\d+))"; constexpr auto RANGE_PATTERN = R"((\d+)-(\d+) or (\d+)-(\d+))";
// This has to be the grossest type signature I've written in a while, but it's of
// a-b or c-d maps to pair<a, b> or pair<c ,d>
using RangeSpec = std::pair<std::pair<int, int>, std::pair<int, int>>;
class TicketSpec { class TicketSpec {
public: public:
TicketSpec( TicketSpec(
std::vector<int> &ourTicket, std::vector<std::vector<int>> &otherTickets, std::vector<int> &ourTicket, std::vector<std::vector<int>> &otherTickets, std::vector<RangeSpec> &fieldRanges)
std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> &fieldRanges)
: ourTicket(ourTicket), otherTickets(otherTickets), fieldRanges(fieldRanges) { : ourTicket(ourTicket), otherTickets(otherTickets), fieldRanges(fieldRanges) {
} }
TicketSpec( TicketSpec(
std::vector<int> &&ourTicket, std::vector<std::vector<int>> &&otherTickets, std::vector<int> &&ourTicket, std::vector<std::vector<int>> &&otherTickets,
std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> &&fieldRanges) std::vector<RangeSpec> &&fieldRanges)
: ourTicket(std::move(ourTicket)), otherTickets(std::move(otherTickets)), fieldRanges(std::move(fieldRanges)) { : ourTicket(std::move(ourTicket)), otherTickets(std::move(otherTickets)), fieldRanges(std::move(fieldRanges)) {
} }
@ -34,16 +37,14 @@ class TicketSpec {
return this->otherTickets; return this->otherTickets;
} }
const std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> &getFieldRanges() const { const std::vector<RangeSpec> &getFieldRanges() const {
return this->fieldRanges; return this->fieldRanges;
} }
private: private:
std::vector<int> ourTicket; std::vector<int> ourTicket;
std::vector<std::vector<int>> otherTickets; std::vector<std::vector<int>> otherTickets;
// This has to be the grossest type signature I've written in a while, but it's of std::vector<RangeSpec> fieldRanges;
// a-b or c-d maps to pair<a, b> or pair<c ,d>
std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> fieldRanges;
}; };
std::vector<std::string> readInput(const std::string &filename) { std::vector<std::string> readInput(const std::string &filename) {
@ -58,8 +59,8 @@ std::vector<std::string> readInput(const std::string &filename) {
} }
template <typename Iter> template <typename Iter>
std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> findPairs(Iter start, Iter end) { std::vector<RangeSpec> findRanges(Iter start, Iter end) {
std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> pairs; std::vector<RangeSpec> pairs;
std::regex rangeExpression(RANGE_PATTERN); std::regex rangeExpression(RANGE_PATTERN);
for (auto textIter = start; textIter != end; ++textIter) { for (auto textIter = start; textIter != end; ++textIter) {
std::smatch matches; std::smatch matches;
@ -96,7 +97,7 @@ TicketSpec parseInput(const std::vector<std::string> &input) {
auto ruleEnd = std::find(input.cbegin(), input.cend(), ""); auto ruleEnd = std::find(input.cbegin(), input.cend(), "");
auto yourTicketBegin = std::find(input.cbegin(), input.cend(), YOUR_TICKET_HEADER); auto yourTicketBegin = std::find(input.cbegin(), input.cend(), YOUR_TICKET_HEADER);
auto nearbyTicketsBegin = std::find(input.cbegin(), input.cend(), NEARBY_TICKETS_HEADER); auto nearbyTicketsBegin = std::find(input.cbegin(), input.cend(), NEARBY_TICKETS_HEADER);
auto pairs = findPairs(input.begin(), ruleEnd); auto pairs = findRanges(input.begin(), ruleEnd);
auto nearbyTickets = parseNearbyTickets(nearbyTicketsBegin + 1, input.cend()); auto nearbyTickets = parseNearbyTickets(nearbyTicketsBegin + 1, input.cend());
auto yourTicket = parseTicket(*(yourTicketBegin + 1)); auto yourTicket = parseTicket(*(yourTicketBegin + 1));
@ -105,9 +106,7 @@ TicketSpec parseInput(const std::vector<std::string> &input) {
bool inRangesForTicket(int value, const TicketSpec &ticketSpec) { bool inRangesForTicket(int value, const TicketSpec &ticketSpec) {
return std::any_of( return std::any_of(
ticketSpec.getFieldRanges().cbegin(), ticketSpec.getFieldRanges().cbegin(), ticketSpec.getFieldRanges().cend(), [value](const RangeSpec &ranges) {
ticketSpec.getFieldRanges().cend(),
[value](const std::pair<std::pair<int, int>, std::pair<int, int>> &ranges) {
auto range1 = ranges.first; auto range1 = ranges.first;
auto range2 = ranges.second; auto range2 = ranges.second;
return (value >= range1.first && value <= range1.second) || return (value >= range1.first && value <= range1.second) ||