-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2016_day3.cpp
104 lines (87 loc) · 2.96 KB
/
2016_day3.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
#include <string>
#include <sstream>
#include <fstream>
#include <vector>
#include <iostream>
#include <iterator>
struct Triangle {
Triangle(std::string a, std::string b, std::string c)
: mA(std::stoi(a))
, mB(std::stoi(b))
, mC(std::stoi(c)) {
}
int mA;
int mB;
int mC;
};
bool isTriangle(const Triangle& triangle) {
return (triangle.mA + triangle.mB > triangle.mC) &&
(triangle.mA + triangle.mC > triangle.mB) &&
(triangle.mB + triangle.mC > triangle.mA);
}
std::vector<std::string> extractTrianglesByRow(const std::string& filePath) {
std::ifstream fileStream(filePath);
std::stringstream stringStream;
std::copy(std::istreambuf_iterator<char>(fileStream),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(stringStream));
std::string currentTriangle;
std::vector<std::string> triangles;
while (std::getline(stringStream, currentTriangle, '\n')) {
triangles.push_back(currentTriangle);
}
return triangles;
}
std::vector<Triangle> extractTrianglesByColumn(const std::string& filePath) {
std::ifstream fileStream(filePath);
std::stringstream stringStream;
std::copy(std::istreambuf_iterator<char>(fileStream),
std::istreambuf_iterator<char>(),
std::ostreambuf_iterator<char>(stringStream));
std::vector<Triangle> output;
std::string currentTriangle;
std::string a[3];
std::string b[3];
std::string c[3];
int index = 0;
while (std::getline(stringStream, currentTriangle, '\n')) {
std::stringstream(currentTriangle) >> a[index] >> b[index] >> c[index];
++index;
if (index == 3) {
output.push_back(Triangle(a[0], a[1], a[2]));
output.push_back(Triangle(b[0], b[1], b[2]));
output.push_back(Triangle(c[0], c[1], c[2]));
index = 0;
}
}
return output;
}
std::vector<Triangle> convertToTriangles(const std::vector<std::string>& triangles) {
std::vector<Triangle> output;
output.reserve(triangles.size());
for (auto triangle : triangles) {
std::istringstream iss(triangle);
std::vector<std::string> result {
std::istream_iterator<std::string>(iss),{} };
output.push_back(Triangle(*result.begin(), *(result.begin() + 1), *(result.begin() + 2)));
}
return output;
}
int main() {
const std::vector<std::string> stringCandidates = extractTrianglesByRow("c:/input.txt");
const std::vector<Triangle> rowCandidates = convertToTriangles(stringCandidates);
int nOfTriangles = 0;
for (auto candidate : rowCandidates) {
if (isTriangle(candidate)) {
++nOfTriangles;
}
}
const std::vector<Triangle> columnCandidates = extractTrianglesByColumn("c:/input.txt");
int nOfTrianglesP2 = 0;
for (auto candidate : columnCandidates) {
if (isTriangle(candidate)) {
++nOfTrianglesP2;
}
}
return 0;
}