generated from sigonasr2/CPlusPlusProjectTemplate
Optimized algo
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
0e3364917e
commit
09a3d84459
BIN
C++ProjectTemplate
Executable file
BIN
C++ProjectTemplate
Executable file
Binary file not shown.
84
main.cpp
84
main.cpp
@ -5,10 +5,11 @@
|
|||||||
|
|
||||||
using namespace olc;
|
using namespace olc;
|
||||||
|
|
||||||
|
std::array<std::array<char,7>,30000000>playfieldRows;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::ifstream file("input");
|
std::ifstream file("testinput");
|
||||||
std::vector<std::array<char,7>>playfieldRows;
|
|
||||||
std::array<std::array<char,4>,4>piece{{
|
std::array<std::array<char,4>,4>piece{{
|
||||||
{' ',' ',' ',' '},
|
{' ',' ',' ',' '},
|
||||||
{' ',' ',' ',' '},
|
{' ',' ',' ',' '},
|
||||||
@ -37,8 +38,16 @@ int main()
|
|||||||
{' ',' ',' ',' '},
|
{' ',' ',' ',' '},
|
||||||
{' ',' ',' ',' '}}},{2,2}},}};
|
{' ',' ',' ',' '}}},{2,2}},}};
|
||||||
int currentPiece=0;
|
int currentPiece=0;
|
||||||
int topRow=0;
|
int topRow=playfieldRows.size();
|
||||||
|
int stackSize=0;
|
||||||
int jetMarker=0;
|
int jetMarker=0;
|
||||||
|
std::cout<<"Initializing array..."<<std::endl;
|
||||||
|
for (int y=0;y<playfieldRows.size();y++){
|
||||||
|
for (int x=0;x<7;x++){
|
||||||
|
playfieldRows[y][x]=' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<"Starting."<<std::endl;
|
||||||
while (file.good()){
|
while (file.good()){
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(file,line);
|
std::getline(file,line);
|
||||||
@ -50,16 +59,7 @@ int main()
|
|||||||
currentPiece=(currentPiece+1)%5;
|
currentPiece=(currentPiece+1)%5;
|
||||||
int newRows=3-topRow+piece.second.y;
|
int newRows=3-topRow+piece.second.y;
|
||||||
//std::cout<<"Adding "<<newRows<<" rows..."<<std::endl;
|
//std::cout<<"Adding "<<newRows<<" rows..."<<std::endl;
|
||||||
if (newRows>0){
|
piecePos={2,topRow-3-piece.second.y};
|
||||||
for (int j=0;j<newRows;j++){
|
|
||||||
std::array<char,7>newRow={' ',' ',' ',' ',' ',' ',' '};
|
|
||||||
playfieldRows.insert(playfieldRows.begin(),newRow);
|
|
||||||
topRow++;
|
|
||||||
}
|
|
||||||
piecePos={2,0};
|
|
||||||
} else {
|
|
||||||
piecePos={2,topRow-3-piece.second.y};
|
|
||||||
}
|
|
||||||
while (!pieceLanded) {
|
while (!pieceLanded) {
|
||||||
char direction=line[jetMarker];
|
char direction=line[jetMarker];
|
||||||
//std::cout<<direction;
|
//std::cout<<direction;
|
||||||
@ -125,12 +125,68 @@ int main()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (piecePos.y<topRow){
|
if (piecePos.y<topRow){
|
||||||
|
stackSize+=topRow-piecePos.y;
|
||||||
topRow=piecePos.y;
|
topRow=piecePos.y;
|
||||||
}
|
}
|
||||||
pieceLanded=true;
|
pieceLanded=true;
|
||||||
} else {
|
} else {
|
||||||
piecePos.y++;
|
piecePos.y++;
|
||||||
}
|
}
|
||||||
|
for (int yy=0;yy<piece.second.y;yy++){
|
||||||
|
bool completeRow=true;
|
||||||
|
for (int xx=0;xx<7;xx++){
|
||||||
|
if (playfieldRows[piecePos.y+yy][xx]==' '){
|
||||||
|
completeRow=false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (completeRow){
|
||||||
|
//std::cout<<"Complete row found! @"<<piecePos.y<<std::endl;
|
||||||
|
int chopPoint=piecePos.y+yy;
|
||||||
|
int topRowDiff=chopPoint-topRow;
|
||||||
|
//std::cout<<"Top Row Diff: "<<topRowDiff<<std::endl;
|
||||||
|
/*for (int yyy=chopPoint-topRowDiff;yyy<playfieldRows.size();yyy++){
|
||||||
|
for (int xxx=0;xxx<9;xxx++){
|
||||||
|
if (xxx==0||xxx==8){
|
||||||
|
std::cout<<'|';
|
||||||
|
} else {
|
||||||
|
std::cout<<playfieldRows[yyy][xxx-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<std::endl;
|
||||||
|
}*/
|
||||||
|
//std::cout<<"==========="<<std::endl;
|
||||||
|
for (int yyy=chopPoint;yyy<playfieldRows.size();yyy++){
|
||||||
|
for (int xxx=0;xxx<7;xxx++){
|
||||||
|
playfieldRows[yyy][xxx]=' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//std::cout<<"Cleared current board!"<<std::endl;
|
||||||
|
topRow=playfieldRows.size();
|
||||||
|
for (int yyy=chopPoint-topRowDiff;yyy<chopPoint;yyy++){
|
||||||
|
for (int xxx=0;xxx<7;xxx++){
|
||||||
|
playfieldRows[yyy+playfieldRows.size()-chopPoint][xxx]=playfieldRows[yyy][xxx];
|
||||||
|
if (playfieldRows[yyy+playfieldRows.size()-chopPoint][xxx]!=' '&&yyy+playfieldRows.size()-chopPoint<topRow){
|
||||||
|
topRow=yyy+playfieldRows.size()-chopPoint;
|
||||||
|
}
|
||||||
|
playfieldRows[yyy][xxx]=' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*std::cout<<"New board:"<<std::endl;
|
||||||
|
for (int yyy=playfieldRows.size()-topRowDiff;yyy<playfieldRows.size();yyy++){
|
||||||
|
for (int xxx=0;xxx<9;xxx++){
|
||||||
|
if (xxx==0||xxx==8){
|
||||||
|
std::cout<<'|';
|
||||||
|
} else {
|
||||||
|
std::cout<<playfieldRows[yyy][xxx-1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<std::endl;
|
||||||
|
}
|
||||||
|
std::cout<<"Shifted down elements. New Top:"<<topRow<<std::endl;*/
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
jetMarker=(jetMarker+1)%line.length();
|
jetMarker=(jetMarker+1)%line.length();
|
||||||
//std::cout<<"jetMarker is now "<<jetMarker<<std::endl;
|
//std::cout<<"jetMarker is now "<<jetMarker<<std::endl;
|
||||||
/*for (int y=0;y<playfieldRows.size();y++){
|
/*for (int y=0;y<playfieldRows.size();y++){
|
||||||
@ -163,7 +219,7 @@ int main()
|
|||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
std::cout<<std::endl;
|
std::cout<<std::endl;
|
||||||
std::cout<<"The tower is "<<playfieldRows.size()-topRow<<" tall!"<<std::endl;
|
std::cout<<"The tower is "<<stackSize<<" tall!"<<std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user