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/part1.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 2023/04/src/part1.cpp (limited to '2023/04/src/part1.cpp') diff --git a/2023/04/src/part1.cpp b/2023/04/src/part1.cpp new file mode 100644 index 0000000..2c052dc --- /dev/null +++ b/2023/04/src/part1.cpp @@ -0,0 +1,61 @@ +#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(); + + for (int m : cards) + { + answer += static_cast(std::pow(2, m - 1)); + } + + std::cout << answer << std::endl; + return 0; +} -- cgit v1.2.3