From fff4541aca442a3502214c959a77c3cece065671 Mon Sep 17 00:00:00 2001 From: David Vereb Date: Thu, 8 Dec 2022 22:39:30 -0500 Subject: [PATCH] Starting work on #7. --- 2022/7/Makefile | 1 + 2022/7/data.txt | 1013 +++++++++++++++++++++++++++++++++++++++++++++++ 2022/7/main.cpp | 180 +++++++++ 3 files changed, 1194 insertions(+) create mode 120000 2022/7/Makefile create mode 100644 2022/7/data.txt create mode 100644 2022/7/main.cpp diff --git a/2022/7/Makefile b/2022/7/Makefile new file mode 120000 index 0000000..02e7391 --- /dev/null +++ b/2022/7/Makefile @@ -0,0 +1 @@ +../starter/Makefile \ No newline at end of file diff --git a/2022/7/data.txt b/2022/7/data.txt new file mode 100644 index 0000000..c988c21 --- /dev/null +++ b/2022/7/data.txt @@ -0,0 +1,1013 @@ +$ cd / +$ ls +dir jmtrrrp +dir jssnn +dir lbrmb +11968 pcccp +$ cd jmtrrrp +$ ls +77968 chq.jvb +dir fmgsql +$ cd fmgsql +$ ls +dir dbnsfp +dir vvp +$ cd dbnsfp +$ ls +51021 crlq.lrj +186829 dhcrzvbr.wmn +16232 fvhn.fqm +54150 qpbqqj.rpg +$ cd .. +$ cd vvp +$ ls +179105 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd .. +$ cd jssnn +$ ls +dir bphfqs +dir dbnsfp +dir pcccp +dir snr +dir zjbvwsnv +$ cd bphfqs +$ ls +110077 dhcrzvbr.wmn +$ cd .. +$ cd dbnsfp +$ ls +dir hgvh +dir jtqdcmsz +154197 rrcsndz.tzp +$ cd hgvh +$ ls +dir qjnbg +$ cd qjnbg +$ ls +dir bqzfpr +$ cd bqzfpr +$ ls +124394 wjsbsp +$ cd .. +$ cd .. +$ cd .. +$ cd jtqdcmsz +$ ls +275597 dbnsfp.fpg +$ cd .. +$ cd .. +$ cd pcccp +$ ls +dir cnbd +85621 cqzvwl +dir dbnsfp +114355 hbhp.cfv +dir mcgq +dir pcccp +dir qpbqqj +224038 rrcsndz.tzp +dir zcsm +27570 zjbvwsnv.fjt +$ cd cnbd +$ ls +dir jrbz +dir pphv +$ cd jrbz +$ ls +dir dwvlwfq +$ cd dwvlwfq +$ ls +32237 fwclr.rnb +$ cd .. +$ cd .. +$ cd pphv +$ ls +180370 dhcrzvbr.wmn +50154 dzvwdwl.gbt +123965 mlsv.hlw +163116 wnhtwr.mwl +$ cd .. +$ cd .. +$ cd dbnsfp +$ ls +252181 btv.mpv +dir hwncj +dir pcccp +$ cd hwncj +$ ls +51410 jbd.fcm +$ cd .. +$ cd pcccp +$ ls +258123 chq.jvb +$ cd .. +$ cd .. +$ cd mcgq +$ ls +206506 qpbqqj.bbb +$ cd .. +$ cd pcccp +$ ls +193219 ddhtnql.hmb +134114 hjbpzqzb.rwn +108927 lznndn.nqd +73241 ncdrv +$ cd .. +$ cd qpbqqj +$ ls +dir crdt +dir tgchdnc +$ cd crdt +$ ls +205710 chq.jvb +$ cd .. +$ cd tgchdnc +$ ls +dir bdw +dir dpl +dir jssnn +dir pcccp +dir plpzbm +$ cd bdw +$ ls +211300 dbnsfp.tjm +$ cd .. +$ cd dpl +$ ls +287744 rsbjqwm +$ cd .. +$ cd jssnn +$ ls +dir jssnn +$ cd jssnn +$ ls +9644 hmjhshg.vbv +$ cd .. +$ cd .. +$ cd pcccp +$ ls +dir jssnn +85888 pcccp.hdj +dir qpbqqj +dir rmscmwtv +$ cd jssnn +$ ls +129698 crlq.lrj +7499 dhcrzvbr.wmn +283607 qpbqqj.djr +234874 wqnrhll +$ cd .. +$ cd qpbqqj +$ ls +184229 qqpb.ftd +$ cd .. +$ cd rmscmwtv +$ ls +188048 dhcrzvbr.wmn +dir jwtpgbnt +$ cd jwtpgbnt +$ ls +209946 hgg +$ cd .. +$ cd .. +$ cd .. +$ cd plpzbm +$ ls +32627 tlb.qmc +$ cd .. +$ cd .. +$ cd .. +$ cd zcsm +$ ls +dir lczflft +dir zjbvwsnv +dir zmh +$ cd lczflft +$ ls +40043 dzgnvlw.scr +dir lrnb +$ cd lrnb +$ ls +109881 bjpbs +dir jssnn +46901 npmw +$ cd jssnn +$ ls +9216 sgrp +$ cd .. +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +214676 jssnn.hgn +$ cd .. +$ cd zmh +$ ls +dir jdt +dir rggpltr +$ cd jdt +$ ls +147387 jhhsv +90052 jssnn.wns +53105 qpbqqj.dzq +$ cd .. +$ cd rggpltr +$ ls +121454 dbnsfp.dzt +dir gcc +$ cd gcc +$ ls +dir zbqwl +dir zjbvwsnv +$ cd zbqwl +$ ls +260297 pcccp.jrw +$ cd .. +$ cd zjbvwsnv +$ ls +248709 pcccp.tph +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd snr +$ ls +152569 chq.jvb +1437 crlq.lrj +$ cd .. +$ cd zjbvwsnv +$ ls +dir cqhb +53235 ghhtl.bhv +199640 npcfdw +136346 qpbqqj.lmv +dir zjbvwsnv +$ cd cqhb +$ ls +24712 sqqf +$ cd .. +$ cd zjbvwsnv +$ ls +dir gzqg +dir hfbfvn +dir srsphr +dir vgvdcvc +$ cd gzqg +$ ls +dir jjw +$ cd jjw +$ ls +240052 zdcjjz.pmg +$ cd .. +$ cd .. +$ cd hfbfvn +$ ls +278190 bfgndw.pvf +$ cd .. +$ cd srsphr +$ ls +42591 zjbvwsnv.hgh +$ cd .. +$ cd vgvdcvc +$ ls +120322 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd lbrmb +$ ls +dir bjhpdj +42241 crlq.lrj +dir dbnsfp +244610 dhcrzvbr.wmn +dir hppb +dir mcnzs +dir npntsr +13625 tpjpcsgp.dlz +219424 vvpbt.zvf +dir zjbvwsnv +191467 zjbvwsnv.htn +$ cd bjhpdj +$ ls +dir bqjvst +204722 dbnsfp +dir dhltrqqq +226082 dmdqcjp +dir fcqwgzp +dir jssnn +6453 jssnn.ndh +23799 jssnn.zqn +dir nwglfhpl +dir pcccp +dir pdnj +269246 shzqns.nws +dir sjstqlcb +dir zssln +$ cd bqjvst +$ ls +202793 dbnsfp.pjj +259783 jssnn +dir rbvbhnvs +30683 rvddnjmb.tlz +dir tzhslnv +$ cd rbvbhnvs +$ ls +86934 vrtrf.htt +$ cd .. +$ cd tzhslnv +$ ls +76278 mghcwdlr.tsc +$ cd .. +$ cd .. +$ cd dhltrqqq +$ ls +dir mfd +dir pcccp +dir smmb +251164 wsdnsgtt.lhr +191876 zvr.bbz +$ cd mfd +$ ls +51017 crlq.lrj +99213 rjtbnnnq.hgd +$ cd .. +$ cd pcccp +$ ls +160487 dhcrzvbr.wmn +dir nhdrnthj +dir qpbqqj +$ cd nhdrnthj +$ ls +181291 bbn.wtm +186551 fnw.tnn +23622 rrcsndz.tzp +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +227547 dhcrzvbr.wmn +$ cd .. +$ cd .. +$ cd qpbqqj +$ ls +212353 crlq.lrj +170195 dhcrzvbr.wmn +dir ttvp +$ cd ttvp +$ ls +185994 tgjcfgjv +$ cd .. +$ cd .. +$ cd .. +$ cd smmb +$ ls +dir dbnsfp +85354 dbnsfp.zpn +80665 dfmmjbm.rnr +135989 dhcrzvbr.wmn +93718 lrbzr.nfs +dir mjpfnfns +dir nsdpfnhb +dir pmnssvd +32270 qpbqqj.vtd +$ cd dbnsfp +$ ls +31796 gzs.rgv +64506 vbjncw.bpz +181659 vjlfrdp.tqh +$ cd .. +$ cd mjpfnfns +$ ls +231611 chq.jvb +17518 cmnlrzq.hvh +144795 dbnsfp +162194 jssnn.wjz +29305 vdqnlw.fzf +$ cd .. +$ cd nsdpfnhb +$ ls +281844 chq.jvb +$ cd .. +$ cd pmnssvd +$ ls +165816 dfvl.czb +144561 gbn +150785 lnzdwrmb +111214 rrcsndz.tzp +164156 tzgdb.hht +$ cd .. +$ cd .. +$ cd .. +$ cd fcqwgzp +$ ls +199161 dhcrzvbr.wmn +34251 rrcsndz.tzp +198345 vjlfrdp.tqh +167001 zjbvwsnv.bsd +$ cd .. +$ cd jssnn +$ ls +dir ccblfvl +103180 dhcrzvbr.wmn +dir prw +dir tzqfn +dir zjbvwsnv +166467 zjbvwsnv.tdt +$ cd ccblfvl +$ ls +159752 crlq.lrj +20805 jssnn.dvb +243040 lct.zll +27492 qbh +27174 vjlfrdp.tqh +dir zvfwq +$ cd zvfwq +$ ls +135126 chq.jvb +41664 gphw.vzd +dir hmrdghbr +dir jssnn +dir qzzb +dir tmdlcv +$ cd hmrdghbr +$ ls +dir jvgpwrbs +$ cd jvgpwrbs +$ ls +dir wzdv +$ cd wzdv +$ ls +26834 qpbqqj.njf +$ cd .. +$ cd .. +$ cd .. +$ cd jssnn +$ ls +90199 jqqmqddf.qnz +$ cd .. +$ cd qzzb +$ ls +dir mgpql +dir src +dir zvdgc +$ cd mgpql +$ ls +141852 qpbqqj +$ cd .. +$ cd src +$ ls +204425 lqmcbndm.jrj +75571 qsbrsv.jcm +$ cd .. +$ cd zvdgc +$ ls +268742 ffjmrmmz.lhg +18385 rvmp.hjv +$ cd .. +$ cd .. +$ cd tmdlcv +$ ls +182587 sfwvjrj.mzl +$ cd .. +$ cd .. +$ cd .. +$ cd prw +$ ls +207429 dbnsfp.rqf +dir ptgn +dir pzgpqp +252902 rbt +169694 trg.rsh +$ cd ptgn +$ ls +dir jssnn +dir qpbqqj +dir rpd +$ cd jssnn +$ ls +189316 dbnsfp.bqc +$ cd .. +$ cd qpbqqj +$ ls +167937 zjbvwsnv.bhz +$ cd .. +$ cd rpd +$ ls +8775 crlq.lrj +$ cd .. +$ cd .. +$ cd pzgpqp +$ ls +dir pcccp +$ cd pcccp +$ ls +51496 pcccp +$ cd .. +$ cd .. +$ cd .. +$ cd tzqfn +$ ls +dir cbpfvdp +285700 crlq.lrj +7426 dbnsfp.fsd +dir gdl +141367 jssnn.hmw +184482 sczphnp.vnc +126288 vjlfrdp.tqh +dir wndpdj +$ cd cbpfvdp +$ ls +dir cvfr +dir qpbqqj +$ cd cvfr +$ ls +dir jfrnvts +dir qpbqqj +$ cd jfrnvts +$ ls +dir vwdn +$ cd vwdn +$ ls +236936 vjlfrdp.tqh +$ cd .. +$ cd .. +$ cd qpbqqj +$ ls +254275 bqd +$ cd .. +$ cd .. +$ cd qpbqqj +$ ls +dir jssnn +201960 qpbqqj +$ cd jssnn +$ ls +131127 jssnn +$ cd .. +$ cd .. +$ cd .. +$ cd gdl +$ ls +225146 hsgzmtp.wcs +204436 jssnn.lhh +64007 mjzjgfg.jsb +$ cd .. +$ cd wndpdj +$ ls +245412 bvcq +211386 dbnsfp.tqd +186962 fql.mww +dir hlmhtfz +117446 smvjvcn.lcp +$ cd hlmhtfz +$ ls +150152 lrdhbq.rvm +$ cd .. +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +179703 fvmbz +87552 qtz.ccw +129764 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd nwglfhpl +$ ls +66039 crlq.lrj +dir cwq +dir dlgrsw +267814 frhlttn.nmd +dir hmprt +dir qpbqqj +dir wnfzznfh +$ cd cwq +$ ls +77655 cpjnwzh +dir pcccp +dir zjbvwsnv +dir zzhjfmnr +$ cd pcccp +$ ls +dir pcccp +$ cd pcccp +$ ls +245309 bggzbrg.flf +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +196915 gnmfb.dzq +dir ngqbdqp +$ cd ngqbdqp +$ ls +355 rrcsndz.tzp +$ cd .. +$ cd .. +$ cd zzhjfmnr +$ ls +dir dbnsfp +$ cd dbnsfp +$ ls +223184 chq.jvb +$ cd .. +$ cd .. +$ cd .. +$ cd dlgrsw +$ ls +181906 chq.jvb +5636 dbnsfp +219889 jbr.slc +dir zrntbl +$ cd zrntbl +$ ls +61864 brnpgpwt +138980 qpbqqj +$ cd .. +$ cd .. +$ cd hmprt +$ ls +90249 dbnsfp.mbd +$ cd .. +$ cd qpbqqj +$ ls +290377 crlq.lrj +$ cd .. +$ cd wnfzznfh +$ ls +83022 hclmps +64095 zhm +$ cd .. +$ cd .. +$ cd pcccp +$ ls +dir rdzntr +dir rvccq +$ cd rdzntr +$ ls +239028 rrcsndz.tzp +$ cd .. +$ cd rvccq +$ ls +22746 chq.jvb +288752 jjvppq.swt +dir msgwsnjq +dir pggz +228469 vjlfrdp.tqh +$ cd msgwsnjq +$ ls +102522 lwgqc.mhv +25239 ndm.llf +$ cd .. +$ cd pggz +$ ls +dir cnjqsqj +$ cd cnjqsqj +$ ls +229407 shpnq +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pdnj +$ ls +193069 rwnhgttz.pvp +$ cd .. +$ cd sjstqlcb +$ ls +263295 chq.jvb +224091 jss.wtr +$ cd .. +$ cd zssln +$ ls +5859 ncdlcr.dll +$ cd .. +$ cd .. +$ cd dbnsfp +$ ls +271252 dhcrzvbr.wmn +$ cd .. +$ cd hppb +$ ls +259968 jssnn +81292 qpqqb.clj +$ cd .. +$ cd mcnzs +$ ls +170903 crlq.lrj +59482 dhcrzvbr.wmn +dir dqzwzbgm +dir gnrztn +286736 jssnn.jcm +32791 phqsgl +dir pzjnrwt +197323 vjlfrdp.tqh +dir wvnwbpct +$ cd dqzwzbgm +$ ls +78575 qpbqqj +251546 qpbqqj.slb +$ cd .. +$ cd gnrztn +$ ls +158603 hdnwmd.rhj +dir nbfdtwzr +178239 ptnchzpg +40517 rrcsndz.tzp +dir smvb +198007 vjlfrdp.tqh +$ cd nbfdtwzr +$ ls +200354 crlq.lrj +$ cd .. +$ cd smvb +$ ls +163921 zjbvwsnv.brz +$ cd .. +$ cd .. +$ cd pzjnrwt +$ ls +33891 lwrll +259646 pcccp.sfn +106835 pqfzthjq +189673 rrcsndz.tzp +$ cd .. +$ cd wvnwbpct +$ ls +234188 dhcrzvbr.wmn +dir gmtpsgv +86379 jssnn +146663 sfpmdbbd.jvt +25795 vjlfrdp.tqh +$ cd gmtpsgv +$ ls +18642 chq.jvb +3046 cznlwtw +26335 ddgpngrc +116455 vnnls.hsg +$ cd .. +$ cd .. +$ cd .. +$ cd npntsr +$ ls +dir cccjdcvb +206657 chq.jvb +280518 crlq.lrj +dir dbnsfp +dir jphnn +dir jssnn +dir mpl +195193 rrcsndz.tzp +dir rztc +dir znwp +$ cd cccjdcvb +$ ls +192965 mcr.sfq +$ cd .. +$ cd dbnsfp +$ ls +dir gfns +173317 jssnn.tjq +dir mgr +68817 mvwcwfcr.zmz +dir pqfht +108571 swfl.dtj +10398 tvvvv +dir vzg +174361 zjbvwsnv +$ cd gfns +$ ls +203999 zjbvwsnv.hfg +$ cd .. +$ cd mgr +$ ls +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +26871 tqlgcf.jrn +$ cd .. +$ cd .. +$ cd pqfht +$ ls +199590 clpvscl.rlm +dir dwlhv +dir vhzfzhrb +$ cd dwlhv +$ ls +130761 qpbqqj +242752 rrcsndz.tzp +$ cd .. +$ cd vhzfzhrb +$ ls +dir njdgcbvm +$ cd njdgcbvm +$ ls +dir snjfqg +$ cd snjfqg +$ ls +dir qpwh +$ cd qpwh +$ ls +153353 qsjpj +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd vzg +$ ls +dir pcccp +$ cd pcccp +$ ls +dir jfbtl +$ cd jfbtl +$ ls +209199 dbnsfp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jphnn +$ ls +52305 crlq.lrj +193480 gmms.whz +59354 nmq.dww +64638 qpbqqj +47072 rrcsndz.tzp +$ cd .. +$ cd jssnn +$ ls +69168 crlq.lrj +1549 dhcrzvbr.wmn +219596 hdmczg.lmm +108063 jssnn +24327 vjlfrdp.tqh +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +189952 chq.jvb +$ cd .. +$ cd .. +$ cd mpl +$ ls +144856 bqrrzm +249487 crlq.lrj +dir ffqgpgfg +93632 flqwtn.nsz +dir mwpcqr +195910 pdqwn.lcg +$ cd ffqgpgfg +$ ls +66459 dbnsfp +200500 lcmt.zmz +207093 qpbqqj +77042 vjlfrdp.tqh +57109 wwzv.hbn +$ cd .. +$ cd mwpcqr +$ ls +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +166393 vjlfrdp.tqh +$ cd .. +$ cd .. +$ cd .. +$ cd rztc +$ ls +57788 chq.jvb +$ cd .. +$ cd znwp +$ ls +164627 chq.jvb +$ cd .. +$ cd .. +$ cd zjbvwsnv +$ ls +dir dgrrl +71529 jssnn +198617 pcccp.qqh +dir phggn +56842 zjbvwsnv.vqd +$ cd dgrrl +$ ls +dir czm +dir fhhlbdlz +dir gstjw +dir qpbqqj +dir stgb +$ cd czm +$ ls +dir jssnn +$ cd jssnn +$ ls +162335 chq.jvb +30099 mfdgdw +96389 pcdsd.wmw +251423 tmz.lcb +205979 vpltdt.gtv +$ cd .. +$ cd .. +$ cd fhhlbdlz +$ ls +dir qpbqqj +dir vdjs +dir zgz +$ cd qpbqqj +$ ls +285561 chq.jvb +263924 lbqcfdrs +138854 pcccp.dtn +$ cd .. +$ cd vdjs +$ ls +32688 chq.jvb +223233 tbn.blt +$ cd .. +$ cd zgz +$ ls +92804 bqltmv.wzb +$ cd .. +$ cd .. +$ cd gstjw +$ ls +151784 fvfszzzn.cbh +$ cd .. +$ cd qpbqqj +$ ls +dir blztqf +dir plgnh +$ cd blztqf +$ ls +195097 wlvmtz +$ cd .. +$ cd plgnh +$ ls +dir dbnsfp +246221 dhcrzvbr.wmn +271121 jhwmmzls.mhw +170162 pcccp.dpp +37872 qpbqqj +$ cd dbnsfp +$ ls +dir dhpnr +$ cd dhpnr +$ ls +152837 pcccp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd stgb +$ ls +248436 vjlfrdp.tqh +$ cd .. +$ cd .. +$ cd phggn +$ ls +284602 dhcrzvbr.wmn +dir lctr +dir rjmc +66651 rrcsndz.tzp +117525 vth.fgw +156877 wcqnjzbq.dgf +7803 zpsrzclh.bzw +$ cd lctr +$ ls +212339 jssnn.whp +dir jzhcqb +99974 pcccp.zhs +111354 pmc +104899 vjlfrdp.tqh +93496 zhwmbw +$ cd jzhcqb +$ ls +dir zjbvwsnv +$ cd zjbvwsnv +$ ls +146807 rbrg +$ cd .. +$ cd .. +$ cd .. +$ cd rjmc +$ ls +dir fvbmsc +139747 glwmr.lrg +dir gvnnz +102023 tbj.qmz +dir vsztsjfh +$ cd fvbmsc +$ ls +136838 vpvbz.qtw +$ cd .. +$ cd gvnnz +$ ls +95498 zjbvwsnv +$ cd .. +$ cd vsztsjfh +$ ls +215479 ffwlcrwb diff --git a/2022/7/main.cpp b/2022/7/main.cpp new file mode 100644 index 0000000..a23b551 --- /dev/null +++ b/2022/7/main.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#include +#include +#include +#include + +std::vector SplitStr(std::string str, const std::string &delim) +{ + std::vector rtn; + + for(auto split = str.find(delim); split != std::string::npos; split = str.find(delim)) + { + rtn.push_back(str.substr(0, split)); + str = str.substr(split + delim.size()); // chop off beginning + } + if(str.size()) + rtn.push_back(str); + + return rtn; +} + +class File { +public: + File(const std::string &set_filename, unsigned long set_size) + { + this->size = set_size; + this->filename = set_filename; + } + + virtual ~File() + { + } + + const std::string& Filename() const + { + return filename; + } + virtual unsigned long Size() const + { + return size; + } + +protected: + unsigned long size; + std::string filename; +}; + +class Folder : public File { +public: + Folder(const std::string &filename) + : File(filename, 0) + { + } + + unsigned long Size() const override + { + unsigned long size = 0; + for(const auto *file : files) + size += file->Size(); + return size; + } + + void AddFile(const std::string &filename, + unsigned long size) + { + // make sure the file doesn't already exist: + if(std::find_if(files.begin(), files.end(), + [&](const File *file) { + return file->Filename() == filename; + }) != files.end()) + { + std::cerr << "Folder \"" << this->Filename() << "\" already contains " + << "the file \"" << filename << "\"." << std::endl; + exit(-1); + } + + // increment this folder's size total: + this->size += size; + + // add the file: + files.push_back(new File(filename, size)); + } + + void AddFolder(const std::string &name) + { + if(std::find_if(files.begin(), files.end(), + [&](const File *file) { + return file->Filename() == name; + }) != files.end()) + { + std::cout << "NOTE: Folder \"" << this->Filename() << "\" already contains " + << "the subfolder \"" << name << "\"." << std::endl; + // exit(-1); + } + + files.push_back(new Folder(name)); + } + + const std::vector& Files() const + { + return files; + } + +private: + std::vector files; +}; + +int main() +{ + std::ifstream ifs("data.txt"); + if(!ifs.is_open()) + { + std::cerr << "Missing data.txt." << std::endl; + return -1; + } + + Folder root("/"); + std::stack dir_stack; + dir_stack.push(&root); + + for(std::string line; std::getline(ifs, line); ) + { + if(line == "") + continue; + + if(line.starts_with("$ ls")) + continue; + else if(line.starts_with("$ cd")) + { + auto to = line.substr(5); + if(to == "/") + { + while(dir_stack.size() > 1) + dir_stack.pop(); + } + else if(to == "..") + { + if(dir_stack.size() > 1) + dir_stack.pop(); + } + else + { + auto existing = std::find_if(dir_stack.top()->Files().begin(), + dir_stack.top()->Files().end(), + [&to](const File *f){ + return f->Filename() == to; + }); + if(existing != dir_stack.top()->Files().end()) // found! + { + // Check to see if it's a folder, first: + Folder *folder = dynamic_cast(*existing); + if(folder) + dir_stack.push(folder); + else + { + std::cerr << (*existing)->Filename() << " is a file, not a folder." + << std::endl; + exit(-3); + } + } + else + std::cerr << "could not navigate to \"" << to << "\" from \"" + << dir_stack.top()->Filename() << "\"." << std::endl; + } + } + else if(line.starts_with("dir ")) + { + dir_stack.top()->AddFolder(line.substr(4)); + } + else + { + auto values = SplitStr(line, " "); + dir_stack.top()->AddFile(values[1], std::atol(values[0].c_str())); + } + } + + return 0; +}