aboutsummaryrefslogtreecommitdiff
path: root/src/day22.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/day22.cpp')
-rw-r--r--src/day22.cpp157
1 files changed, 157 insertions, 0 deletions
diff --git a/src/day22.cpp b/src/day22.cpp
new file mode 100644
index 0000000..1afb994
--- /dev/null
+++ b/src/day22.cpp
@@ -0,0 +1,157 @@
1#include <fstream>
2#include <iostream>
3#include <map>
4#include <regex>
5#include <set>
6#include <string>
7#include <vector>
8using namespace std;
9
10struct piece {
11 int x1, y1, z1;
12 int x2, y2, z2;
13};
14
15vector<piece>
16read_file(string_view filename)
17{
18 static const regex pattern{ R"((\d+),(\d+),(\d+)~(\d+),(\d+),(\d+))" };
19
20 fstream input{ filename };
21 vector<piece> data;
22
23 for ( string line; getline(input, line); ) {
24 smatch matches;
25 if ( regex_search(line, matches, pattern) ) {
26 data.emplace_back(piece{
27 stoi(matches[1]),
28 stoi(matches[2]),
29 stoi(matches[3]),
30 stoi(matches[4]),
31 stoi(matches[5]),
32 stoi(matches[6]) });
33 }
34 }
35 sort(data.begin(), data.end(), [](const auto& lhs, const auto& rhs) { return lhs.z1 < rhs.z1; });
36
37 return data;
38}
39
40void
41part1(vector<piece> puzzle)
42{
43 map<int, map<int, int>> grid;
44
45 map<size_t, vector<size_t>> supported_by;
46 map<size_t, vector<size_t>> supports;
47
48 for ( size_t idx = 0; idx != puzzle.size(); ++idx ) {
49 auto& piece = puzzle[idx];
50
51 int max_z = 0;
52 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
53 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
54 max_z = max(max_z, grid[x][y]);
55 }
56 }
57
58 auto height = piece.z2 - piece.z1 + 1;
59
60 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
61 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
62 grid[x][y] = max_z + height;
63 }
64 }
65
66 piece.z1 = max_z + 1;
67 piece.z2 = max_z + height;
68
69 for ( size_t idx2 = idx; idx2-- > 0; ) {
70 if ( piece.x1 > puzzle[idx2].x2 || puzzle[idx2].x1 > piece.x2 ) {
71 continue;
72 }
73 if ( piece.y1 > puzzle[idx2].y2 || puzzle[idx2].y1 > piece.y2 ) {
74 continue;
75 }
76 if ( puzzle[idx].z1 == puzzle[idx2].z2 + 1 ) {
77 supports[idx2].emplace_back(idx);
78 supported_by[idx].emplace_back(idx2);
79 }
80 }
81 }
82
83 long number = 0;
84 for ( size_t idx = 0; idx != puzzle.size(); ++idx ) {
85 if ( all_of(supports[idx].begin(), supports[idx].end(), [&](const auto& idx2) { return supported_by[idx2].size() > 1; }) ) {
86 ++number;
87 }
88 }
89 cout << number << endl;
90}
91
92void
93part2(vector<piece> puzzle)
94{
95 map<int, map<int, int>> grid;
96
97 for ( auto& piece: puzzle ) {
98 int max_z = 0;
99 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
100 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
101 max_z = max(max_z, grid[x][y]);
102 }
103 }
104
105 auto height = piece.z2 - piece.z1 + 1;
106
107 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
108 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
109 grid[x][y] = max_z + height;
110 }
111 }
112
113 piece.z1 = max_z + 1;
114 piece.z2 = max_z + height;
115 }
116
117 int num = 0;
118 for ( size_t idx = 0; idx != puzzle.size(); ++idx ) {
119 map<int, map<int, int>> grid;
120
121 for ( size_t idx2 = 0; idx2 != puzzle.size(); ++idx2 ) {
122 if ( idx == idx2 ) {
123 continue;
124 }
125
126 const auto& piece = puzzle[idx2];
127
128 int max_z = 0;
129 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
130 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
131 max_z = max(max_z, grid[x][y]);
132 }
133 }
134
135 auto height = piece.z2 - piece.z1 + 1;
136
137 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
138 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
139 grid[x][y] = max_z + height;
140 }
141 }
142
143 if ( piece.z1 != max_z + 1 ) {
144 ++num;
145 }
146 }
147 }
148 cout << num << endl;
149}
150
151int
152main()
153{
154 const auto puzzle = read_file("data/day22.txt");
155 part1(puzzle);
156 part2(puzzle);
157}