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 5/main.cpp

178 lines
4.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 = 5;
1 year ago
Run runInput=FILE2;
struct Seed{
long long val;
long long range;
bool found=false;
};
struct Transform{
long long source;
long long range;
long long dest;
};
void doStuff(){
while(true){ //lines is accessible as a global.
long long smallest=0;
bool firstLine=true;
std::vector<Seed>seeds;
std::vector<std::vector<Transform>>transforms;
bool newTransform=false;
for(std::string&line:lines){
if(firstLine){
firstLine=false;
bool IsNumber=true;
std::string seedNumbers=line.substr(6);
while(seedNumbers.find(' ',1)!=std::string::npos){
size_t spacePos=seedNumbers.find(' ',1);
long long numb=atoll(seedNumbers.substr(1,spacePos).c_str());
if(IsNumber){
seeds.push_back({numb});
}else{
Seed&prevSeed=seeds[seeds.size()-1];
prevSeed.range=numb;
if(prevSeed.val+prevSeed.range-1>smallest)smallest=prevSeed.val+prevSeed.range-1;
}
IsNumber=!IsNumber;
seedNumbers=seedNumbers.substr(spacePos);
}
long long numb=atoll(seedNumbers.substr(1).c_str());
if(IsNumber){
seeds.push_back({numb});
}else{
Seed&prevSeed=seeds[seeds.size()-1];
prevSeed.range=numb;
if(prevSeed.val+prevSeed.range-1>smallest)smallest=prevSeed.val+prevSeed.range-1;
}
}else
if(line.length()>0){
if(newTransform&&line[0]>='0'&&line[0]<='9'){//This is a conversion
std::vector<Transform>&currentTransform=transforms[transforms.size()-1];
size_t spacePos=line.find(' ');
long long dest=atoll(line.substr(0,spacePos).c_str());
size_t prevSpacePos=spacePos;
spacePos=line.find(' ',prevSpacePos+1);
long long source=atoll(line.substr(prevSpacePos+1,spacePos).c_str());
prevSpacePos=spacePos;
spacePos=line.find(' ',prevSpacePos+1);
long long distance=atoll(line.substr(prevSpacePos+1,spacePos).c_str());
currentTransform.push_back({source,distance,dest});
}else{
//No-op, skip non-number line.
/*for(Seed&seed:seeds){
std::cout<<seed.val<<' ';
}
std::cout<<std::endl<<std::endl;*/
transforms.push_back({});
newTransform=true;
for(Seed&seed:seeds){
seed.found=false;
}
}
}
}
for(Seed&seed:seeds){
1 year ago
for(long long originalNumb=seed.val;originalNumb<seed.val+seed.range;originalNumb++){
long long seedNumb=originalNumb;
//std::cout<<"Initial Number: "<<seedNumb<<' ';
for(std::vector<Transform>&transformList:transforms){
for(Transform&transform:transformList){
if(seedNumb>=transform.source&&seedNumb<transform.source+transform.range){
seedNumb+=transform.dest-transform.source;
1 year ago
break;
}
}
1 year ago
//std::cout<<seedNumb<<" ";
}
1 year ago
//std::cout<<"Number Out: "<<seedNumb<<std::endl;
if(seedNumb<smallest)smallest=seedNumb;
}
}
std::cout<<smallest<<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