From 3bc3d81cca6fab3166ca50d117ba3e130549c778 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Wed, 16 Dec 2020 01:34:43 -0500 Subject: [PATCH] Alias RangeSpec in part 1 --- day16/day16.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/day16/day16.cpp b/day16/day16.cpp index acc81cb..7f3d8fe 100644 --- a/day16/day16.cpp +++ b/day16/day16.cpp @@ -12,17 +12,20 @@ constexpr auto NEARBY_TICKETS_HEADER = "nearby tickets:"; constexpr auto YOUR_TICKET_HEADER = "your ticket:"; 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 or pair +using RangeSpec = std::pair, std::pair>; + class TicketSpec { public: TicketSpec( - std::vector &ourTicket, std::vector> &otherTickets, - std::vector, std::pair>> &fieldRanges) + std::vector &ourTicket, std::vector> &otherTickets, std::vector &fieldRanges) : ourTicket(ourTicket), otherTickets(otherTickets), fieldRanges(fieldRanges) { } TicketSpec( std::vector &&ourTicket, std::vector> &&otherTickets, - std::vector, std::pair>> &&fieldRanges) + std::vector &&fieldRanges) : ourTicket(std::move(ourTicket)), otherTickets(std::move(otherTickets)), fieldRanges(std::move(fieldRanges)) { } @@ -34,16 +37,14 @@ class TicketSpec { return this->otherTickets; } - const std::vector, std::pair>> &getFieldRanges() const { + const std::vector &getFieldRanges() const { return this->fieldRanges; } private: std::vector ourTicket; std::vector> otherTickets; - // 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 or pair - std::vector, std::pair>> fieldRanges; + std::vector fieldRanges; }; std::vector readInput(const std::string &filename) { @@ -58,8 +59,8 @@ std::vector readInput(const std::string &filename) { } template -std::vector, std::pair>> findPairs(Iter start, Iter end) { - std::vector, std::pair>> pairs; +std::vector findRanges(Iter start, Iter end) { + std::vector pairs; std::regex rangeExpression(RANGE_PATTERN); for (auto textIter = start; textIter != end; ++textIter) { std::smatch matches; @@ -96,7 +97,7 @@ TicketSpec parseInput(const std::vector &input) { auto ruleEnd = std::find(input.cbegin(), input.cend(), ""); auto yourTicketBegin = std::find(input.cbegin(), input.cend(), YOUR_TICKET_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 yourTicket = parseTicket(*(yourTicketBegin + 1)); @@ -105,9 +106,7 @@ TicketSpec parseInput(const std::vector &input) { bool inRangesForTicket(int value, const TicketSpec &ticketSpec) { return std::any_of( - ticketSpec.getFieldRanges().cbegin(), - ticketSpec.getFieldRanges().cend(), - [value](const std::pair, std::pair> &ranges) { + ticketSpec.getFieldRanges().cbegin(), ticketSpec.getFieldRanges().cend(), [value](const RangeSpec &ranges) { auto range1 = ranges.first; auto range2 = ranges.second; return (value >= range1.first && value <= range1.second) ||