-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_parser.cpp
More file actions
51 lines (46 loc) · 1.18 KB
/
file_parser.cpp
File metadata and controls
51 lines (46 loc) · 1.18 KB
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
#include "file_parser.h"
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;
vector<int> split(string data, char pattern) {
vector<int> res;
istringstream iss(data);
string val = "";
while (getline(iss, val, pattern)) {
res.push_back(stoi(val));
}
return res;
}
bool parseInputFile(const std::string &filename, Puzzle &puzzle) {
ifstream in(filename);
if (!in.is_open()) {
return false;
}
string line = "";
getline(in, line);
if (line.find("column sums:") == string::npos) {
return false;
}
puzzle.columnSums = split(line.substr(13), ',');
getline(in, line);
if (line.find("row sums:") == string::npos) {
in.close();
return false;
}
puzzle.rowSums = split(line.substr(10), ',');
getline(in, line);
while (getline(in, line)) {
vector<int> row = split(line, ',');
if (row.size() != 4) {
return false;
}
puzzle.grid.push_back(row);
}
in.close();
if (puzzle.grid.size() != 4 || puzzle.rowSums.size() != 4 || puzzle.columnSums.size() != 4) {
return false;
}
return true;
}