aboutsummaryrefslogtreecommitdiff
path: root/2018/src/day03.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/day03.cpp')
-rw-r--r--2018/src/day03.cpp95
1 files changed, 95 insertions, 0 deletions
diff --git a/2018/src/day03.cpp b/2018/src/day03.cpp
new file mode 100644
index 0000000..bed3acb
--- /dev/null
+++ b/2018/src/day03.cpp
@@ -0,0 +1,95 @@
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <map>
5#include <regex>
6#include <set>
7#include <tuple>
8#include <vector>
9
10using namespace std;
11
12namespace {
13
14struct Claim {
15 int id;
16 int left;
17 int top;
18 int width;
19 int height;
20};
21
22#if 0
23ostream&
24operator<<(ostream& ostrm, const Claim& claim)
25{
26 ostrm << "id: " << claim.id
27 << " (" << claim.left << ", " << claim.top << ": " << claim.width << "x" << claim.height;
28 return ostrm;
29}
30#endif
31
32vector<Claim>
33read_file(const filesystem::path& filename)
34{
35 ifstream file{ filename };
36 vector<Claim> claims;
37
38 regex pattern(R"#((\d+) @ (\d+),(\d+): (\d+)x(\d+))#");
39 smatch match;
40
41 for ( string line; getline(file, line); ) {
42 if ( !regex_search(line, match, pattern) ) {
43 continue;
44 }
45
46 Claim claim{};
47 claim.id = stoi(match[1]);
48 claim.left = stoi(match[2]);
49 claim.top = stoi(match[3]);
50 claim.width = stoi(match[4]);
51 claim.height = stoi(match[5]);
52
53 claims.emplace_back(claim);
54 }
55
56 return claims;
57}
58
59void
60solve(const vector<Claim>& claims)
61{
62 map<tuple<int, int>, vector<int>> fabric;
63 set<int> allIds;
64
65 for ( const auto& claim: claims ) {
66 for ( int x = claim.left; x < claim.left + claim.width; ++x ) {
67 for ( int y = claim.top; y < claim.top + claim.height; ++y ) {
68 fabric[{ x, y }].emplace_back(claim.id);
69 }
70 }
71 allIds.emplace(claim.id);
72 }
73
74 int overlapCount = 0;
75 for ( const auto& [pos, ids]: fabric ) {
76 if ( ids.size() > 1 ) {
77 ++overlapCount;
78 for ( const auto& id: ids ) {
79 allIds.erase(id);
80 }
81 }
82 }
83
84 cout << "Part 1: " << overlapCount << '\n';
85 cout << "Part 2: " << *allIds.begin() << '\n';
86}
87
88} // namespace
89
90int
91main()
92{
93 auto claims = read_file("data/day03.txt");
94 solve(claims);
95}