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.
201 lines
4.2 KiB
201 lines
4.2 KiB
12 months ago
|
#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 = 11;
|
||
|
Run runInput=FILE2;
|
||
|
|
||
|
std::vector<std::string>space;
|
||
|
|
||
|
std::vector<vi2d>galaxies;
|
||
|
|
||
|
std::vector<vi2d>walkPath;
|
||
|
|
||
|
void doStuff(){
|
||
|
while(true){ //lines is accessible as a global.
|
||
|
for(std::string&line:lines){
|
||
|
space.push_back(line);
|
||
|
bool allDots=true;
|
||
|
for(int i=0;i<line.length();i++){
|
||
|
if(line[i]!='.'){
|
||
|
allDots=false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if(allDots){
|
||
|
space.push_back(std::string(line.size(),'M'));
|
||
|
}
|
||
|
}
|
||
|
for(int col=0;col<space[0].length();col++){
|
||
|
bool allDots=true;
|
||
|
for(int row=0;row<space.size();row++){
|
||
|
if(space[row][col]!='.'&&space[row][col]!='M'){
|
||
|
allDots=false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if(allDots){
|
||
|
for(int row=0;row<space.size();row++){
|
||
|
std::string&r=space[row];
|
||
|
r.insert(r.begin()+col,'M');
|
||
|
}
|
||
|
col++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for(int y=0;std::string&row:space){
|
||
|
for(int x=0;char c:row){
|
||
|
if(c=='#'){
|
||
|
galaxies.push_back({x,y});
|
||
|
}
|
||
|
x++;
|
||
|
}
|
||
|
y++;
|
||
|
}
|
||
|
|
||
|
long long sum=0;
|
||
|
|
||
|
for(int index=0;vi2d&galaxy:galaxies){
|
||
|
walkPath.clear();
|
||
|
for(int index2=index+1;index2<galaxies.size();index2++){
|
||
|
vi2d&galaxy2=galaxies[index2];
|
||
|
//int distance=abs(galaxy.x-galaxy2.x)+abs(galaxy.y-galaxy2.y);
|
||
|
vi2d startingPos=galaxy;
|
||
|
while(startingPos!=galaxy2){
|
||
|
if(startingPos.x!=galaxy2.x){
|
||
|
if(galaxy2.x>startingPos.x){
|
||
|
startingPos.x++;
|
||
|
}else{
|
||
|
startingPos.x--;
|
||
|
}
|
||
|
if(space[startingPos.y][startingPos.x]=='.'||space[startingPos.y][startingPos.x]=='#'){
|
||
|
sum++;
|
||
|
}else{
|
||
|
sum+=999999;
|
||
|
}
|
||
|
}else{
|
||
|
if(galaxy2.y>startingPos.y){
|
||
|
startingPos.y++;
|
||
|
}else{
|
||
|
startingPos.y--;
|
||
|
}
|
||
|
if(space[startingPos.y][startingPos.x]=='.'||space[startingPos.y][startingPos.x]=='#'){
|
||
|
sum++;
|
||
|
}else{
|
||
|
sum+=999999;
|
||
|
}
|
||
|
}
|
||
|
walkPath.push_back(startingPos);
|
||
|
}
|
||
|
wait(1);
|
||
|
}
|
||
|
index++;
|
||
|
}
|
||
|
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!
|
||
|
if(GetScreenSize()!=vi2d{int(space[0].size()*8),int(space.size()*8)})SetScreenSize(space[0].size()*8,space.size()*8);
|
||
|
Clear(BLACK);
|
||
|
for(int count=0;std::string&row:space){
|
||
|
DrawString({0,count*8},row,WHITE,1);
|
||
|
count++;
|
||
|
}
|
||
|
for(vi2d&galaxy:galaxies){
|
||
|
DrawString(galaxy*8,std::string(1,'#'),CYAN,1);
|
||
|
}
|
||
|
SetPixelMode(Pixel::ALPHA);
|
||
|
srand(48910);
|
||
|
for(vi2d&path:walkPath){
|
||
|
switch(rand()%6){
|
||
|
case 0:{
|
||
|
FillRect(path*8,{8,8},{uint8_t(rand()%255),uint8_t(rand()%255),0,64});
|
||
|
}break;
|
||
|
case 1:{
|
||
|
FillRect(path*8,{8,8},{uint8_t(rand()%255),0,uint8_t(rand()%255),64});
|
||
|
}break;
|
||
|
case 2:{
|
||
|
FillRect(path*8,{8,8},{uint8_t(rand()%255),0,uint8_t(rand()%255),64});
|
||
|
}break;
|
||
|
case 3:{
|
||
|
FillRect(path*8,{8,8},{0,0,uint8_t(rand()%255),64});
|
||
|
}break;
|
||
|
case 4:{
|
||
|
FillRect(path*8,{8,8},{0,uint8_t(rand()%255),0,64});
|
||
|
}break;
|
||
|
case 5:{
|
||
|
FillRect(path*8,{8,8},{uint8_t(rand()%255),0,0,64});
|
||
|
}break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#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
|