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

134 lines
4.4 KiB

2 years ago
#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
#include "List.h"
2 years ago
bool examineList(List*list1,List*list2){
if (list1->index>=list1->elements.size()){
return true;
}
while (true) {
if (list1->elements.size()>list2->elements.size()){
return false;
}
if (list1->elements[list1->index].list!=nullptr){
if (list2->elements[list2->index].list==nullptr){
//List 1: List
//List 2: Number
List*prevPtr=list1;
while (list1->elements[list1->index].list!=nullptr){
list1=list1->elements[list1->index].list;
}
//Now we should be at a number.
if (list1->elements[list1->index].number>list2->elements[list2->index].number){
return false;
}
list1=prevPtr;
} else {
//List 1: List
//List 2: List
if (!examineList(list1->elements[list1->index].list,list2->elements[list2->index].list)){
return false;
}
}
//It's a list.
} else {
//Compare number.
if (list2->elements[list2->index].list==nullptr){
//List 1: Number
//List 2: Number
if (list1->elements[list1->index].number>list2->elements[list2->index].number){
return false;
}
} else {
//List 1: Number
//List 2: List
List*prevPtr=list2;
while (list2->elements[list2->index].list!=nullptr){
list2=list2->elements[list2->index].list;
}
//Now we should be at a number.
if (list2->elements[list2->index].number<list1->elements[list1->index].number){
return false;
}
list2=prevPtr;
}
}
list1->index++;
list2->index++;
if (list1->index>=list1->elements.size()||list2->index>=list2->elements.size()){
break;
}
}
return true;
}
2 years ago
using namespace olc;
int main()
{
std::ifstream file("testinput");
int index=1;
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;
int sum=0;
bool passed=examineList(list1,list2);
if (passed){
sum+=index;
std::cout<<"Passed! Sum:"<<sum<<std::endl;
} else {
std::cout<<"Failed"<<std::endl;
}
index++;
}
2 years ago
return 0;
}