-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.cpp
136 lines (117 loc) · 3.14 KB
/
08.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <map>
#include <vector>
using NumberMap = std::map<int, std::string>;
bool isOne(const std::string& s) {
return s.size() == 2;
}
bool isSeven(const std::string& s) {
return s.size() == 3;
}
bool isFour(const std::string& s) {
return s.size() == 4;
}
bool isEight(const std::string& s) {
return s.size() == 7;
}
std::string setDifference(const std::string& s1, const std::string& s2) {
std::string s;
for (auto&& x: s1) {
// if s2 doesn't contain x
if (s2.find(x) == std::string::npos) s += x;
}
return s;
}
bool containsNum(const std::string& src, const std::string& num) {
for (auto&& x: num) {
if (src.find(x) == std::string::npos) return false;
}
return true;
}
int getKey(const std::string& value, const NumberMap& map) {
for (auto&& x: map) {
if (containsNum(x.second, value) && x.second.size() == value.size()) {
return x.first;
}
}
return -1;
}
int solve(const std::string& line) {
std::stringstream ss{ line };
std::string s;
NumberMap map;
std::vector<std::string> fiveCharsLong, sixCharsLong;
// read numbers
for (int i = 0; i < 10; ++i) {
ss >> s;
if (isOne(s)) map[1] = s;
else if (isSeven(s)) map[7] = s;
else if (isFour(s)) map[4] = s;
else if (isEight(s)) map[8] = s;
else if (s.size() == 5) fiveCharsLong.emplace_back(s);
else sixCharsLong.emplace_back(s);
}
// get three
for (int i = 0; i < fiveCharsLong.size(); ++i) {
if (containsNum(fiveCharsLong[i], map[1])) {
map[3] = fiveCharsLong[i];
fiveCharsLong.erase(fiveCharsLong.begin() + i);
break;
}
}
// get nine
for (int i = 0; i < sixCharsLong.size(); ++i) {
if (containsNum(sixCharsLong[i], map[3])) {
map[9] = sixCharsLong[i];
sixCharsLong.erase(sixCharsLong.begin() + i);
break;
}
}
for (auto&& x: sixCharsLong) {
if (containsNum(x, map[1])) map[0] = x;
else map[6] = x;
}
auto res = setDifference(map[8], map[6]);
for (auto&& x: fiveCharsLong) {
if (containsNum(x, res)) map[2] = x;
else map[5] = x;
}
// delimiter we can skip
ss >> s;
int num = 0;
for (int i = 0; i < 4; ++i) {
ss >> s;
num = 10 * num + getKey(s, map);
}
return num;
}
void partTwo(std::istream& in) {
std::string line;
int sum = 0;
while (std::getline(in, line)) {
sum += solve(line);
}
std::cout << sum << std::endl;
}
void partOne(std::istream& in) {
int count = 0;
std::string s;
while (std::getline(in, s, '|')) {
std::getline(in, s);
std::stringstream ss{ s };
for (int i = 0; i < 4; ++i) {
ss >> s;
if (isOne(s) || isSeven(s) || isFour(s) || isEight(s)) ++count;
}
}
std::cout << count << std::endl;
}
int main(int argc, char* argv[]) {
std::ifstream dataFile(argv[1]);
//partOne(dataFile);
partTwo(dataFile);
dataFile.close();
}