Created the advent of code day 4 challenge
This commit is contained in:
parent
ba5e11956a
commit
0f99dca6e1
67
aoc/04.cpp
Normal file
67
aoc/04.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Advent of Code 2023, Day 4
|
||||
|
||||
int main() {
|
||||
ifstream input("input.txt");
|
||||
|
||||
int sums;
|
||||
for (string line; getline(input, line);) {
|
||||
regex e("Card\\s*[0-9]+: (.*)");
|
||||
smatch m;
|
||||
|
||||
if (!regex_search(line, m, e) && m.size() <= 1) continue;
|
||||
|
||||
string set = m.str(1) + " ";
|
||||
|
||||
string currentNumber;
|
||||
vector<int> winning;
|
||||
vector<int> guesses;
|
||||
|
||||
bool buildMode = false;
|
||||
bool guessing = false;
|
||||
for (char current : set) {
|
||||
if (current == '|') {
|
||||
guessing = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isdigit(current)) {
|
||||
currentNumber.push_back(current);
|
||||
buildMode = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (buildMode && current == ' ') {
|
||||
if (guessing) {
|
||||
guesses.push_back(stoi(currentNumber));
|
||||
} else {
|
||||
winning.push_back(stoi(currentNumber));
|
||||
}
|
||||
currentNumber = "";
|
||||
buildMode = false;
|
||||
}
|
||||
}
|
||||
|
||||
int winningNums = 0;
|
||||
for (auto item : winning) {
|
||||
if (std::find(guesses.begin(), guesses.end(), item) != guesses.end()) {
|
||||
if (winningNums == 0) {
|
||||
winningNums = 1;
|
||||
continue;
|
||||
}
|
||||
winningNums *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
cout << line << " [" << winningNums << "]" << endl;
|
||||
|
||||
sums += winningNums;
|
||||
}
|
||||
|
||||
cout << endl << "Sum: " << sums;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user