summaryrefslogtreecommitdiff
path: root/2023/04/src/part1.cpp
diff options
context:
space:
mode:
authorFederico Igne <undyamon@disroot.org>2023-12-05 20:28:31 +0100
committerFederico Igne <undyamon@disroot.org>2023-12-05 20:28:31 +0100
commit8ed68d64a9f49635209c7d6e231c6cc4d3443172 (patch)
tree709775738b4a89f33309a404df390d1ed35d13c9 /2023/04/src/part1.cpp
parentad9cecf233977d8ea4a61abbc4fb9f1a0d9924bc (diff)
downloadaoc-8ed68d64a9f49635209c7d6e231c6cc4d3443172.tar.gz
aoc-8ed68d64a9f49635209c7d6e231c6cc4d3443172.zip
aoc(2304): Scratchcards
Diffstat (limited to '2023/04/src/part1.cpp')
-rw-r--r--2023/04/src/part1.cpp61
1 files changed, 61 insertions, 0 deletions
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 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <fstream>
5#include <vector>
6#include <cmath>
7
8int matchings(const std::string& line, int ws = 10, int ns = 25)
9{
10 int matchings{};
11 std::istringstream in{line};
12
13 int a;
14 std::string skip;
15 in >> skip >> skip;
16
17 std::vector<int> winning(ws);
18 for (int w = 0; w < ws; ++w)
19 {
20 in >> a;
21 winning.push_back(a);
22 }
23
24 in >> skip;
25
26 for (int n = 0; n < ns; ++n)
27 {
28 in >> a;
29 if (std::any_of(winning.cbegin(), winning.cend(), [a](int w){ return w == a; }))
30 {
31 ++matchings;
32 }
33 }
34
35 return matchings;
36}
37
38int main(void)
39{
40 int answer{};
41 std::vector<int> cards;
42
43 std::ifstream input{ "resources/input.txt" };
44 if (input.is_open())
45 {
46 std::string line;
47 while (not std::getline(input,line).eof())
48 {
49 cards.push_back(matchings(line));
50 }
51 }
52 input.close();
53
54 for (int m : cards)
55 {
56 answer += static_cast<int>(std::pow(2, m - 1));
57 }
58
59 std::cout << answer << std::endl;
60 return 0;
61}