Part 1 completed!

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent f898c10f72
commit 0e3364917e
  1. BIN
      C++ProjectTemplate
  2. 1
      input
  3. 238
      main.cpp
  4. 29
      out
  5. 171
      part1
  6. 1
      testinput

Binary file not shown.

File diff suppressed because one or more lines are too long

@ -1,89 +1,171 @@
#define OLC_PGE_APPLICATION #define OLC_PGE_APPLICATION
#include "pixelGameEngine.h" #include "pixelGameEngine.h"
#include "olcutils.h" #include "olcutils.h"
#include <cassert>
using namespace olc; using namespace olc;
class Example : public olc::PixelGameEngine
{
public:
Example()
{
sAppName = "Example";
}
public:
bool RayVsRect(const vf2d ray_origin, const vf2d ray_dir, const olc::utils::geom2d::rect<float> target, vf2d&contact_point, vf2d&contact_normal, float&t_hit_near){
contact_normal = { 0, 0 };
contact_point = { 0, 0 };
vf2d t_near = {(target.pos.x - ray_origin.x) / ray_dir.x, (target.pos.y - ray_origin.y) / ray_dir.y};
vf2d t_far = {(target.pos.x + target.size.x - ray_origin.x) / ray_dir.x, (target.pos.y + target.size.y - ray_origin.y) / ray_dir.y};
if (t_near.x > t_far.x) {float b; b = t_near.x; t_near.x = t_far.x; t_far.x = b;};
if (t_near.y > t_far.y) {float b; b = t_near.y; t_near.y = t_far.y; t_far.y = b;};
if (t_near.x > t_far.y || t_near.y > t_far.x) return false;
t_hit_near = fmax(t_near.x, t_near.y);
float t_hit_far = fmin(t_far.x, t_far.y);
if (t_hit_far < 0) return false;
contact_point.x = ray_origin.x + t_hit_near * ray_dir.x;
contact_point.y = ray_origin.y + t_hit_near * ray_dir.y;
if (t_near.x > t_near.y)
if ( 1.0f / ray_dir.x < 0)
contact_normal = { 1, 0 };
else
contact_normal = { -1, 0};
else
if ( t_near.x < t_near.y)
if ( 1.0f / ray_dir.y < 0)
contact_normal = { 0, 1 };
else
contact_normal = { 0, -1 };
return true;
}
vf2d originPoint={16,16};
bool OnUserCreate() override
{
// Called once at the start, so create things here
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
vf2d velocity={(GetKey(D).bHeld-GetKey(A).bHeld)*20*fElapsedTime,(GetKey(S).bHeld-GetKey(W).bHeld)*20*fElapsedTime};
vf2d contact_point;
vf2d contact_normal;
float t_hit_near;
Clear(Pixel(64,64,255));
if (!olc::utils::geom2d::overlaps(olc::utils::geom2d::circle<float>{originPoint+velocity,5},olc::utils::geom2d::rect<float>{{32,32},{64,32}})) {
originPoint+=velocity;
DrawCircle(originPoint,5);
} else {
DrawCircle(originPoint,5,RED);
}
DrawLine(originPoint,GetMousePos());
DrawRect({32,32},{64,32},RayVsRect(originPoint, GetMousePos()-originPoint, olc::utils::geom2d::rect<float>{{32,32},{64,32}},contact_point,contact_normal,t_hit_near)&&t_hit_near<1?YELLOW:WHITE);
return true;
}
};
int main() int main()
{ {
Example demo; std::ifstream file("input");
if (demo.Construct(128, 120, 8, 8)) std::vector<std::array<char,7>>playfieldRows;
demo.Start(); 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; return 0;
} }

29
out

@ -0,0 +1,29 @@
Check Java/
Check C/
C++/scripts is a directory
C++/scripts/build.sh is a file
C++/scripts/commit.sh is a file
C++/scripts/debug.sh is a file
C++/scripts/lines.sh is a file
C++/scripts/release.sh is a file
C++/scripts/temp is a file
C++/scripts/web.sh is a file
Check C++/
C++/scripts is a directory
Check C++/scripts/
md5: http://sig.projectdivar.com/sigonasr2/SigScript/raw/branch/main/C++/scripts/md5
filelist: http://sig.projectdivar.com/sigonasr2/SigScript/raw/branch/main/C++/scripts/filelist
Check scripts/
utils/.coauthors is a file
utils/define.sh is a file
utils/main.sh is a file
utils/search.sh is a file
utils/.updateDirectories is a file
Check utils/
md5: http://sig.projectdivar.com/sigonasr2/SigScript/raw/branch/main/utils/md5
filelist: http://sig.projectdivar.com/sigonasr2/SigScript/raw/branch/main/utils/filelist
Running program...

171
part1

@ -0,0 +1,171 @@
#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;
}

@ -0,0 +1 @@
>>><<><>><<<>><>>><<<>>><<<><<<>><>><<>>
Loading…
Cancel
Save