-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.cpp
180 lines (163 loc) · 4.98 KB
/
day16.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <map>
#include <vector>
#include <regex>
#include <set>
using namespace std;
bool isValid(map<string, vector<int>> fields, int value) {
for (const auto& [key, ranges] : fields) {
if ((value >= ranges[0] && value <= ranges[1]) || (value >= ranges[2] && value <= ranges[3])) {
return true;
}
}
return false;
}
set<string> getValidRows(map<string, vector<int>> fields, int value) {
set<string> rows;
for (const auto& [key, ranges] : fields) {
if ((value >= ranges[0] && value <= ranges[1]) || (value >= ranges[2] && value <= ranges[3])) {
rows.insert(key);
}
}
return rows;
}
template <class Os, class K>
Os& operator<<(Os& os, const std::set<K>& v) {
os << '[' << v.size() << "] {";
bool o{};
for (const auto& e : v)
os << (o ? ", " : (o = 1, " ")) << e;
return os << " }\n";
}
long long getErrorRate(string filename) {
ifstream sstream(filename);
map<string, vector<int>> fields;
regex re("(\\w+): (\\d+)-(\\d+) or (\\d+)-(\\d+)");
for (string ss; getline(sstream, ss); ) {
if (ss == "") {
break;
}
smatch m;
if (regex_match(ss, m, re)) {
fields[m[1].str()] = {stoi(m[2].str()), stoi(m[3].str()), stoi(m[4].str()), stoi(m[5].str())};
}
}
// skip to nearby tickets
for (int i = 0; i < 4; ++i) {
string ss;
getline(sstream, ss);
}
// check invalid tickets
long long invalidTotal = 0;
for (string ss; getline(sstream, ss); ) {
stringstream nearby(ss);
while (nearby.good()) {
string substr;
getline(nearby, substr, ',');
if (!isValid(fields, stoi(substr))) {
invalidTotal += stoi(substr);
}
}
}
return invalidTotal;
}
long long getDeparture(string filename) {
ifstream sstream(filename);
map<string, vector<int>> fields;
regex re("([\\w ]+): (\\d+)-(\\d+) or (\\d+)-(\\d+)");
for (string ss; getline(sstream, ss); ) {
if (ss == "") {
break;
}
smatch m;
if (regex_match(ss, m, re)) {
fields[m[1].str()] = {stoi(m[2].str()), stoi(m[3].str()), stoi(m[4].str()), stoi(m[5].str())};
}
}
// save my ticket
vector<int> myTicket;
{
string ss;
getline(sstream, ss);
getline(sstream, ss);
stringstream ticketss(ss);
while (ticketss.good()) {
string substr;
getline(ticketss, substr, ',');
myTicket.push_back(stoi(substr));
}
getline(sstream, ss);
getline(sstream, ss);
}
// check tickets
vector<set<string>> validRows;
{
set<string> tempSet;
for (const auto& [key, ranges] : fields) {
tempSet.insert(key);
}
for (int i = 0; i < myTicket.size(); ++i) {
set<string> mySet(tempSet);
validRows.push_back(mySet);
}
}
for (string ss; getline(sstream, ss); ) {
stringstream nearby(ss);
vector<int> ticketValues;
bool valid = true;
while (nearby.good()) {
string substr;
getline(nearby, substr, ',');
if (!isValid(fields, stoi(substr))) {
valid = false;
break;
} else {
ticketValues.push_back(stoi(substr));
}
}
if (valid) {
for (int i = 0; i < ticketValues.size(); ++i) {
auto rows = getValidRows(fields, ticketValues[i]);
rows.merge(validRows[i]);
}
}
}
// now go through the validRows to assign the positions. Check those
// with the least valid things first
map<string, int> fieldPositions;
while (fieldPositions.size() < fields.size()) {
for (int i = 0; i < validRows.size(); ++i) {
string field;
if (validRows[i].size() == 1) {
// found a match!
for (const auto& r : validRows[i]) {
field = r;
}
fieldPositions[field] = i;
// delete it from all sets in the list
for (auto it = validRows.begin(); it != validRows.end(); ++it) {
it->erase(field);
}
}
}
}
// for (const auto& [key, value] : fieldPositions) {
// cout << key << ", " << value << endl;
// }
long long departure = 1;
for (const auto& [key, value] : fieldPositions) {
if (key.find("departure") != string::npos) {
departure *= myTicket[value];
}
}
return departure;
}
int main() {
auto errorRate = getErrorRate("day16.txt");
auto departure = getDeparture("day16.txt");
cout << "Ticket scanning error rate = " << errorRate << endl;
cout << "Ticket scanning error rate = " << departure << endl;
}