@ -16,44 +16,9 @@ struct node{
node * parent = nullptr ;
} ;
bool escape ( int x , int y , int z , std : : map < std : : string , bool > visited ) {
visited [ std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ] = true ;
if ( x < = minX | | x > = maxX | | y < = minY | | y > = maxY | | z < = minZ | | z > = maxZ ) {
return true ;
} else {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) ! = blocks . end ( ) ) {
return false ;
}
bool visitedSections [ ] = { visited . find ( std : : to_string ( x + 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = visited . end ( ) ,
visited . find ( std : : to_string ( x - 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = visited . end ( ) ,
visited . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y + 1 ) + " _ " + std : : to_string ( z ) ) = = visited . end ( ) ,
visited . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y - 1 ) + " _ " + std : : to_string ( z ) ) = = visited . end ( ) ,
visited . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z + 1 ) ) = = visited . end ( ) ,
visited . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z - 1 ) ) = = visited . end ( ) , } ;
visited [ std : : to_string ( x + 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ] = true ;
visited [ std : : to_string ( x - 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ] = true ;
visited [ std : : to_string ( x ) + " _ " + std : : to_string ( y + 1 ) + " _ " + std : : to_string ( z ) ] = true ;
visited [ std : : to_string ( x ) + " _ " + std : : to_string ( y - 1 ) + " _ " + std : : to_string ( z ) ] = true ;
visited [ std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z + 1 ) ] = true ;
visited [ std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z - 1 ) ] = true ;
if ( ! visitedSections [ 0 ] & & escape ( x + 1 , y , z , visited ) | |
! visitedSections [ 1 ] & & escape ( x - 1 , y , z , visited ) | |
! visitedSections [ 2 ] & & escape ( x , y + 1 , z , visited ) | |
! visitedSections [ 3 ] & & escape ( x , y - 1 , z , visited ) | |
! visitedSections [ 4 ] & & escape ( x , y , z + 1 , visited ) | |
! visitedSections [ 5 ] & & escape ( x , y , z - 1 , visited ) ) {
return true ;
} else {
return false ;
}
}
}
int main ( )
{
std : : ifstream file ( " test input" ) ;
std : : ifstream file ( " input " ) ;
int surfaceArea = 0 ;
while ( file . good ( ) ) {
@ -108,6 +73,9 @@ int main()
std : : cout < < surfaceArea < < std : : endl ;
}
}
std : : cout < < " MinX: " < < minX < < " MaxX: " < < maxX < < std : : endl ;
std : : cout < < " MinY: " < < minY < < " MaxY: " < < maxY < < std : : endl ;
std : : cout < < " MinZ: " < < minZ < < " MaxZ: " < < maxZ < < std : : endl ;
std : : vector < std : : vector < std : : vector < node > > > grid ;
for ( int z = minZ ; z < = maxZ ; z + + ) {
@ -122,35 +90,86 @@ int main()
}
grid . push_back ( minigrid ) ;
}
for ( int x = - minX ; x < = maxX ; x + + ) {
for ( int y = - minY ; y < = maxY ; y + + ) {
for ( int z = - minZ ; z < = maxZ ; z + + ) {
for ( int x = minX ; x < = maxX ; x + + ) {
for ( int y = minY ; y < = maxY ; y + + ) {
for ( int z = minZ ; z < = maxZ ; z + + ) {
node & myNode = grid [ z - minZ ] [ y - minY ] [ x - minX ] ;
if ( x + 1 < = maxX & & blocks . find ( std : : to_string ( x + 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y - minY ] [ ( x + 1 ) - minX ] ) ;
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) ! = blocks . end ( ) ) continue ;
std : : cout < < " ==================== " < < std : : endl ;
std : : cout < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ( " < < x < < " , " < < y < < " , " < < z < < " ) " < < std : : endl ;
if ( x + 1 < = maxX ) {
if ( blocks . find ( std : : to_string ( x + 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
node & tempNode = grid [ z - minZ ] [ y - minY ] [ ( x + 1 ) - minX ] ;
myNode . connections . push_back ( & tempNode ) ;
std : : cout < < " Connected ( " < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ) -> ( " < < tempNode . x < < " , " < < tempNode . y < < " , " < < tempNode . z < < " ) " < < std : : endl ;
} else {
std : : cout < < " Ignored ( " < < x + 1 < < " , " < < y < < " , " < < z < < " ) because a block exists there. " < < std : : endl ;
}
if ( x - 1 > = minX & & blocks . find ( std : : to_string ( x - 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y - minY ] [ ( x - 1 ) - minX ] ) ;
} else {
std : : cout < < " Ignored ( " < < x + 1 < < " , " < < y < < " , " < < z < < " ) because it's out of bounds. " < < std : : endl ;
}
if ( y + 1 < = maxY & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y + 1 ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y + 1 - minY ] [ ( x ) - minX ] ) ;
if ( x - 1 > = minX ) {
if ( blocks . find ( std : : to_string ( x - 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
node & tempNode = grid [ z - minZ ] [ y - minY ] [ ( x - 1 ) - minX ] ;
myNode . connections . push_back ( & tempNode ) ;
std : : cout < < " Connected ( " < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ) -> ( " < < tempNode . x < < " , " < < tempNode . y < < " , " < < tempNode . z < < " ) " < < std : : endl ;
} else {
std : : cout < < " Ignored ( " < < x - 1 < < " , " < < y < < " , " < < z < < " ) because a block exists there. " < < std : : endl ;
}
if ( y - 1 > = minY & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y - 1 ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y - 1 - minY ] [ ( x ) - minX ] ) ;
} else {
std : : cout < < " Ignored ( " < < x - 1 < < " , " < < y < < " , " < < z < < " ) because it's out of bounds. " < < std : : endl ;
}
if ( z + 1 < = maxZ & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z + 1 ) ) = = blocks . end ( ) ) {
myNode . connections . push_back ( & grid [ z + 1 - minZ ] [ y - minY ] [ ( x ) - minX ] ) ;
if ( y + 1 < = maxY ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y + 1 ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
node & tempNode = grid [ z - minZ ] [ y + 1 - minY ] [ ( x ) - minX ] ;
myNode . connections . push_back ( & tempNode ) ;
std : : cout < < " Connected ( " < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ) -> ( " < < tempNode . x < < " , " < < tempNode . y < < " , " < < tempNode . z < < " ) " < < std : : endl ;
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y + 1 < < " , " < < z < < " ) because a block exists there. " < < std : : endl ;
}
if ( z - 1 > = minZ & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z - 1 ) ) = = blocks . end ( ) ) {
myNode . connections . push_back ( & grid [ z - 1 - minZ ] [ y - minY ] [ ( x ) - minX ] ) ;
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y + 1 < < " , " < < z < < " ) because it's out of bounds. " < < std : : endl ;
}
if ( y - 1 > = minY ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y - 1 ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
node & tempNode = grid [ z - minZ ] [ y - 1 - minY ] [ ( x ) - minX ] ;
myNode . connections . push_back ( & tempNode ) ;
std : : cout < < " Connected ( " < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ) -> ( " < < tempNode . x < < " , " < < tempNode . y < < " , " < < tempNode . z < < " ) " < < std : : endl ;
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y - 1 < < " , " < < z < < " ) because a block exists there. " < < std : : endl ;
}
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y - 1 < < " , " < < z < < " ) because it's out of bounds. " < < std : : endl ;
}
if ( z + 1 < = maxZ ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z + 1 ) ) = = blocks . end ( ) ) {
node & tempNode = grid [ z + 1 - minZ ] [ y - minY ] [ ( x ) - minX ] ;
myNode . connections . push_back ( & tempNode ) ;
std : : cout < < " Connected ( " < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ) -> ( " < < tempNode . x < < " , " < < tempNode . y < < " , " < < tempNode . z < < " ) " < < std : : endl ;
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y < < " , " < < z + 1 < < " ) because a block exists there. " < < std : : endl ;
}
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y < < " , " < < z + 1 < < " ) because it's out of bounds. " < < std : : endl ;
}
if ( z - 1 > = minZ ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z - 1 ) ) = = blocks . end ( ) ) {
node & tempNode = grid [ z - 1 - minZ ] [ y - minY ] [ ( x ) - minX ] ;
myNode . connections . push_back ( & tempNode ) ;
std : : cout < < " Connected ( " < < myNode . x < < " , " < < myNode . y < < " , " < < myNode . z < < " ) -> ( " < < tempNode . x < < " , " < < tempNode . y < < " , " < < tempNode . z < < " ) " < < std : : endl ;
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y < < " , " < < z - 1 < < " ) because a block exists there. " < < std : : endl ;
}
} else {
std : : cout < < " Ignored ( " < < x < < " , " < < y < < " , " < < z - 1 < < " ) because it's out of bounds. " < < std : : endl ;
}
}
}
}
int trappedSurfaceArea = 0 ;
for ( int x = - minX ; x < = maxX ; x + + ) {
for ( int y = - minY ; y < = maxY ; y + + ) {
for ( int z = - minZ ; z < = maxZ ; z + + ) {
for ( int x = minX ; x < = maxX ; x + + ) {
for ( int y = minY ; y < = maxY ; y + + ) {
for ( int z = minZ ; z < = maxZ ; z + + ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
std : : vector < node * > searchNodes ;
for ( int zz = 0 ; zz < grid . size ( ) ; zz + + ) {
@ -189,15 +208,17 @@ int main()
node * searchNode = endNode ;
int jumps = 0 ;
std : : cout < < " Trying to path to (0,0,0)... " < < std : : endl ;
while ( searchNode ! = startNode ) {
if ( searchNode - > parent ! = nullptr ) {
std : : cout < < " Node goes from " < < searchNode - > x < < " , " < < searchNode - > y < < " , " < < searchNode - > z < < " to " < < searchNode - > parent - > x < < " , " < < searchNode - > parent - > y < < " , " < < searchNode - > parent - > z < < std : : endl ;
jumps + + ;
searchNode = searchNode - > parent ;
std : : cout < < " ->( " < < searchNode - > x < < " , " < < searchNode - > y < < " , " < < searchNode - > z ;
} else {
std : : cout < < " ->X " ;
break ;
}
}
std : : cout < < std : : endl ;
if ( searchNode ! = startNode ) { //Couldn't find it.
trappedBlocks [ std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ] = true ;
std : : cout < < " Block " < < std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) < < " is trapped " < < std : : endl ;