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

186 lines
4.1 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 = 1;
Run runInput=FILE1;
void doStuff2(){
while(true){ //lines is accessible as a global.
std::map<int,std::string>numberWord{
{0,"zero"},
{1,"one"},
{2,"two"},
{3,"three"},
{4,"four"},
{5,"five"},
{6,"six"},
{7,"seven"},
{8,"eight"},
{9,"nine"},
};
int sum=0;
int lineCount=0;
for(std::string&line:lines){
lineCount++;
int digitFrontIndex=-1;
int digitBackIndex=-1;
std::array<int,10>wordDigitIndexFront;
std::array<int,10>wordDigitIndexBack;
wordDigitIndexFront.fill(-1);
wordDigitIndexBack.fill(-1);
for(int i=0;i<10;i++){
wordDigitIndexFront[i]=line.find(numberWord[i]);
}
for(int i=0;i<10;i++){
wordDigitIndexBack[i]=line.rfind(numberWord[i]);
}
for(int i=0;i<line.size();i++){
if(line[i]>='0'&&line[i]<='9'){
digitFrontIndex=i;
break;
}
}
for(int i=line.size()-1;i>=0;i--){
if(line[i]>='0'&&line[i]<='9'){
digitBackIndex=i;
break;
}
}
int lowestFrontIndex=std::numeric_limits<int>::max();
int highestBackIndex=std::numeric_limits<int>::min();
int frontDigit=0;
int backDigit=0;
for(int i=0;i<10;i++){
if(wordDigitIndexFront[i]==std::string::npos)continue;
if(lowestFrontIndex>wordDigitIndexFront[i]){
lowestFrontIndex=wordDigitIndexFront[i];
frontDigit=i;
}
}
if(digitFrontIndex!=-1&&lowestFrontIndex>digitFrontIndex){
lowestFrontIndex=digitFrontIndex;
frontDigit=line[digitFrontIndex]-'0';
}
for(int i=0;i<10;i++){
if(wordDigitIndexBack[i]==std::string::npos)continue;
if(highestBackIndex<wordDigitIndexBack[i]){
highestBackIndex=wordDigitIndexBack[i];
backDigit=i;
}
}
if(digitBackIndex!=-1&&highestBackIndex<digitBackIndex){
highestBackIndex=digitBackIndex;
backDigit=line[digitBackIndex]-'0';
}
sum+=frontDigit*10+backDigit;
std::cout<<lineCount<<std::endl;
if(frontDigit<=0||backDigit<=0||frontDigit>=10||backDigit>=10)throw;
std::cout<<frontDigit<<backDigit<<"/"<<sum<<std::endl;
}
std::cout<<sum<<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 sum=0;
int digitFront=0;
int digitBack=0;
for(std::string&line:lines){
for(int i=0;i<line.size();i++){
if(line[i]>='0'&&line[i]<='9'){
digitFront=line[i]-'0';
}
}
for(int i=line.size()-1;i>=0;i--){
if(line[i]>='0'&&line[i]<='9'){
digitBack=line[i]-'0';
}
}
sum+=digitBack*10+digitFront;
//std::cout<<digitFront<<digitBack<<"/"<<sum<<std::endl;
}
std::cout<<sum<<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