|
|
|
#define OLC_PGE_APPLICATION
|
|
|
|
#include "pixelGameEngine.h"
|
|
|
|
#include "olcutils.h"
|
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
using namespace olc;
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
std::ifstream file("input");
|
|
|
|
std::vector<std::array<char,7>>playfieldRows;
|
|
|
|
std::array<std::array<char,4>,4>piece{{
|
|
|
|
{' ',' ',' ',' '},
|
|
|
|
{' ',' ',' ',' '},
|
|
|
|
{' ',' ',' ',' '},
|
|
|
|
{' ',' ',' ',' '}}};
|
|
|
|
vi2d piecePos={0,0};
|
|
|
|
std::array<std::pair<std::array<std::array<char,4>,4>,vi2d>,5>pieces{{
|
|
|
|
{{{{'#','#','#','#'},
|
|
|
|
{' ',' ',' ',' '},
|
|
|
|
{' ',' ',' ',' '},
|
|
|
|
{' ',' ',' ',' '}}},{4,1}},
|
|
|
|
{{{{' ','#',' ',' '},
|
|
|
|
{'#','#','#',' '},
|
|
|
|
{' ','#',' ',' '},
|
|
|
|
{' ',' ',' ',' '}}},{3,3}},
|
|
|
|
{{{{' ',' ','#',' '},
|
|
|
|
{' ',' ','#',' '},
|
|
|
|
{'#','#','#',' '},
|
|
|
|
{' ',' ',' ',' '}}},{3,3}},
|
|
|
|
{{{{'#',' ',' ',' '},
|
|
|
|
{'#',' ',' ',' '},
|
|
|
|
{'#',' ',' ',' '},
|
|
|
|
{'#',' ',' ',' '}}},{1,4}},
|
|
|
|
{{{{'#','#',' ',' '},
|
|
|
|
{'#','#',' ',' '},
|
|
|
|
{' ',' ',' ',' '},
|
|
|
|
{' ',' ',' ',' '}}},{2,2}},}};
|
|
|
|
int currentPiece=0;
|
|
|
|
int topRow=0;
|
|
|
|
int jetMarker=0;
|
|
|
|
while (file.good()){
|
|
|
|
std::string line;
|
|
|
|
std::getline(file,line);
|
|
|
|
if (line.length()>0){
|
|
|
|
std::cout<<line<<std::endl;
|
|
|
|
for (int i=0;i<2022;i++){
|
|
|
|
bool pieceLanded=false;
|
|
|
|
std::pair<std::array<std::array<char,4>,4>,vi2d>piece=pieces[currentPiece];
|
|
|
|
currentPiece=(currentPiece+1)%5;
|
|
|
|
int newRows=3-topRow+piece.second.y;
|
|
|
|
//std::cout<<"Adding "<<newRows<<" rows..."<<std::endl;
|
|
|
|
if (newRows>0){
|
|
|
|
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) {
|
|
|
|
char direction=line[jetMarker];
|
|
|
|
//std::cout<<direction;
|
|
|
|
switch (direction){
|
|
|
|
case '<':{
|
|
|
|
if (piecePos.x>0){
|
|
|
|
for (int yy=0;yy<piece.second.y;yy++){
|
|
|
|
for (int xx=0;xx<piece.second.x;xx++){
|
|
|
|
if (piece.first[yy][xx]!=' '&&playfieldRows[piecePos.y+yy][piecePos.x+xx-1]=='#'){
|
|
|
|
//std::cout<<"Collision - Ignore Move Left"<<std::endl;
|
|
|
|
goto resolveX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//std::cout<<"Move left"<<std::endl;
|
|
|
|
piecePos.x--;
|
|
|
|
} else {
|
|
|
|
//std::cout<<"Ignore Move left"<<std::endl;
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
case '>':{
|
|
|
|
if (piecePos.x+piece.second.x<7){
|
|
|
|
for (int yy=0;yy<piece.second.y;yy++){
|
|
|
|
for (int xx=0;xx<piece.second.x;xx++){
|
|
|
|
if (piece.first[yy][xx]!=' '&&playfieldRows[piecePos.y+yy][piecePos.x+xx+1]=='#'){
|
|
|
|
//std::cout<<"Collision - Ignore Move Right"<<std::endl;
|
|
|
|
goto resolveX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//std::cout<<"Move right"<<std::endl;
|
|
|
|
piecePos.x++;
|
|
|
|
} else {
|
|
|
|
//std::cout<<"Ignore Move Right"<<std::endl;
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
default:{
|
|
|
|
std::cout<<"An invalid character! '"<<direction<<"'"<<std::endl;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resolveX:
|
|
|
|
bool landed=false;
|
|
|
|
if (piecePos.y+piece.second.y<playfieldRows.size()){
|
|
|
|
for (int yy=0;yy<piece.second.y;yy++){
|
|
|
|
for (int xx=0;xx<piece.second.x;xx++){
|
|
|
|
if (piece.first[yy][xx]!=' '&&playfieldRows[piecePos.y+yy+1][piecePos.x+xx]=='#'){
|
|
|
|
landed=true;
|
|
|
|
goto resolveY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
landed=true;
|
|
|
|
}
|
|
|
|
resolveY:
|
|
|
|
if (landed){
|
|
|
|
for (int yy=0;yy<piece.second.y;yy++){
|
|
|
|
for (int xx=0;xx<piece.second.x;xx++){
|
|
|
|
if (piece.first[yy][xx]=='#'){
|
|
|
|
playfieldRows[piecePos.y+yy][piecePos.x+xx]=piece.first[yy][xx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (piecePos.y<topRow){
|
|
|
|
topRow=piecePos.y;
|
|
|
|
}
|
|
|
|
pieceLanded=true;
|
|
|
|
} else {
|
|
|
|
piecePos.y++;
|
|
|
|
}
|
|
|
|
jetMarker=(jetMarker+1)%line.length();
|
|
|
|
//std::cout<<"jetMarker is now "<<jetMarker<<std::endl;
|
|
|
|
/*for (int y=0;y<playfieldRows.size();y++){
|
|
|
|
std::cout<<" ";
|
|
|
|
for (int x=0;x<9;x++){
|
|
|
|
if (x==0||x==8) {
|
|
|
|
std::cout<<"|";
|
|
|
|
} else {
|
|
|
|
if (x-1>=piecePos.x&&y>=piecePos.y&&x-1<piecePos.x+piece.second.x&&y<piecePos.y+piece.second.y){
|
|
|
|
std::cout<<piece.first[y-piecePos.y][x-1-piecePos.x];
|
|
|
|
} else {
|
|
|
|
std::cout<<playfieldRows[y][x-1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout<<std::endl;
|
|
|
|
}
|
|
|
|
std::cout<<"=================="<<std::endl;*/
|
|
|
|
}
|
|
|
|
/*std::cout<<"=================="<<std::endl;
|
|
|
|
for (int y=0;y<playfieldRows.size();y++){
|
|
|
|
for (int x=0;x<9;x++){
|
|
|
|
if (x==0||x==8) {
|
|
|
|
std::cout<<"|";
|
|
|
|
} else {
|
|
|
|
std::cout<<playfieldRows[y][x-1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout<<std::endl;
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
std::cout<<std::endl;
|
|
|
|
std::cout<<"The tower is "<<playfieldRows.size()-topRow<<" tall!"<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|