-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (92 loc) · 2.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <algorithm>
#include <cassert>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
const int GRID_WIDTH = 5;
const int GRID_SIZE = GRID_WIDTH * GRID_WIDTH;
typedef unordered_map<int, int> NumberGrid; // number to index
typedef unordered_set<int> MarkedGrid; // marked indexes
struct InputType {
vector<int> numbers;
vector<NumberGrid> grids;
vector<MarkedGrid> markedGrids;
};
InputType parseInput(const string& path);
int findLastWinner(InputType& input);
bool hasWon(int currentIndex, const MarkedGrid& markedGrid);
int calculateScore(const NumberGrid& grid, const MarkedGrid& markedGrid, int lastNumber);
int main(int argc, char** argv) {
vector<string> args(argv + 1, argv + argc);
InputType input = parseInput(args.at(0));
int score = findLastWinner(input);
cout << score << endl;
return 0;
}
InputType parseInput(const string& path) {
ifstream ifs(path);
if (!ifs.is_open()) {
throw runtime_error("File not found: " + path);
}
InputType input;
string firstLine;
getline(ifs, firstLine);
stringstream ss(firstLine);
string number;
while (getline(ss, number, ',')) {
input.numbers.push_back(stoi(number));
}
NumberGrid grid;
for (int i = 0, number; ifs >> number; i = (i + 1) % GRID_SIZE) {
grid.emplace(number, i);
if (i == GRID_SIZE - 1) {
input.grids.push_back(grid);
input.markedGrids.push_back(MarkedGrid());
grid.clear();
}
}
return input;
}
int findLastWinner(InputType& input) {
unordered_set<int> winningGrids;
for (int number: input.numbers) {
for (int gridId = 0; gridId < input.grids.size(); gridId++) {
NumberGrid& grid = input.grids.at(gridId);
MarkedGrid& markedGrid = input.markedGrids.at(gridId);
auto itr = grid.find(number);
if (itr != grid.end()) {
int index = itr->second;
markedGrid.emplace(index);
if (hasWon(index, markedGrid)) winningGrids.emplace(gridId);
if (winningGrids.size() == input.grids.size()) return calculateScore(grid, markedGrid, number);
}
}
}
return 0;
}
bool hasWon(int currentIndex, const MarkedGrid& markedGrid) {
int x = currentIndex % GRID_WIDTH;
int y = currentIndex / GRID_WIDTH;
bool hasRow = true;
bool hasColumn = true;
for (int i = 0; i < GRID_WIDTH; i++) {
int row_index = y * GRID_WIDTH + i;
int col_index = i * GRID_WIDTH + x;
hasRow = hasRow && markedGrid.find(row_index) != markedGrid.end();
hasColumn = hasColumn && markedGrid.find(col_index) != markedGrid.end();
}
return hasRow || hasColumn;
}
int calculateScore(const NumberGrid& grid, const MarkedGrid& markedGrid, int lastNumber) {
int sum = 0;
for (const auto& p: grid) {
int index = p.second;
if (markedGrid.find(index) == markedGrid.end()) sum += p.first;
}
return sum * lastNumber;
}