From 6b289756222dfd6c112b2b5a67689819e5b2ae4c Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Tue, 26 Dec 2023 22:24:27 +0100 Subject: Lösungen für Tag 22, Teil 2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/day22.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'src') 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) } void -part1() +part1(vector puzzle) { - auto puzzle = read_file("data/day22.txt"); - map> grid; map> supported_by; @@ -91,8 +89,69 @@ part1() cout << number << endl; } +void +part2(vector puzzle) +{ + map> grid; + + for ( auto& piece: puzzle ) { + int max_z = 0; + for ( auto x = piece.x1; x <= piece.x2; ++x ) { + for ( auto y = piece.y1; y <= piece.y2; ++y ) { + max_z = max(max_z, grid[x][y]); + } + } + + auto height = piece.z2 - piece.z1 + 1; + + for ( auto x = piece.x1; x <= piece.x2; ++x ) { + for ( auto y = piece.y1; y <= piece.y2; ++y ) { + grid[x][y] = max_z + height; + } + } + + piece.z1 = max_z + 1; + piece.z2 = max_z + height; + } + + int num = 0; + for ( size_t idx = 0; idx != puzzle.size(); ++idx ) { + map> grid; + + for ( size_t idx2 = 0; idx2 != puzzle.size(); ++idx2 ) { + if ( idx == idx2 ) { + continue; + } + + const auto& piece = puzzle[idx2]; + + int max_z = 0; + for ( auto x = piece.x1; x <= piece.x2; ++x ) { + for ( auto y = piece.y1; y <= piece.y2; ++y ) { + max_z = max(max_z, grid[x][y]); + } + } + + auto height = piece.z2 - piece.z1 + 1; + + for ( auto x = piece.x1; x <= piece.x2; ++x ) { + for ( auto y = piece.y1; y <= piece.y2; ++y ) { + grid[x][y] = max_z + height; + } + } + + if ( piece.z1 != max_z + 1 ) { + ++num; + } + } + } + cout << num << endl; +} + int main() { - part1(); + const auto puzzle = read_file("data/day22.txt"); + part1(puzzle); + part2(puzzle); } -- cgit v1.3