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.
241 lines
6.2 KiB
241 lines
6.2 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 = 3;
|
|
Run runInput=FILE1;
|
|
|
|
struct numb{
|
|
vi2d pos;
|
|
int number;
|
|
};
|
|
|
|
std::vector<numb>parts;
|
|
std::vector<std::vector<char>>board;
|
|
|
|
void doStuff2(){
|
|
while(true){ //lines is accessible as a global.
|
|
int sum=0;
|
|
vi2d pos={};
|
|
for(std::string&line:lines){
|
|
std::string numb="";
|
|
bool isAdjacent=false;
|
|
std::vector<char>row;
|
|
vi2d initialNumberPos={};
|
|
for(int i=0;i<line.size();i++){
|
|
if(line[i]>='0'&&line[i]<='9'){
|
|
if(numb.length()==0){
|
|
initialNumberPos=pos;
|
|
}
|
|
numb+=line[i];
|
|
}else
|
|
if(numb.length()>0){
|
|
//A number has been parsed.
|
|
parts.push_back({initialNumberPos,atoi(numb.c_str())});
|
|
numb="";
|
|
}
|
|
row.push_back(line[i]);
|
|
pos.x++;
|
|
}
|
|
if(numb.length()>0){
|
|
//A number has been parsed.
|
|
parts.push_back({initialNumberPos,atoi(numb.c_str())});
|
|
}
|
|
board.push_back(row);
|
|
pos.y++;
|
|
pos.x=0;
|
|
std::cout<<line<<std::endl;
|
|
}
|
|
|
|
for(numb&number:parts){
|
|
int myNumb=number.number;
|
|
vi2d startingPos=number.pos;
|
|
while(myNumb>0){
|
|
auto validatePos=[&](vi2d checkPos){
|
|
return startingPos.y+checkPos.y>0&&startingPos.y+checkPos.y<board.size()-1&&
|
|
startingPos.x+checkPos.x>0&&startingPos.x+checkPos.x<board[startingPos.y+checkPos.y].size()-1;
|
|
};
|
|
auto testChar=[&](char test){
|
|
if(test!='.'&&!(test>='0'&&test<='9')){
|
|
sum+=number.number;
|
|
std::cout<<number.number<<" was adjacent to a symbol"<<std::endl;
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
vi2d checkPos={1,0};
|
|
if(validatePos({1,0})){if(testChar(board[startingPos.y][startingPos.x+1]))break;}
|
|
if(validatePos({0,1})){if(testChar(board[startingPos.y+1][startingPos.x]))break;}
|
|
if(validatePos({-1,0})){if(testChar(board[startingPos.y][startingPos.x-1]))break;}
|
|
if(validatePos({0,-1})){if(testChar(board[startingPos.y-1][startingPos.x]))break;}
|
|
if(validatePos({1,1})){if(testChar(board[startingPos.y+1][startingPos.x+1]))break;}
|
|
if(validatePos({1,-1})){if(testChar(board[startingPos.y-1][startingPos.x+1]))break;}
|
|
if(validatePos({-1,1})){if(testChar(board[startingPos.y+1][startingPos.x-1]))break;}
|
|
if(validatePos({-1,-1})){if(testChar(board[startingPos.y-1][startingPos.x-1]))break;}
|
|
myNumb/=10;
|
|
startingPos.x++;
|
|
}
|
|
}
|
|
std::cout<<sum<<std::endl;
|
|
wait(0); //Wait for 0ms and render the screen (calls draw())
|
|
break;
|
|
}
|
|
}
|
|
|
|
void doStuff(){
|
|
while(true){ //lines is accessible as a global.
|
|
int sum=0;
|
|
vi2d pos={};
|
|
for(std::string&line:lines){
|
|
std::string numb="";
|
|
bool isAdjacent=false;
|
|
std::vector<char>row;
|
|
vi2d initialNumberPos={};
|
|
for(int i=0;i<line.size();i++){
|
|
if(line[i]>='0'&&line[i]<='9'){
|
|
if(numb.length()==0){
|
|
initialNumberPos=pos;
|
|
}
|
|
numb+=line[i];
|
|
}else
|
|
if(numb.length()>0){
|
|
//A number has been parsed.
|
|
parts.push_back({initialNumberPos,atoi(numb.c_str())});
|
|
numb="";
|
|
}
|
|
row.push_back(line[i]);
|
|
pos.x++;
|
|
}
|
|
if(numb.length()>0){
|
|
//A number has been parsed.
|
|
parts.push_back({initialNumberPos,atoi(numb.c_str())});
|
|
}
|
|
board.push_back(row);
|
|
pos.y++;
|
|
pos.x=0;
|
|
std::cout<<line<<std::endl;
|
|
}
|
|
|
|
for(numb&number:parts){
|
|
int myNumb=number.number;
|
|
vi2d startingPos=number.pos;
|
|
while(myNumb>0){
|
|
auto validatePos=[&](vi2d checkPos){
|
|
return startingPos.y+checkPos.y>0&&startingPos.y+checkPos.y<board.size()-1&&
|
|
startingPos.x+checkPos.x>0&&startingPos.x+checkPos.x<board[startingPos.y+checkPos.y].size()-1;
|
|
};
|
|
auto testChar=[&](char test){
|
|
if(test!='.'&&!(test>='0'&&test<='9')){
|
|
sum+=number.number;
|
|
std::cout<<number.number<<" was adjacent to a symbol"<<std::endl;
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
vi2d checkPos={1,0};
|
|
if(validatePos({1,0})){if(testChar(board[startingPos.y][startingPos.x+1]))break;}
|
|
if(validatePos({0,1})){if(testChar(board[startingPos.y+1][startingPos.x]))break;}
|
|
if(validatePos({-1,0})){if(testChar(board[startingPos.y][startingPos.x-1]))break;}
|
|
if(validatePos({0,-1})){if(testChar(board[startingPos.y-1][startingPos.x]))break;}
|
|
if(validatePos({1,1})){if(testChar(board[startingPos.y+1][startingPos.x+1]))break;}
|
|
if(validatePos({1,-1})){if(testChar(board[startingPos.y-1][startingPos.x+1]))break;}
|
|
if(validatePos({-1,1})){if(testChar(board[startingPos.y+1][startingPos.x-1]))break;}
|
|
if(validatePos({-1,-1})){if(testChar(board[startingPos.y-1][startingPos.x-1]))break;}
|
|
myNumb/=10;
|
|
startingPos.x++;
|
|
}
|
|
}
|
|
std::cout<<sum<<std::endl;
|
|
wait(0); //Wait for 0ms and render the screen (calls draw())
|
|
break;
|
|
}
|
|
}
|
|
|
|
void draw(){ //Only use Sprites! If using decals, you must reference global variables!
|
|
Clear(BLACK);
|
|
int count=0;
|
|
vi2d pos;
|
|
for(std::vector<char>&row:board){
|
|
for(char c:row){
|
|
DrawString(pos,std::string(1,c));
|
|
pos.x+=8;
|
|
}
|
|
pos.y+=8;
|
|
pos.x=0;
|
|
}
|
|
for(numb&number:parts){
|
|
std::string numbStr="";
|
|
int numb=number.number;
|
|
while(numb>0){
|
|
numbStr=std::to_string(numb%10)+numbStr;
|
|
numb/=10;
|
|
}
|
|
DrawString(number.pos*8,numbStr,RED);
|
|
}
|
|
}
|
|
|
|
#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(1280, 960, 2, 2))
|
|
game.Start();
|
|
return 0;
|
|
}
|
|
#pragma endregion |