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

153 lines
6.6 KiB

#define OLC_PGE_APPLICATION
#include "pixelGameEngine.h"
#include "olcutils.h"
using namespace olc;
std::ifstream file("testinput");
std::map<std::string,int> filedata;
std::string pwd;
std::string tempstr;
class File{
public:
std::map<std::string,File> files;
int size;
};
int DisplayContents(File&f,std::string path,int filter,int*filtersum){
std::cout<<" Contents of "<<path<<"(Size "<<f.size<<"): "<<std::endl;
int sum=0;
for (std::map<std::string,File>::iterator it=f.files.begin();it!=f.files.end();it++){
int amt=DisplayContents(it->second,(path=="/")?path+it->first:path+"/"+it->first,filter,filtersum);
sum+=amt;
}
if (sum<=filter){
*filtersum+=sum;
}
std::cout<<" Size of "<<path<<": "<<sum<<std::endl;
return sum+f.size;
}
int main()
{
File rootFile{{},0};
while (file.good()||tempstr.length()>0){
std::string line;
if (tempstr.length()!=0){
line=tempstr;
tempstr="";
} else {
std::getline(file,line);
}
//std::cout<<line<<std::endl;
if (line[0]=='$'){
//Command.
std::string command=line.substr(line.find_first_of(' ')+1,2);
std::string arg=line.substr(line.find_last_of(' ')+1,std::string::npos);
std::cout<<"Command "<<command<<" w/arg "<<arg<<std::endl;
if (command=="cd"){
if (arg==".."){
pwd=pwd.substr(0,pwd.find_last_of('/'));
} else
if (arg=="/"){
pwd="/";
} else {
if (pwd=="/"){
pwd+=arg;
} else {
pwd+="/"+arg;
}
}
std::cout<<"New directory is "<<pwd<<std::endl;
} else {
//It's ls.
while (file.good()) {
std::getline(file,line);
if (line[0]=='$'){
tempstr=line;
break; //It's now a command again.
}
std::string data1=line.substr(0,line.find_first_of(' '));
std::string filename=line.substr(line.find_first_of(' ')+1,std::string::npos);
std::cout<<"ls part 1: "<<data1<<"// Filename:"<<filename<<std::endl;
if (data1=="dir"){
std::string navigator;
if (pwd=="/") {
navigator=pwd+filename;
} else {
navigator=pwd+"/"+filename;
}
std::cout<<" Navigator starts at "<<navigator<<std::endl;
File*filer=&rootFile;
while(navigator!=filename){
std::string dir=navigator.substr(0,navigator.find_first_of('/'));
if (dir.length()==0){
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
continue;
}
if (filer->files.find(dir)==filer->files.end()){
//Does not exist. Create.
filer->files[dir]=File();
std::cout<<"Created "<<dir<<". Size: "<<filer->files.size()<<std::endl;
filer=&filer->files[dir];
}else{
filer=&filer->files[dir];
}
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
std::cout<<" Navigator is now at "<<navigator<<std::endl;
}
//Create this directory too.
if (filer->files.find(navigator)==filer->files.end()){
//Does not exist. Create.
filer->files[navigator]=File();
std::cout<<"Created "<<navigator<<". Size: "<<filer->files.size()<<std::endl;
filer=&filer->files[navigator];
}else{
filer=&filer->files[navigator];
}
} else
if (data1.length()>0&&data1!="dir"){
int size=std::atoi(line.substr(0,line.find_first_of(' ')).c_str());
std::string filename=line.substr(line.find_first_of(' ')+1,std::string::npos);
File*filer=&rootFile;
std::string navigator;
if (pwd=="/") {
navigator=pwd+filename;
} else {
navigator=pwd+"/"+filename;
}
std::cout<<" Navigator starts at "<<navigator<<std::endl;
while(navigator!=filename){
std::string dir=navigator.substr(0,navigator.find_first_of('/'));
if (dir.length()==0){
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
continue;
}
if (filer->files.find(dir)==filer->files.end()){
//Does not exist. Create.
filer->files[dir]=File();
std::cout<<"Created "<<dir<<". Size: "<<filer->files.size()<<std::endl;
filer=&filer->files[dir];
}else{
filer=&filer->files[dir];
}
navigator=navigator.substr(navigator.find_first_of('/')+1,std::string::npos);
std::cout<<" Navigator is now at "<<navigator<<std::endl;
}
//This file gets its size set.
filer->files[navigator]=File();
std::cout<<"Created "<<navigator<<". Size: "<<filer->files.size()<<std::endl;
filer->files[navigator].size=size;
}
}
std::cout<<"end ls"<<std::endl;
}
}
std::cout<<line<<std::endl;
}
int sum=0;
DisplayContents(rootFile,"/",100000,&sum);
std::cout<<"Sum: "<<sum<<std::endl;
return 0;
}