aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/day22.cpp67
1 files changed, 63 insertions, 4 deletions
diff --git a/src/day22.cpp b/src/day22.cpp
index 71320f5..1afb994 100644
--- a/src/day22.cpp
+++ b/src/day22.cpp
@@ -38,10 +38,8 @@ read_file(string_view filename)
38} 38}
39 39
40void 40void
41part1() 41part1(vector<piece> puzzle)
42{ 42{
43 auto puzzle = read_file("data/day22.txt");
44
45 map<int, map<int, int>> grid; 43 map<int, map<int, int>> grid;
46 44
47 map<size_t, vector<size_t>> supported_by; 45 map<size_t, vector<size_t>> supported_by;
@@ -91,8 +89,69 @@ part1()
91 cout << number << endl; 89 cout << number << endl;
92} 90}
93 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
94int 151int
95main() 152main()
96{ 153{
97 part1(); 154 const auto puzzle = read_file("data/day22.txt");
155 part1(puzzle);
156 part2(puzzle);
98} 157}