Added year directories for advent of code

This commit is contained in:
2022-12-02 13:37:41 +01:00
parent 27d00340b9
commit 6c9525248e
31 changed files with 275 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
#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);
}
bool found_result = false;
cout << "Best board score:" << endl;
for(auto n : calls){
for(auto& board: bingo_cards){
bool r = board.call(n);
if(r){
cout << n << " * " << board.score() << " = " << n * board.score() << '\n';
found_result = true;
break;
}
}
if(found_result) break;
}
// Now for part 2, find the worst board. So we go through all boards and see when they finish
// Reset all boards first
// Then go through all boards and do all calls on them and keep track of the #numbers needed
int worst_board = 0, calls_needed_worst_board = 0, current_board = 0;
for(auto& b : bingo_cards){
for(int y = 0; y < 5; ++y){
b.opens[y].fill(true);
}
int calls_needed = 0;
for(auto c : calls){
bool call_result = b.call(c);
if(call_result){
if(calls_needed_worst_board < calls_needed){
worst_board = current_board;
calls_needed_worst_board = calls_needed;
}
break;
}
calls_needed++;
}
current_board++;
}
int last_call = calls[calls_needed_worst_board];
int worst_board_score = bingo_cards[worst_board].score();
cout << "Worst board:" << endl;
cout << last_call << " * " << worst_board_score << " = " << last_call * worst_board_score << endl;
cout << flush;
file_stream.close();
return 0;
}