blob: bed3acb607aaa87d8ff0230b6853b2cb77ccb999 (
plain)
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
|
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <regex>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
namespace {
struct Claim {
int id;
int left;
int top;
int width;
int height;
};
#if 0
ostream&
operator<<(ostream& ostrm, const Claim& claim)
{
ostrm << "id: " << claim.id
<< " (" << claim.left << ", " << claim.top << ": " << claim.width << "x" << claim.height;
return ostrm;
}
#endif
vector<Claim>
read_file(const filesystem::path& filename)
{
ifstream file{ filename };
vector<Claim> claims;
regex pattern(R"#((\d+) @ (\d+),(\d+): (\d+)x(\d+))#");
smatch match;
for ( string line; getline(file, line); ) {
if ( !regex_search(line, match, pattern) ) {
continue;
}
Claim claim{};
claim.id = stoi(match[1]);
claim.left = stoi(match[2]);
claim.top = stoi(match[3]);
claim.width = stoi(match[4]);
claim.height = stoi(match[5]);
claims.emplace_back(claim);
}
return claims;
}
void
solve(const vector<Claim>& claims)
{
map<tuple<int, int>, vector<int>> fabric;
set<int> allIds;
for ( const auto& claim: claims ) {
for ( int x = claim.left; x < claim.left + claim.width; ++x ) {
for ( int y = claim.top; y < claim.top + claim.height; ++y ) {
fabric[{ x, y }].emplace_back(claim.id);
}
}
allIds.emplace(claim.id);
}
int overlapCount = 0;
for ( const auto& [pos, ids]: fabric ) {
if ( ids.size() > 1 ) {
++overlapCount;
for ( const auto& id: ids ) {
allIds.erase(id);
}
}
}
cout << "Part 1: " << overlapCount << '\n';
cout << "Part 2: " << *allIds.begin() << '\n';
}
} // namespace
int
main()
{
auto claims = read_file("data/day03.txt");
solve(claims);
}
|