-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.cpp
68 lines (58 loc) · 1.29 KB
/
day4.cpp
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
62
63
64
65
66
67
68
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <math.h>
#include "Helpers/HelperFunctions.h"
int main() {
cout << "Day 4" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2023\inputs\day4.txt)");
string str;
vector<string> cards;
map<int, int> winnings;
int x = 1;
while (getline(file, str)) {
cout << str << endl;
cards.push_back(str);
winnings[x] = 1;
x++;
}
int sum = 0;
int instances = 0;
x = 1;
for (auto &card : cards) {
vector<string> out;
tokenize(card, ' ', out);
vector<int> nums;
vector<int> winning;
int i = 2;
while (out[i] != "|") {
nums.push_back(stoi(out[i]));
i++;
}
i++;
while (i < out.size()) {
winning.push_back(stoi(out[i]));
i++;
}
int total = 0;
for (auto &num : nums) {
if (std::find(winning.begin(), winning.end(), num) != winning.end()) {
total++;
}
}
if (total > 0) {
sum += pow(2, total-1);
}
for (int j = x+1; j <= x+total; j++) {
winnings[j] += winnings[x];
}
instances += winnings[x];
x++;
}
cout << "Part 1: " << sum << endl;
cout << "Part 2: " << instances << endl;
return sum;
}