From 8ed68d64a9f49635209c7d6e231c6cc4d3443172 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 5 Dec 2023 20:28:31 +0100 Subject: aoc(2304): Scratchcards --- 2023/04/src/part2.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2023/04/src/part2.cpp (limited to '2023/04/src/part2.cpp') diff --git a/2023/04/src/part2.cpp b/2023/04/src/part2.cpp new file mode 100644 index 0000000..1e369d8 --- /dev/null +++ b/2023/04/src/part2.cpp @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include +#include + +int matchings(const std::string& line, int ws = 10, int ns = 25) +{ + int matchings{}; + std::istringstream in{line}; + + int a; + std::string skip; + in >> skip >> skip; + + std::vector winning(ws); + for (int w = 0; w < ws; ++w) + { + in >> a; + winning.push_back(a); + } + + in >> skip; + + for (int n = 0; n < ns; ++n) + { + in >> a; + if (std::any_of(winning.cbegin(), winning.cend(), [a](int w){ return w == a; })) + { + ++matchings; + } + } + + return matchings; +} + +int main(void) +{ + int answer{}; + std::vector cards; + + std::ifstream input{ "resources/input.txt" }; + if (input.is_open()) + { + std::string line; + while (not std::getline(input,line).eof()) + { + cards.push_back(matchings(line)); + } + } + input.close(); + + std::vector mult(cards.size(), 1); + for (int a = 0; a < cards.size(); ++a) + { + for (int b = 1; b <= cards[a] and a + b < cards.size(); ++b) + { + mult[a + b] += mult[a]; + } + answer += mult[a]; + } + + std::cout << answer << std::endl; + return 0; +} -- cgit v1.2.3