|
|
|
#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;
|
|
|
|
|
|
|
|
struct Node{
|
|
|
|
std::string val;
|
|
|
|
std::string startingNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Node> currentNodes;
|
|
|
|
int instructionIndex=0;
|
|
|
|
|
|
|
|
//133x133
|
|
|
|
vi2d lineStartNode;
|
|
|
|
vi2d lineEndNode;
|
|
|
|
bool drawInitialGrid=true;
|
|
|
|
int path=0;
|
|
|
|
|
|
|
|
void doStuff2(){
|
|
|
|
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){
|
|
|
|
std::string node=line.substr(0,3);
|
|
|
|
nodes[node]={line.substr(line.find('(')+1,3),line.substr(line.find(')')-3,3)};
|
|
|
|
if(node[2]=='A'){
|
|
|
|
currentNodes.push_back({node,node});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lineNumb++;
|
|
|
|
}
|
|
|
|
wait(10000);
|
|
|
|
long long step=0;
|
|
|
|
bool notAtZ=true;
|
|
|
|
while(notAtZ){
|
|
|
|
notAtZ=false;
|
|
|
|
for(int counter=0;auto&[val,startingNode]:currentNodes){
|
|
|
|
char currentInstruction=instructions[instructionIndex];
|
|
|
|
int numb=0;
|
|
|
|
for(int i=0;i<3;i++){
|
|
|
|
numb*=26;
|
|
|
|
numb+=val[i]-'A';
|
|
|
|
}
|
|
|
|
lineStartNode.x=(numb%133)*24+12;
|
|
|
|
lineStartNode.y=(numb/133)*8;
|
|
|
|
if(currentInstruction=='R'){
|
|
|
|
val=nodes[val].second;
|
|
|
|
}else{
|
|
|
|
val=nodes[val].first;
|
|
|
|
}
|
|
|
|
if(val[2]!='Z'){
|
|
|
|
notAtZ=true;
|
|
|
|
}
|
|
|
|
numb=0;
|
|
|
|
for(int i=0;i<3;i++){
|
|
|
|
numb*=26;
|
|
|
|
numb+=val[i]-'A';
|
|
|
|
}
|
|
|
|
lineEndNode.x=(numb%133)*24+12;
|
|
|
|
lineEndNode.y=(numb/133)*8;
|
|
|
|
path=counter;
|
|
|
|
if(counter<7){
|
|
|
|
wait(0);
|
|
|
|
}
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
instructionIndex=(instructionIndex+1)%instructions.length();
|
|
|
|
step++;
|
|
|
|
}
|
|
|
|
std::cout<<step<<std::endl;
|
|
|
|
break;
|
|
|
|
//wait(0); //Wait for 0ms and render the screen (calls draw())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
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!
|
|
|
|
if(drawInitialGrid){
|
|
|
|
Clear(BLACK);
|
|
|
|
drawInitialGrid=false;
|
|
|
|
for(int i=0;i<26*26*26;i++){
|
|
|
|
std::string node="";
|
|
|
|
int val=i;
|
|
|
|
for(int i=0;i<3;i++){
|
|
|
|
node=std::string(1,'A'+(val%26))+node;
|
|
|
|
val/=26;
|
|
|
|
}
|
|
|
|
int y=(i/133*8);
|
|
|
|
int x=(i%133)*24;
|
|
|
|
|
|
|
|
DrawString(x,y,node,VERY_DARK_GREY);
|
|
|
|
for(auto&[key,value]:nodes){
|
|
|
|
if(key==node){
|
|
|
|
DrawString(x,y,node,GREY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
const static std::array<Pixel,7>colors{RED,BLUE,GREEN,YELLOW,MAGENTA,CYAN,WHITE};
|
|
|
|
SetPixelMode(Pixel::ALPHA);
|
|
|
|
FillRect({0,0},GetScreenSize(),{0,0,0,1});
|
|
|
|
DrawLine(lineStartNode,lineEndNode,{colors[path%7].r,colors[path%7].g,colors[path%7].b,100},0xDDDDDDDD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#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::doStuff2,this);
|
|
|
|
|
|
|
|
if(waitForRender){
|
|
|
|
draw();
|
|
|
|
waitForRender=false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
AoC2023 game;
|
|
|
|
if (game.Construct(3186, 1062, 2, 2))
|
|
|
|
game.Start();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#pragma endregion
|