@ -16,44 +16,9 @@ struct node{
node * parent = nullptr ;
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 ( )
int main ( )
{
{
std : : ifstream file ( " test input" ) ;
std : : ifstream file ( " input " ) ;
int surfaceArea = 0 ;
int surfaceArea = 0 ;
while ( file . good ( ) ) {
while ( file . good ( ) ) {
@ -108,6 +73,9 @@ int main()
std : : cout < < surfaceArea < < std : : endl ;
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 ;
std : : vector < std : : vector < std : : vector < node > > > grid ;
for ( int z = minZ ; z < = maxZ ; z + + ) {
for ( int z = minZ ; z < = maxZ ; z + + ) {
@ -122,35 +90,86 @@ int main()
}
}
grid . push_back ( minigrid ) ;
grid . push_back ( minigrid ) ;
}
}
for ( int x = - minX ; x < = maxX ; x + + ) {
for ( int x = minX ; x < = maxX ; x + + ) {
for ( int y = - minY ; y < = maxY ; y + + ) {
for ( int y = minY ; y < = maxY ; y + + ) {
for ( int z = - minZ ; z < = maxZ ; z + + ) {
for ( int z = minZ ; z < = maxZ ; z + + ) {
node & myNode = grid [ z - minZ ] [ y - minY ] [ x - minX ] ;
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 ( ) ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) ! = blocks . end ( ) ) continue ;
myNode . connections . push_back ( & grid [ z - minZ ] [ y - minY ] [ ( x + 1 ) - minX ] ) ;
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 ;
}
} else {
std : : cout < < " Ignored ( " < < x + 1 < < " , " < < y < < " , " < < z < < " ) because it's out of bounds. " < < std : : endl ;
}
}
if ( x - 1 > = minX & & blocks . find ( std : : to_string ( x - 1 ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
if ( x - 1 > = minX ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y - minY ] [ ( 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 ;
}
} 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 ( ) ) {
if ( y + 1 < = maxY ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y + 1 - minY ] [ ( x ) - minX ] ) ;
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 ( y - 1 > = minY & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y - 1 ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
if ( y - 1 > = minY ) {
myNode . connections . push_back ( & grid [ z - minZ ] [ y - 1 - minY ] [ ( x ) - minX ] ) ;
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 & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z + 1 ) ) = = blocks . end ( ) ) {
if ( z + 1 < = maxZ ) {
myNode . connections . push_back ( & grid [ z + 1 - minZ ] [ y - minY ] [ ( x ) - minX ] ) ;
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 & & blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z - 1 ) ) = = blocks . end ( ) ) {
if ( z - 1 > = minZ ) {
myNode . connections . push_back ( & grid [ z - 1 - minZ ] [ y - minY ] [ ( x ) - minX ] ) ;
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 ;
int trappedSurfaceArea = 0 ;
for ( int x = - minX ; x < = maxX ; x + + ) {
for ( int x = minX ; x < = maxX ; x + + ) {
for ( int y = - minY ; y < = maxY ; y + + ) {
for ( int y = minY ; y < = maxY ; y + + ) {
for ( int z = - minZ ; z < = maxZ ; z + + ) {
for ( int z = minZ ; z < = maxZ ; z + + ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
if ( blocks . find ( std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ) = = blocks . end ( ) ) {
std : : vector < node * > searchNodes ;
std : : vector < node * > searchNodes ;
for ( int zz = 0 ; zz < grid . size ( ) ; zz + + ) {
for ( int zz = 0 ; zz < grid . size ( ) ; zz + + ) {
@ -189,15 +208,17 @@ int main()
node * searchNode = endNode ;
node * searchNode = endNode ;
int jumps = 0 ;
int jumps = 0 ;
std : : cout < < " Trying to path to (0,0,0)... " < < std : : endl ;
while ( searchNode ! = startNode ) {
while ( searchNode ! = startNode ) {
if ( searchNode - > parent ! = nullptr ) {
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 ;
searchNode = searchNode - > parent ;
std : : cout < < " ->( " < < searchNode - > x < < " , " < < searchNode - > y < < " , " < < searchNode - > z ;
} else {
} else {
std : : cout < < " ->X " ;
break ;
break ;
}
}
}
}
std : : cout < < std : : endl ;
if ( searchNode ! = startNode ) { //Couldn't find it.
if ( searchNode ! = startNode ) { //Couldn't find it.
trappedBlocks [ std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) ] = true ;
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 ;
std : : cout < < " Block " < < std : : to_string ( x ) + " _ " + std : : to_string ( y ) + " _ " + std : : to_string ( z ) < < " is trapped " < < std : : endl ;