Day 3 complete!

master
sigonasr2 12 months ago
parent 9dfd519fca
commit 1781503d7a
  1. 83
      Day 3/main.cpp

@ -24,19 +24,27 @@ void wait(int pauseMs=0){
#pragma endregion #pragma endregion
const int DAY = 3; const int DAY = 3;
Run runInput=FILE1; Run runInput=FILE2;
struct numb{ struct numb{
vi2d pos; vi2d pos;
vi2d endPos;
int number; int number;
bool operator<(numb&rhs){
return number<rhs.number;
}
bool operator==(numb&rhs){
return number==rhs.number;
}
}; };
std::vector<numb>parts; std::vector<numb>parts;
std::vector<std::vector<char>>board; std::vector<std::vector<char>>board;
std::vector<vi2d>gears;
void doStuff2(){ void doStuff2(){
while(true){ //lines is accessible as a global. while(true){ //lines is accessible as a global.
int sum=0; long sum=0;
vi2d pos={}; vi2d pos={};
for(std::string&line:lines){ for(std::string&line:lines){
std::string numb=""; std::string numb="";
@ -44,6 +52,7 @@ void doStuff2(){
std::vector<char>row; std::vector<char>row;
vi2d initialNumberPos={}; vi2d initialNumberPos={};
for(int i=0;i<line.size();i++){ for(int i=0;i<line.size();i++){
if(line[i]=='*')gears.push_back(pos);
if(line[i]>='0'&&line[i]<='9'){ if(line[i]>='0'&&line[i]<='9'){
if(numb.length()==0){ if(numb.length()==0){
initialNumberPos=pos; initialNumberPos=pos;
@ -52,7 +61,7 @@ void doStuff2(){
}else }else
if(numb.length()>0){ if(numb.length()>0){
//A number has been parsed. //A number has been parsed.
parts.push_back({initialNumberPos,atoi(numb.c_str())}); parts.push_back({initialNumberPos,pos+vi2d{-1,0},atoi(numb.c_str())});
numb=""; numb="";
} }
row.push_back(line[i]); row.push_back(line[i]);
@ -60,7 +69,7 @@ void doStuff2(){
} }
if(numb.length()>0){ if(numb.length()>0){
//A number has been parsed. //A number has been parsed.
parts.push_back({initialNumberPos,atoi(numb.c_str())}); parts.push_back({initialNumberPos,pos+vi2d{-1,0},atoi(numb.c_str())});
} }
board.push_back(row); board.push_back(row);
pos.y++; pos.y++;
@ -68,33 +77,41 @@ void doStuff2(){
std::cout<<line<<std::endl; std::cout<<line<<std::endl;
} }
for(vi2d&pos:gears){
std::function<numb*(vi2d)>validatePos=[&](vi2d checkPos){
if(pos.y+checkPos.y>=0&&pos.y+checkPos.y<=board.size()-1&&
pos.x+checkPos.x>=0&&pos.x+checkPos.x<=board[pos.y+checkPos.y].size()-1){
char c=board[pos.y+checkPos.y][pos.x+checkPos.x];
if(c>='0'&&c<='9'){
for(numb&number:parts){ for(numb&number:parts){
int myNumb=number.number; if(number.pos.y==pos.y+checkPos.y&&number.pos.x<=pos.x+checkPos.x&&number.endPos.x>=pos.x+checkPos.x){
vi2d startingPos=number.pos; return &number;
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; }
throw; //Something went terribly wrong.
}
}
numb*test=nullptr;
return test;
}; };
vi2d checkPos={1,0}; numb*val;
if(validatePos({1,0})){if(testChar(board[startingPos.y][startingPos.x+1]))break;} std::vector<numb>numbers;
if(validatePos({0,1})){if(testChar(board[startingPos.y+1][startingPos.x]))break;} if((val=validatePos({1,0}))!=nullptr){numbers.push_back(*val);val=nullptr;}
if(validatePos({-1,0})){if(testChar(board[startingPos.y][startingPos.x-1]))break;} if((val=validatePos({0,1}))!=nullptr){numbers.push_back(*val);val=nullptr;}
if(validatePos({0,-1})){if(testChar(board[startingPos.y-1][startingPos.x]))break;} if((val=validatePos({-1,0}))!=nullptr){numbers.push_back(*val);val=nullptr;}
if(validatePos({1,1})){if(testChar(board[startingPos.y+1][startingPos.x+1]))break;} if((val=validatePos({0,-1}))!=nullptr){numbers.push_back(*val);val=nullptr;}
if(validatePos({1,-1})){if(testChar(board[startingPos.y-1][startingPos.x+1]))break;} if((val=validatePos({1,1}))!=nullptr){numbers.push_back(*val);val=nullptr;}
if(validatePos({-1,1})){if(testChar(board[startingPos.y+1][startingPos.x-1]))break;} if((val=validatePos({1,-1}))!=nullptr){numbers.push_back(*val);val=nullptr;}
if(validatePos({-1,-1})){if(testChar(board[startingPos.y-1][startingPos.x-1]))break;} if((val=validatePos({-1,1}))!=nullptr){numbers.push_back(*val);val=nullptr;}
myNumb/=10; if((val=validatePos({-1,-1}))!=nullptr){numbers.push_back(*val);val=nullptr;}
startingPos.x++; if(numbers.size()>=2){
std::sort(numbers.begin(),numbers.end());
auto last=std::unique(numbers.begin(),numbers.end());
numbers.erase(last,numbers.end());
if(numbers.size()==2){
sum+=numbers[0].number*numbers[1].number;
std::cout<<numbers[0].number<<"*"<<numbers[1].number<<std::endl;
}
} }
} }
std::cout<<sum<<std::endl; std::cout<<sum<<std::endl;
@ -102,7 +119,7 @@ void doStuff2(){
break; break;
} }
} }
/*
void doStuff(){ void doStuff(){
while(true){ //lines is accessible as a global. while(true){ //lines is accessible as a global.
int sum=0; int sum=0;
@ -171,7 +188,7 @@ void doStuff(){
break; break;
} }
} }
*/
void draw(){ //Only use Sprites! If using decals, you must reference global variables! void draw(){ //Only use Sprites! If using decals, you must reference global variables!
Clear(BLACK); Clear(BLACK);
int count=0; int count=0;
@ -189,10 +206,16 @@ void draw(){ //Only use Sprites! If using decals, you must reference global vari
int numb=number.number; int numb=number.number;
while(numb>0){ while(numb>0){
numbStr=std::to_string(numb%10)+numbStr; numbStr=std::to_string(numb%10)+numbStr;
if(numb<10){
DrawString(number.endPos*8,std::string(1,numbStr[numbStr.size()-1]),GREEN);
}
numb/=10; numb/=10;
} }
DrawString(number.pos*8,numbStr,RED); DrawString(number.pos*8,numbStr,RED);
} }
for(vi2d&pos:gears){
DrawString(pos*8,std::string(1,'*'),BLUE);
}
} }
#pragma region Hidden Engine Stuff #pragma region Hidden Engine Stuff

Loading…
Cancel
Save