Logic for testinput is good!

Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
sigonasr2 2 years ago
parent 3f3d30dcc9
commit 0b1f29bfe9
  1. BIN
      C++ProjectTemplate
  2. 1
      List.h
  3. 86
      main.cpp

Binary file not shown.

@ -5,5 +5,6 @@
struct List{ struct List{
std::vector<Element>elements; std::vector<Element>elements;
List*parent; List*parent;
int index=0;
friend std::ostream&operator<<(std::ostream&out,List&rhs); friend std::ostream&operator<<(std::ostream&out,List&rhs);
}; };

@ -3,16 +3,79 @@
#include "olcutils.h" #include "olcutils.h"
#include "List.h" #include "List.h"
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;
}
using namespace olc; using namespace olc;
int main() int main()
{ {
std::ifstream file("input"); std::ifstream file("testinput");
int index=1;
while (file.good()){ while (file.good()){
std::string line; std::string line;
List*list1;
List*list2;
for (int j=0;j<2;j++){
std::getline(file,line); std::getline(file,line);
List*startingList=new List(); List*startingList=new List();
List*currentList=nullptr; List*currentList=nullptr;
if (line.length()>0){
for (int i=0;i<line.length();i++){ for (int i=0;i<line.length();i++){
if (line[i]=='['){ if (line[i]=='['){
//std::cout<<"Read "<<line[i]<<"..."; //std::cout<<"Read "<<line[i]<<"...";
@ -46,9 +109,24 @@ int main()
i--; i--;
} }
} }
std::cout<<line<<std::endl; if (j==0){
std::cout<<*startingList<<std::endl; 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++;
} }
return 0; return 0;

Loading…
Cancel
Save