You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
AoC2022_Day17/main.cpp

228 lines
10 KiB

2 years ago
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
#include <cassert>
2 years ago
using namespace olc;
std::array<std::array<char,7>,30000000>playfieldRows;
2 years ago
int main()
{
std::ifstream file("testinput");
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=playfieldRows.size();
int stackSize=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()){
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;
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){
stackSize+=topRow-piecePos.y;
topRow=piecePos.y;
}
pieceLanded=true;
} else {
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();
//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 "<<stackSize<<" tall!"<<std::endl;
}
}
2 years ago
return 0;
}