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.
AoC2023/Day 8/main.cpp

117 lines
2.3 KiB

#pragma region Hidden Setup Stuff
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
using namespace olc;
enum Run{
FILE1,
FILE2
};
// Override base class with your custom functionality
class AoC2023 : public olc::PixelGameEngine
{
std::vector<std::string>lines;
bool waitForRender=false;
void wait(int pauseMs=0){
waitForRender=true;
while(waitForRender);
std::this_thread::sleep_for(std::chrono::milliseconds(pauseMs));
}
#pragma endregion
const int DAY = 8;
Run runInput=FILE2;
std::map<std::string,std::pair<std::string,std::string>>nodes;
std::string instructions;
std::string currentNode="AAA";
int instructionIndex=0;
void doStuff(){
while(true){ //lines is accessible as a global.
bool firstLine=true;
for(int lineNumb=0;std::string&line:lines){
if(lineNumb==0){
instructions=line;
}else
if(lineNumb>=2){
nodes[line.substr(0,3)]={line.substr(line.find('(')+1,3),line.substr(line.find(')')-3,3)};
}
lineNumb++;
}
int step=0;
while(currentNode!="ZZZ"){
char currentInstruction=instructions[instructionIndex];
if(currentInstruction=='R'){
currentNode=nodes[currentNode].second;
}else{
currentNode=nodes[currentNode].first;
}
instructionIndex=(instructionIndex+1)%instructions.length();
step++;
}
std::cout<<step<<std::endl;
break;
//wait(0); //Wait for 0ms and render the screen (calls draw())
}
}
void draw(){ //Only use Sprites! If using decals, you must reference global variables!
Clear(BLACK);
int count=0;
for(std::string&line:lines){
DrawString({0,count*32},line,WHITE,4);
count++;
}
}
#pragma region Hidden Engine Stuff
public:
AoC2023()
{
// Name your application
std::string fileName="day"+std::to_string(DAY)+"_1.txt";
if(runInput==FILE2){fileName="day"+std::to_string(DAY)+"_2.txt";}
std::ifstream file(fileName);
while(file.good()){
std::string line;
std::getline(file,line);
lines.push_back(line);
}
sAppName = "Advent of Code 2023 - Day "+std::to_string(DAY);
}
public:
bool OnUserCreate() override
{
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
static std::thread aocSolver(&AoC2023::doStuff,this);
if(waitForRender){
draw();
waitForRender=false;
}
return true;
}
};
int main()
{
AoC2023 game;
if (game.Construct(640, 480, 2, 2))
game.Start();
return 0;
}
#pragma endregion