04 formatting
This commit is contained in:
parent
338bda525e
commit
e8db71e56c
56
04/main.cpp
56
04/main.cpp
@ -10,18 +10,18 @@ public:
|
|||||||
~Bingo() = default;
|
~Bingo() = default;
|
||||||
|
|
||||||
void addNumberToCard(int number) {
|
void addNumberToCard(int number) {
|
||||||
card[numbers / 5][numbers % 5] = {number, false};
|
card[numbers / 5][numbers % 5] = { number, false };
|
||||||
numbers += 1;
|
numbers += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkNumber(int number) {
|
void checkNumber(int number) {
|
||||||
if(hasWon()) {
|
if (hasWon()) {
|
||||||
invalidate();
|
invalidate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
for(int j = 0; j < 5; j++) {
|
for (int j = 0; j < 5; j++) {
|
||||||
if(card[i][j].first == number) {
|
if (card[i][j].first == number) {
|
||||||
card[i][j].second = true;
|
card[i][j].second = true;
|
||||||
checkWin(number);
|
checkWin(number);
|
||||||
return;
|
return;
|
||||||
@ -40,9 +40,9 @@ public:
|
|||||||
|
|
||||||
void printSelf() {
|
void printSelf() {
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
for(int j = 0; j < 5; j++) {
|
for (int j = 0; j < 5; j++) {
|
||||||
if(card[i][j].second) {
|
if (card[i][j].second) {
|
||||||
std::cout << "*";
|
std::cout << "*";
|
||||||
}
|
}
|
||||||
std::cout << card[i][j].first << " ";
|
std::cout << card[i][j].first << " ";
|
||||||
@ -58,18 +58,18 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void checkWin(int multiplier) {
|
void checkWin(int multiplier) {
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
int horizontal = 0;
|
int horizontal = 0;
|
||||||
int vertical = 0;
|
int vertical = 0;
|
||||||
for(int j = 0; j < 5; j++) {
|
for (int j = 0; j < 5; j++) {
|
||||||
if(card[i][j].second) {
|
if (card[i][j].second) {
|
||||||
horizontal += 1;
|
horizontal += 1;
|
||||||
}
|
}
|
||||||
if(card[j][i].second) {
|
if (card[j][i].second) {
|
||||||
vertical += 1;
|
vertical += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(horizontal == 5 || vertical == 5) {
|
if (horizontal == 5 || vertical == 5) {
|
||||||
won = true;
|
won = true;
|
||||||
calculateScore(multiplier);
|
calculateScore(multiplier);
|
||||||
}
|
}
|
||||||
@ -78,9 +78,9 @@ private:
|
|||||||
|
|
||||||
void calculateScore(int multiplier) {
|
void calculateScore(int multiplier) {
|
||||||
int score = 0;
|
int score = 0;
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
for(int j = 0; j < 5; j++) {
|
for (int j = 0; j < 5; j++) {
|
||||||
if(!card[i][j].second) {
|
if (!card[i][j].second) {
|
||||||
score += card[i][j].first;
|
score += card[i][j].first;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,15 +92,15 @@ private:
|
|||||||
valid = false;
|
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;
|
int numbers = 0;
|
||||||
bool won = false;
|
bool won = false;
|
||||||
int winning_score = -1;
|
int winning_score = -1;
|
||||||
bool valid = true;
|
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::pair<std::vector<int>, std::vector<Bingo>> result;
|
||||||
|
|
||||||
std::ifstream file(file_name);
|
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::string str;
|
||||||
std::getline(file, str);
|
std::getline(file, str);
|
||||||
std::stringstream ss(str);
|
std::stringstream ss(str);
|
||||||
while(ss >> tmp) {
|
while (ss >> tmp) {
|
||||||
result.first.push_back(tmp);
|
result.first.push_back(tmp);
|
||||||
ss >> tmp_char;
|
ss >> tmp_char;
|
||||||
}
|
}
|
||||||
while (std::getline(file, str)) {
|
while (std::getline(file, str)) {
|
||||||
if(str.empty()) {
|
if (str.empty()) {
|
||||||
result.second.emplace_back();
|
result.second.emplace_back();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
std::stringstream ss2(str);
|
std::stringstream ss2(str);
|
||||||
for(int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
ss2 >> tmp;
|
ss2 >> tmp;
|
||||||
result.second.back().addNumberToCard(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) {
|
int part1(const std::vector<int> &numbers, std::vector<Bingo> &bingo) {
|
||||||
for(auto &number : numbers) {
|
for (auto &number : numbers) {
|
||||||
for(auto &card : bingo) {
|
for (auto &card : bingo) {
|
||||||
card.checkNumber(number);
|
card.checkNumber(number);
|
||||||
if(card.hasWon()) {
|
if (card.hasWon()) {
|
||||||
return card.getWinningScore();
|
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 part2(const std::vector<int> &numbers, std::vector<Bingo> &bingo) {
|
||||||
int last_score = -1;
|
int last_score = -1;
|
||||||
for(auto &number : numbers) {
|
for (auto &number : numbers) {
|
||||||
for(auto &card : bingo) {
|
for (auto &card : bingo) {
|
||||||
card.checkNumber(number);
|
card.checkNumber(number);
|
||||||
if(card.hasWon() && card.isValid()) {
|
if (card.hasWon() && card.isValid()) {
|
||||||
last_score = card.getWinningScore();
|
last_score = card.getWinningScore();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user