Part 1 done!

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent bc094bebdc
commit 8d8db664f5
  1. BIN
      C++ProjectTemplate
  2. 124
      main.cpp
  3. 2
      testinput

Binary file not shown.

@ -8,6 +8,12 @@ using namespace olc;
vi2d upperLeftCoords={500,0};
vi2d lowerRightCoords={500,0};
std::vector<std::vector<char>>board;
int sum=0;
float timePerTick=0.03;
float acc=0;
float acc2=0;
vi2d sandCoord={500,0};
bool simulationDone=false;
struct Connection{
std::vector<vi2d>coords;
@ -23,11 +29,100 @@ vi2d getArrayCoords(vi2d pos){
return pos-upperLeftCoords;
}
char getBoard(vi2d pos){
vi2d actualCoords=getArrayCoords(pos);
if (actualCoords.x<0||actualCoords.y<0||actualCoords.x>=board[0].size()||actualCoords.y>=board.size()) {
simulationDone=true;
return 'X';
}
return board[actualCoords.y][actualCoords.x];
}
void modifyBoard(vi2d pos,char val){
vi2d actualCoords=getArrayCoords(pos);
board[actualCoords.y][actualCoords.x]=val;
}
class SandSim : public olc::PixelGameEngine
{
public:
SandSim()
{
sAppName = "AoC Day 14 Part I";
}
public:
bool OnUserCreate() override
{
// Called once at the start, so create things here
return true;
}
void updateGame(){
if (!simulationDone){
vi2d boardCoords=getArrayCoords(sandCoord);
if (getBoard({sandCoord.x,sandCoord.y+1})==' '){
//Fall straight down.
sandCoord.y++;
} else
if (getBoard({sandCoord.x-1,sandCoord.y+1})==' '){
sandCoord.x--;
sandCoord.y++;
} else
if (getBoard({sandCoord.x+1,sandCoord.y+1})==' '){
sandCoord.x++;
sandCoord.y++;
} else {
if (!simulationDone){
sum++;
modifyBoard(sandCoord,'.');
sandCoord={500,0};
}
}
}
}
bool OnUserUpdate(float fElapsedTime) override
{
acc+=fElapsedTime;
acc2+=fElapsedTime;
if (acc>=timePerTick){
acc-=timePerTick;
updateGame();
}
if (acc2>=0.01){
if (GetKey(RIGHT).bHeld){
timePerTick=std::clamp(timePerTick+0.001f,0.f,1.f);
acc2=0;
}
if (GetKey(LEFT).bHeld){
timePerTick=std::clamp(timePerTick-0.001f,0.f,1.f);
acc2=0;
}
}
//DrawStringDecal({0,0},std::to_string((int)(timePerTick*1000))+"ms");
DrawStringDecal({1,(float)(ScreenHeight()-GetTextSize("S").y*0.4)-1},"Sand Pieces: "+std::to_string(sum),BLACK,{0.4,0.4});
DrawStringDecal({0,(float)(ScreenHeight()-GetTextSize("S").y*0.4)},"Sand Pieces: "+std::to_string(sum),WHITE,{0.4,0.4});
// called once per frame
for (int x = upperLeftCoords.x; x <= lowerRightCoords.x; x++){
for (int y = upperLeftCoords.y; y <= lowerRightCoords.y; y++){
vi2d coords=getArrayCoords({x,y});
if (x==sandCoord.x&&y==sandCoord.y){
Draw(coords.x, coords.y,YELLOW);
} else {
Draw(coords.x, coords.y,
(board[coords.y][coords.x]==' ')?Pixel(40,40,40):
(board[coords.y][coords.x]=='.')?DARK_YELLOW:
Pixel(255,255,255));
}
}
}
return true;
}
};
int main()
{
std::vector<Connection>connections;
@ -75,6 +170,16 @@ int main()
}break;
}
}
int numb1=std::atoi(num1.c_str());
int numb2=std::atoi(num2.c_str());
c.coords.push_back({numb1,numb2});
upperLeftCoords.x=std::min(numb1,upperLeftCoords.x);
upperLeftCoords.y=std::min(numb2,upperLeftCoords.y);
lowerRightCoords.x=std::max(numb1,lowerRightCoords.x);
lowerRightCoords.y=std::max(numb2,lowerRightCoords.y);
std::cout<<"read "<<num1<<"//"<<num2<<std::endl;
num1="";
num2="";
connections.push_back(c);
}
std::cout<<line<<std::endl;
@ -97,31 +202,22 @@ int main()
vi2d&startPos=c.coords[0];
for (int j=1;j<c.coords.size();j++){
vi2d&endPos=c.coords[j];
modifyBoard(startPos,'#');
while (startPos!=endPos){
modifyBoard(startPos,'#');
if (startPos.x!=endPos.x){
startPos.x+=(endPos.x-startPos.x)/std::abs(startPos.x-endPos.x);
}
if (startPos.y!=endPos.y){
startPos.y+=(endPos.y-startPos.y)/std::abs(startPos.y-endPos.y);
}
modifyBoard(startPos,'#');
}
}
}
for (int y=0;y<board.size();y++){
for (int x=0;x<board[y].size();x++){
if (board[y][x]!=' '){
std::cout<<board[y][x];
} else
if (x+upperLeftCoords.x==500){
std::cout<<'|';
} else {
std::cout<<board[y][x];
}
}
std::cout<<std::endl;
}
SandSim demo;
if (demo.Construct(54, 170, 8, 8))
demo.Start();
return 0;
}

@ -0,0 +1,2 @@
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9
Loading…
Cancel
Save