04 formatting

This commit is contained in:
zv0n 2021-12-05 09:52:11 +01:00
parent 338bda525e
commit e8db71e56c

View File

@ -10,18 +10,18 @@ public:
~Bingo() = default;
void addNumberToCard(int number) {
card[numbers / 5][numbers % 5] = {number, false};
card[numbers / 5][numbers % 5] = { number, false };
numbers += 1;
}
void checkNumber(int number) {
if(hasWon()) {
if (hasWon()) {
invalidate();
return;
}
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(card[i][j].first == number) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (card[i][j].first == number) {
card[i][j].second = true;
checkWin(number);
return;
@ -40,9 +40,9 @@ public:
void printSelf() {
std::cout << std::endl;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(card[i][j].second) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (card[i][j].second) {
std::cout << "*";
}
std::cout << card[i][j].first << " ";
@ -58,18 +58,18 @@ public:
private:
void checkWin(int multiplier) {
for(int i = 0; i < 5; i++) {
for (int i = 0; i < 5; i++) {
int horizontal = 0;
int vertical = 0;
for(int j = 0; j < 5; j++) {
if(card[i][j].second) {
for (int j = 0; j < 5; j++) {
if (card[i][j].second) {
horizontal += 1;
}
if(card[j][i].second) {
if (card[j][i].second) {
vertical += 1;
}
}
if(horizontal == 5 || vertical == 5) {
if (horizontal == 5 || vertical == 5) {
won = true;
calculateScore(multiplier);
}
@ -78,9 +78,9 @@ private:
void calculateScore(int multiplier) {
int score = 0;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(!card[i][j].second) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (!card[i][j].second) {
score += card[i][j].first;
}
}
@ -92,15 +92,15 @@ private:
valid = false;
}
std::array<std::array<std::pair<int,bool>, 5>, 5> card;
std::array<std::array<std::pair<int, bool>, 5>, 5> card;
int numbers = 0;
bool won = false;
int winning_score = -1;
bool valid = true;
};
std::pair<std::vector<int>, std::vector<Bingo>> getBingo(const std::string &file_name) {
std::pair<std::vector<int>, std::vector<Bingo>>
getBingo(const std::string &file_name) {
std::pair<std::vector<int>, std::vector<Bingo>> result;
std::ifstream file(file_name);
@ -109,17 +109,17 @@ std::pair<std::vector<int>, std::vector<Bingo>> getBingo(const std::string &file
std::string str;
std::getline(file, str);
std::stringstream ss(str);
while(ss >> tmp) {
while (ss >> tmp) {
result.first.push_back(tmp);
ss >> tmp_char;
}
while (std::getline(file, str)) {
if(str.empty()) {
if (str.empty()) {
result.second.emplace_back();
continue;
}
std::stringstream ss2(str);
for(int i = 0; i < 5; i++) {
for (int i = 0; i < 5; i++) {
ss2 >> tmp;
result.second.back().addNumberToCard(tmp);
}
@ -128,10 +128,10 @@ std::pair<std::vector<int>, std::vector<Bingo>> getBingo(const std::string &file
}
int part1(const std::vector<int> &numbers, std::vector<Bingo> &bingo) {
for(auto &number : numbers) {
for(auto &card : bingo) {
for (auto &number : numbers) {
for (auto &card : bingo) {
card.checkNumber(number);
if(card.hasWon()) {
if (card.hasWon()) {
return card.getWinningScore();
}
}
@ -141,10 +141,10 @@ int part1(const std::vector<int> &numbers, std::vector<Bingo> &bingo) {
int part2(const std::vector<int> &numbers, std::vector<Bingo> &bingo) {
int last_score = -1;
for(auto &number : numbers) {
for(auto &card : bingo) {
for (auto &number : numbers) {
for (auto &card : bingo) {
card.checkNumber(number);
if(card.hasWon() && card.isValid()) {
if (card.hasWon() && card.isValid()) {
last_score = card.getWinningScore();
}
}