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.
172 lines
4.6 KiB
172 lines
4.6 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 = 12;
|
|
Run runInput=FILE1;
|
|
|
|
std::vector<int>countGaps(std::string springList){
|
|
std::vector<int>sizes;
|
|
int hashRun=0;
|
|
for(char c:springList){
|
|
if(c=='#')hashRun++;
|
|
else{
|
|
if(hashRun>0)sizes.push_back(hashRun);
|
|
hashRun=0;
|
|
}
|
|
}
|
|
if(hashRun>0)sizes.push_back(hashRun);
|
|
return sizes;
|
|
}
|
|
|
|
size_t CalculateCombinations(std::string springList,std::vector<int>sizes){
|
|
long combinationCount=0;
|
|
size_t questionMarkCount=std::count(springList.begin(),springList.end(),'?');
|
|
size_t possibilities=std::pow(2,questionMarkCount);
|
|
for(size_t i=0;i<possibilities;i++){
|
|
std::string modifiedSpringList=springList;
|
|
size_t springIndex=0;
|
|
size_t questionMarkPos=-1;
|
|
while(springIndex!=questionMarkCount){
|
|
questionMarkPos=springList.find('?',questionMarkPos+1);
|
|
if(i&(1<<springIndex))modifiedSpringList[questionMarkPos]='#';
|
|
else modifiedSpringList[questionMarkPos]='.';
|
|
springIndex++;
|
|
}
|
|
|
|
std::vector<int>modifiedSizeList=countGaps(modifiedSpringList);
|
|
if(sizes.size()==modifiedSizeList.size()&&std::equal(sizes.begin(),sizes.end(),modifiedSizeList.begin())){
|
|
combinationCount++;
|
|
}
|
|
}
|
|
return combinationCount;
|
|
}
|
|
|
|
size_t CalculateCombinationsQuick(std::string springList,std::vector<int>sizes){
|
|
long long total=0;
|
|
size_t combinationCount=CalculateCombinations(springList,sizes);
|
|
size_t combinationCount2=springList[springList.size()-1]=='#'?combinationCount:CalculateCombinations("?"+springList,sizes);
|
|
size_t combinationCount3=CalculateCombinations(springList+"?",sizes);
|
|
|
|
total+=combinationCount;
|
|
for(int i=0;i<4;i++){
|
|
total*=std::max(combinationCount2,combinationCount3);
|
|
}
|
|
return total;
|
|
}
|
|
|
|
void doStuff(){
|
|
long long sum=0;
|
|
std::vector<std::pair<std::string,std::vector<int>>>springLists;
|
|
while(true){ //lines is accessible as a global.
|
|
for(std::string&line:lines){
|
|
std::string springList=line.substr(0,line.find(' '));
|
|
std::vector<int>sizes;
|
|
std::string sizeList=line.substr(line.find(' ')+1);
|
|
size_t comma=sizeList.find(',');
|
|
while(sizeList.length()>0){
|
|
int numb=std::stoi(sizeList.substr(0,comma));
|
|
sizes.push_back(numb);
|
|
if(comma==std::string::npos)break;
|
|
sizeList=sizeList.substr(comma+1);
|
|
comma=sizeList.find(',');
|
|
}
|
|
springLists.push_back({springList,sizes});
|
|
|
|
}
|
|
std::sort(springLists.begin(),springLists.end(),[](std::pair<std::string,std::vector<int>>&spring1,std::pair<std::string,std::vector<int>>&spring2){
|
|
return std::count(spring1.first.begin(),spring1.first.end(),'?')<std::count(spring2.first.begin(),spring2.first.end(),'?');
|
|
});
|
|
for(auto&spring:springLists){
|
|
std::string modifiedSpringList=spring.first;
|
|
std::vector<int>modifiedSizes=spring.second;
|
|
for(int i=0;i<4;i++){
|
|
modifiedSpringList+="?"+spring.first;
|
|
std::copy(spring.second.begin(),spring.second.end(),std::back_inserter(modifiedSizes));
|
|
}
|
|
//long long bruteForceTotal=CalculateCombinations(modifiedSpringList,modifiedSizes);
|
|
long long total=CalculateCombinations(spring.first,spring.second);
|
|
//std::cout<<bruteForceTotal<<" vs "<<total<<": "<<spring.first<<std::endl;
|
|
//if(bruteForceTotal!=total)throw;
|
|
std::cout<<total<<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::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 |