summaryrefslogtreecommitdiff
path: root/2023/04/src/part1.cpp
blob: 2c052dc7dc6ce549390132122a4ebc553741e699 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <cmath>

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<int> 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<int> 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<int>(std::pow(2, m - 1));
    }

    std::cout << answer << std::endl;
    return 0;
}