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

138 lines
4.2 KiB

#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
#include "List.h"
#include <cassert>
enum status{
FALSE,
TRUE,
CONTINUE
};
status examineList(List*list1,List*list2){
std::cout<<"Examining "<<*list1<<" vs "<<*list2<<std::endl;
while (true) {
if (list2->index>=list2->elements.size()&&list1->elements.size()>list1->index){
return FALSE;
}
if (list1->index>=list1->elements.size()){
if (list2->index>=list2->elements.size()){
return CONTINUE;
}
return TRUE;
}
Element e1=list1->elements[list1->index];
Element e2=list2->elements[list2->index];
if (e1.list==nullptr&&e2.list==nullptr){
if (e1.number<e2.number){
return TRUE;
} else if (e1.number>e2.number){
return FALSE;
}
} else
if (e1.list!=nullptr&&e2.list!=nullptr){
status Result = examineList(e1.list,e2.list);
if (Result!=CONTINUE){
return Result;
}
} else {
if (e1.list==nullptr){
e1.list = new List();
Element newE;
newE.number=e1.number;
e1.list->elements.push_back(newE);
status Result = examineList(e1.list,e2.list);
if (Result!=CONTINUE){
return Result;
}
} else {
e2.list = new List();
Element newE;
newE.number=e2.number;
e2.list->elements.push_back(newE);
status Result = examineList(e1.list,e2.list);
if (Result!=CONTINUE){
return Result;
}
}
}
list1->index++;
list2->index++;
}
return TRUE; //Inputs are in the right order.
}
using namespace olc;
int main()
{
int sum=0;
std::ifstream file("input");
int index=1;
std::vector<List*>lists;
while (file.good()){
std::string line;
List*list1;
List*list2;
for (int j=0;j<2;j++){
std::getline(file,line);
List*startingList=new List();
List*currentList=nullptr;
for (int i=0;i<line.length();i++){
if (line[i]=='['){
//std::cout<<"Read "<<line[i]<<"...";
if (currentList==nullptr){
currentList=startingList;
} else {
List*newList=new List();
Element el;
el.list=newList;
newList->parent=currentList;
currentList->elements.push_back(el);
currentList=el.list;
}
} else
if (line[i]==']'){
//std::cout<<"Read "<<line[i]<<"...";
currentList=currentList->parent;
} else
if (line[i]==','){
//std::cout<<"Read "<<line[i]<<"...";
continue;
} else {//Assume it's a digit.
std::string acc="";
while (line[i]>='0'&&line[i]<='9'){
//std::cout<<"Read "<<line[i]<<"...";
acc+=line[i++];
}
Element el;
el.number=std::atoi(acc.c_str());
currentList->elements.push_back(el);
i--;
}
}
if (j==0){
list1=startingList;
} else {
list2=startingList;
}
}
std::getline(file,line);
std::cout<<"List 1: "<<*list1<<std::endl;
std::cout<<"List 2: "<<*list2<<std::endl;
bool passed=true;
if (passed) {
passed=examineList(list1,list2);
}
if (passed){
sum+=index;
std::cout<<"Passed! Sum:"<<sum<<std::endl;
} else {
std::cout<<"Failed"<<std::endl;
}
index++;
}
std::cout<<"Sum: "<<sum<<std::endl;
return 0;
}