-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_03b.cpp
78 lines (71 loc) · 2 KB
/
day_03b.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
#include <iostream>
#include <fstream>
#include <regex>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
struct pair_hash{
template <typename T1, typename T2>
size_t operator ()(const std::pair<T1, T2>& p) const {
return std::hash<T1>{}(p.first) ^ std::hash<T2>{}(p.second);
}
};
int main(int argc, char* argv[]) {
std::string input = "../input/day_03_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::ifstream file(input);
const std::regex pattern(R"(#([0-9]+) @ ([0-9]+),([0-9]+): ([0-9]+)x([0-9]+))");
std::unordered_map<std::pair<int, int>, std::pair<int, int>, pair_hash> coords;
std::vector<std::vector<int>> claims;
std::smatch match;
while(std::getline(file, line)) {
std::regex_search(line, match, pattern);
const int id = stoi(match[1]);
const int x = stoi(match[2]);
const int y = stoi(match[3]);
const int l = stoi(match[4]);
const int b = stoi(match[5]);
claims.emplace_back(std::vector<int>{x, y, l, b});
for (int i = 0; i < l; i++) {
for (int j = 0; j < b; j++) {
const auto p = std::make_pair(x+i, y+j);
if (coords.find(p) == coords.end()) {
coords[p] = {1, id};
} else {
coords[p].first++;
}
}
}
}
std::unordered_set<int> ids;
for(const auto& [key, val] : coords) {
if (val.first == 1) {
ids.insert(val.second);
}
}
for(const auto id : ids) {
const int x = claims[id][0];
const int y = claims[id][1];
const int l = claims[id][2];
const int b = claims[id][3];
bool all_one = true;
for (int i = 0; i < l && all_one; i++) {
for (int j = 0; j < b && all_one; j++) {
const auto p = std::make_pair(x+i, y+j);
if (coords[p].first != 1) {
all_one = false;
}
}
}
if (all_one == true) {
// Id and index in vector is offset by 1, ids start with 1, index with 0
std::cout << id + 1 << '\n';
return id + 1;
}
}
return 0;
}