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.
231 lines
5.7 KiB
231 lines
5.7 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
|
|
#include <limits>
|
|
|
|
const int DAY = 2;
|
|
Run runInput=FILE2;
|
|
|
|
void doStuff2(){
|
|
while(true){ //lines is accessible as a global.
|
|
int id=1;
|
|
long powerSum=0;
|
|
for(std::string&line:lines){
|
|
bool allowed=true;
|
|
std::string strippedLine=line.substr(line.find(':')+2);
|
|
size_t semiColon=strippedLine.find(';');
|
|
int min_red=0,min_green=0,min_blue=0;
|
|
#pragma region Parse by Semi-Colon
|
|
while(semiColon!=std::string::npos){
|
|
std::string pull=strippedLine.substr(0,semiColon);
|
|
|
|
#pragma region Parse by Spaces
|
|
size_t space=pull.find(' ');
|
|
while(space!=std::string::npos){
|
|
std::string numb=pull.substr(0,space);
|
|
int number=atoi(numb.c_str());
|
|
if(pull[space+1]=='b'){
|
|
if(number>min_blue)min_blue=number;
|
|
}else
|
|
if(pull[space+1]=='g'){
|
|
if(number>min_green)min_green=number;
|
|
}else
|
|
if(pull[space+1]=='r'){
|
|
if(number>min_red)min_red=number;
|
|
}
|
|
if(pull.find(' ',space)==std::string::npos)break;
|
|
pull=pull.substr(pull.find(' ',space)+1);
|
|
space=pull.find(' ');
|
|
}
|
|
#pragma endregion
|
|
|
|
strippedLine=strippedLine.substr(semiColon+2);
|
|
semiColon=strippedLine.find(';');
|
|
}
|
|
#pragma endregion
|
|
#pragma region Parse by Spaces
|
|
std::string pull=strippedLine;
|
|
size_t space=pull.find(' ');
|
|
while(space!=std::string::npos){
|
|
std::string numb=pull.substr(0,space);
|
|
int number=atoi(numb.c_str());
|
|
if(pull[space+1]=='b'){
|
|
if(number>min_blue)min_blue=number;
|
|
}else
|
|
if(pull[space+1]=='g'){
|
|
if(number>min_green)min_green=number;
|
|
}else
|
|
if(pull[space+1]=='r'){
|
|
if(number>min_red)min_red=number;
|
|
}
|
|
if(pull.find(' ',space)==std::string::npos)break;
|
|
pull=pull.substr(pull.find(' ',space)+1);
|
|
space=pull.find(' ');
|
|
}
|
|
#pragma endregion
|
|
|
|
uint32_t power=min_red*min_green*min_blue;
|
|
std::cout<<"======="<<std::endl;
|
|
powerSum+=power;
|
|
id++;
|
|
}
|
|
|
|
std::cout<<"Sum of Power: "<<powerSum<<std::endl;
|
|
break;
|
|
|
|
//wait(0); //Wait for 0ms and render the screen (calls draw())
|
|
}
|
|
}
|
|
|
|
void doStuff(){
|
|
while(true){ //lines is accessible as a global.
|
|
int id=1;
|
|
int limit_red=12,limit_green=13,limit_blue=14;
|
|
int idSum=0;
|
|
for(std::string&line:lines){
|
|
bool allowed=true;
|
|
std::string strippedLine=line.substr(line.find(':')+2);
|
|
size_t semiColon=strippedLine.find(';');
|
|
#pragma region Parse by Semi-Colon
|
|
while(semiColon!=std::string::npos){
|
|
int red=0,green=0,blue=0;
|
|
std::string pull=strippedLine.substr(0,semiColon);
|
|
|
|
#pragma region Parse by Spaces
|
|
size_t space=pull.find(' ');
|
|
while(space!=std::string::npos){
|
|
std::string numb=pull.substr(0,space);
|
|
int number=atoi(numb.c_str());
|
|
if(pull[space+1]=='b'){
|
|
blue=number;
|
|
}else
|
|
if(pull[space+1]=='g'){
|
|
green=number;
|
|
}else
|
|
if(pull[space+1]=='r'){
|
|
red=number;
|
|
}
|
|
if(pull.find(' ',space)==std::string::npos)break;
|
|
pull=pull.substr(pull.find(' ',space)+1);
|
|
space=pull.find(' ');
|
|
}
|
|
#pragma endregion
|
|
|
|
std::cout<<"Pulled out "<<red<<" red, "<<blue<<" blue, "<<green<<" green"<<std::endl;
|
|
|
|
if(allowed)allowed=!(red>limit_red||green>limit_green||blue>limit_blue);
|
|
|
|
strippedLine=strippedLine.substr(semiColon+2);
|
|
semiColon=strippedLine.find(';');
|
|
}
|
|
#pragma endregion
|
|
#pragma region Parse by Spaces
|
|
int red=0,green=0,blue=0;
|
|
std::string pull=strippedLine;
|
|
size_t space=pull.find(' ');
|
|
while(space!=std::string::npos){
|
|
std::string numb=pull.substr(0,space);
|
|
int number=atoi(numb.c_str());
|
|
if(pull[space+1]=='b'){
|
|
blue=number;
|
|
}else
|
|
if(pull[space+1]=='g'){
|
|
green=number;
|
|
}else
|
|
if(pull[space+1]=='r'){
|
|
red=number;
|
|
}
|
|
if(pull.find(' ',space)==std::string::npos)break;
|
|
pull=pull.substr(pull.find(' ',space)+1);
|
|
space=pull.find(' ');
|
|
}
|
|
#pragma endregion
|
|
|
|
std::cout<<"Pulled out "<<red<<" red, "<<blue<<" blue, "<<green<<" green"<<std::endl;
|
|
if(allowed)allowed=!(red>limit_red||green>limit_green||blue>limit_blue);
|
|
std::cout<<"======="<<std::endl;
|
|
if(allowed)idSum+=id;
|
|
id++;
|
|
}
|
|
|
|
std::cout<<"Sum of IDs: "<<idSum<<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::doStuff2,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 |