Finished part 1 day 4, after a night of bugs and a morning of wonder
This commit is contained in:
144
advent_of_code/d4/main.cpp
Normal file
144
advent_of_code/d4/main.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string FILE_NAME = "4.txt";
|
||||
|
||||
struct Card {
|
||||
map<int, pair<int, int>> numbers = {};
|
||||
array<array<bool, 5>, 5> opens;
|
||||
|
||||
Card(){
|
||||
for(int x = 0; x < 5; ++x){
|
||||
opens[x].fill(true);
|
||||
}
|
||||
}
|
||||
|
||||
bool check(pair<int, int> coords){
|
||||
bool x_result = true, y_result = true;
|
||||
|
||||
for(int x = 0; x < 5; ++x){
|
||||
if(opens[x][coords.second]){
|
||||
x_result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(int y = 0; y < 5; ++y){
|
||||
if(opens[coords.first][y]){
|
||||
y_result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return x_result || y_result;
|
||||
}
|
||||
|
||||
void insert(int n, pair<int, int> coords){
|
||||
numbers[n] = coords;
|
||||
}
|
||||
|
||||
int score(){
|
||||
int result = 0;
|
||||
|
||||
for(auto n : numbers){
|
||||
auto coords = n.second;
|
||||
|
||||
if(opens[coords.first][coords.second]){
|
||||
result += n.first;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool call(int n){
|
||||
bool result = false;
|
||||
|
||||
if(numbers.find(n) != numbers.end()){
|
||||
auto coords = numbers[n];
|
||||
opens[coords.first][coords.second] = false;
|
||||
|
||||
result = check(coords);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
vector<int> string_to_int_vector(const string& s){
|
||||
vector<int> result;
|
||||
int carry = 0;
|
||||
|
||||
for(auto c : s){
|
||||
if(c >= 48 && c <= 57){
|
||||
carry *= 10;
|
||||
carry += c - 48;
|
||||
} else{
|
||||
result.push_back(carry);
|
||||
carry = 0;
|
||||
}
|
||||
}
|
||||
result.push_back(carry);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
ios::sync_with_stdio(0);
|
||||
cin.tie(0);
|
||||
|
||||
ifstream file_stream;
|
||||
file_stream.open(FILE_NAME);
|
||||
|
||||
if(!file_stream.is_open()){
|
||||
cerr << "File " << FILE_NAME << " not opened correctly\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
// the first line will always be the sequence of bingo calls
|
||||
string line_1;
|
||||
getline(file_stream, line_1);
|
||||
|
||||
auto calls = string_to_int_vector(line_1);
|
||||
|
||||
// The second line will always be empty and not parsed or whatever
|
||||
getline(file_stream, line_1);
|
||||
|
||||
vector<Card> bingo_cards;
|
||||
string cur_line;
|
||||
|
||||
while(getline(file_stream, cur_line)){
|
||||
Card card;
|
||||
|
||||
for(int i = 0; i < 5; ++i){
|
||||
istringstream ss(cur_line);
|
||||
|
||||
for(int j = 0; j < 5; ++j){
|
||||
int a;
|
||||
ss >> a;
|
||||
|
||||
card.insert(a, {i, j});
|
||||
}
|
||||
|
||||
getline(file_stream, cur_line);
|
||||
}
|
||||
|
||||
bingo_cards.push_back(card);
|
||||
}
|
||||
|
||||
for(auto n : calls){
|
||||
for(auto& board: bingo_cards){
|
||||
bool r = board.call(n);
|
||||
if(r){
|
||||
cout << n << " * " << board.score() << " = " << n * board.score() << '\n';
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << flush;
|
||||
|
||||
file_stream.close();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user