From f76c1b07de4ce3b08d658ffda640374ae0d01134 Mon Sep 17 00:00:00 2001 From: MelKam Date: Fri, 9 Dec 2022 18:12:34 +0000 Subject: [PATCH] 2022_07: rust solution(part 1) --- .vscode/launch.json | 144 +-- 2022/day_7/input.txt | 1017 ++++++++++++++++++ 2022/day_7/rust/Cargo.toml | 8 + 2022/day_7/rust/src/file_system/directory.rs | 64 ++ 2022/day_7/rust/src/file_system/file.rs | 11 + 2022/day_7/rust/src/file_system/mod.rs | 7 + 2022/day_7/rust/src/file_system/system.rs | 78 ++ 2022/day_7/rust/src/main.rs | 96 ++ Cargo.lock | 4 + Cargo.toml | 3 +- 10 files changed, 1297 insertions(+), 135 deletions(-) create mode 100644 2022/day_7/input.txt create mode 100644 2022/day_7/rust/Cargo.toml create mode 100644 2022/day_7/rust/src/file_system/directory.rs create mode 100644 2022/day_7/rust/src/file_system/file.rs create mode 100644 2022/day_7/rust/src/file_system/mod.rs create mode 100644 2022/day_7/rust/src/file_system/system.rs create mode 100644 2022/day_7/rust/src/main.rs diff --git a/.vscode/launch.json b/.vscode/launch.json index c46707f..07f6206 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,30 +9,7 @@ "request": "launch", "name": "Debug executable 'rust_2022_01'", "cargo": { - "args": [ - "build", - "--bin=rust_2022_01", - "--package=rust_2022_01" - ], - "filter": { - "name": "rust_2022_01", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug unit tests in executable 'rust_2022_01'", - "cargo": { - "args": [ - "test", - "--no-run", - "--bin=rust_2022_01", - "--package=rust_2022_01" - ], + "args": ["build", "--bin=rust_2022_01", "--package=rust_2022_01"], "filter": { "name": "rust_2022_01", "kind": "bin" @@ -46,30 +23,7 @@ "request": "launch", "name": "Debug executable 'rust_2022_02'", "cargo": { - "args": [ - "build", - "--bin=rust_2022_02", - "--package=rust_2022_02" - ], - "filter": { - "name": "rust_2022_02", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug unit tests in executable 'rust_2022_02'", - "cargo": { - "args": [ - "test", - "--no-run", - "--bin=rust_2022_02", - "--package=rust_2022_02" - ], + "args": ["build", "--bin=rust_2022_02", "--package=rust_2022_02"], "filter": { "name": "rust_2022_02", "kind": "bin" @@ -83,30 +37,7 @@ "request": "launch", "name": "Debug executable 'rust_2022_03'", "cargo": { - "args": [ - "build", - "--bin=rust_2022_03", - "--package=rust_2022_03" - ], - "filter": { - "name": "rust_2022_03", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug unit tests in executable 'rust_2022_03'", - "cargo": { - "args": [ - "test", - "--no-run", - "--bin=rust_2022_03", - "--package=rust_2022_03" - ], + "args": ["build", "--bin=rust_2022_03", "--package=rust_2022_03"], "filter": { "name": "rust_2022_03", "kind": "bin" @@ -120,30 +51,7 @@ "request": "launch", "name": "Debug executable 'rust_2022_04'", "cargo": { - "args": [ - "build", - "--bin=rust_2022_04", - "--package=rust_2022_04" - ], - "filter": { - "name": "rust_2022_04", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug unit tests in executable 'rust_2022_04'", - "cargo": { - "args": [ - "test", - "--no-run", - "--bin=rust_2022_04", - "--package=rust_2022_04" - ], + "args": ["build", "--bin=rust_2022_04", "--package=rust_2022_04"], "filter": { "name": "rust_2022_04", "kind": "bin" @@ -157,30 +65,7 @@ "request": "launch", "name": "Debug executable 'rust_2022_05'", "cargo": { - "args": [ - "build", - "--bin=rust_2022_05", - "--package=rust_2022_05" - ], - "filter": { - "name": "rust_2022_05", - "kind": "bin" - } - }, - "args": [], - "cwd": "${workspaceFolder}" - }, - { - "type": "lldb", - "request": "launch", - "name": "Debug unit tests in executable 'rust_2022_05'", - "cargo": { - "args": [ - "test", - "--no-run", - "--bin=rust_2022_05", - "--package=rust_2022_05" - ], + "args": ["build", "--bin=rust_2022_05", "--package=rust_2022_05"], "filter": { "name": "rust_2022_05", "kind": "bin" @@ -194,11 +79,7 @@ "request": "launch", "name": "Debug executable 'rust_2022_06'", "cargo": { - "args": [ - "build", - "--bin=rust_2022_06", - "--package=rust_2022_06" - ], + "args": ["build", "--bin=rust_2022_06", "--package=rust_2022_06"], "filter": { "name": "rust_2022_06", "kind": "bin" @@ -210,16 +91,11 @@ { "type": "lldb", "request": "launch", - "name": "Debug unit tests in executable 'rust_2022_06'", + "name": "Debug executable 'rust_2022_07'", "cargo": { - "args": [ - "test", - "--no-run", - "--bin=rust_2022_06", - "--package=rust_2022_06" - ], + "args": ["build", "--bin=rust_2022_07", "--package=rust_2022_07"], "filter": { - "name": "rust_2022_06", + "name": "rust_2022_07", "kind": "bin" } }, @@ -227,4 +103,4 @@ "cwd": "${workspaceFolder}" } ] -} \ No newline at end of file +} diff --git a/2022/day_7/input.txt b/2022/day_7/input.txt new file mode 100644 index 0000000..2ba6559 --- /dev/null +++ b/2022/day_7/input.txt @@ -0,0 +1,1017 @@ +$ cd / +$ ls +dir bntdgzs +179593 cjw.jgc +110209 grbwdwsm.znn +dir hsswswtq +dir jdfwmhg +dir jlcbpsr +70323 qdtbvqjj +48606 qdtbvqjj.zdg +dir tvcr +dir vhjbjr +dir vvsg +270523 wpsjfqtn.ljt +$ cd bntdgzs +$ ls +297955 gcwcp +$ cd .. +$ cd hsswswtq +$ ls +dir bsjbvff +dir dpgvp +267138 grbwdwsm.znn +dir hldgfpvh +dir jdfwmhg +dir jtgdv +93274 ptsd.nzh +268335 qdtbvqjj.dlh +185530 qdtbvqjj.jrw +dir vcbqdj +dir wtrsg +$ cd bsjbvff +$ ls +dir dmnt +148799 grbwdwsm.znn +324931 hzmqrfc.lsd +211089 qdtbvqjj +$ cd dmnt +$ ls +221038 zht +$ cd .. +$ cd .. +$ cd dpgvp +$ ls +dir fzttpjtd +dir jdrbwrc +dir rwz +dir tssm +$ cd fzttpjtd +$ ls +149872 jdfwmhg +$ cd .. +$ cd jdrbwrc +$ ls +149973 hpgg.srm +dir ptsd +$ cd ptsd +$ ls +2594 twzf.pqq +$ cd .. +$ cd .. +$ cd rwz +$ ls +dir jdfwmhg +302808 zzlh +$ cd jdfwmhg +$ ls +229683 cdcrgcmh +218733 nhzt +$ cd .. +$ cd .. +$ cd tssm +$ ls +dir ptsd +37272 qfnnrqsh.qvg +215066 wnvjc.jqf +$ cd ptsd +$ ls +24102 bwtbht.dwq +224035 qdtbvqjj.dmp +$ cd .. +$ cd .. +$ cd .. +$ cd hldgfpvh +$ ls +316712 grbwdwsm.znn +328950 tqvgqjrr +$ cd .. +$ cd jdfwmhg +$ ls +130652 gcwcp +dir jdfwmhg +215427 lfw.zml +dir qdtbvqjj +4181 rgsvgssj.qsr +$ cd jdfwmhg +$ ls +dir bvm +dir hsswswtq +122279 qznt.jhl +dir sjw +dir zpfdtl +$ cd bvm +$ ls +22841 fbcgh.mrp +dir hsswswtq +dir hstg +41317 ndrt +dir nvmvghb +239316 ptsd +dir qtwvdtsp +98555 vzh +$ cd hsswswtq +$ ls +dir ddcjvjgf +127104 plwvb.pbj +dir ptsd +dir qhp +dir rjtrhgwh +$ cd ddcjvjgf +$ ls +135870 bwtbht.dwq +81968 gcwcp +182253 mrbh.wmc +275931 nsrqrts +322128 pfpcp +$ cd .. +$ cd ptsd +$ ls +214981 jsrlsc +dir wpbdrcw +$ cd wpbdrcw +$ ls +197849 mljfb.ggb +173586 ptsd +$ cd .. +$ cd .. +$ cd qhp +$ ls +293198 bnrgl +$ cd .. +$ cd rjtrhgwh +$ ls +224393 clrp.nst +$ cd .. +$ cd .. +$ cd hstg +$ ls +51671 gdsfpc +209216 hsswswtq +97203 jlnr +dir thdhg +57399 tssm +$ cd thdhg +$ ls +201896 jjp.wvw +$ cd .. +$ cd .. +$ cd nvmvghb +$ ls +210047 gfcrzgj +dir rqjbplv +dir rvwd +292931 sgwvcqfr.bpq +dir vtjd +$ cd rqjbplv +$ ls +105204 gcwcp +$ cd .. +$ cd rvwd +$ ls +66170 jdfwmhg +$ cd .. +$ cd vtjd +$ ls +dir ptsd +$ cd ptsd +$ ls +300524 bwtbht.dwq +$ cd .. +$ cd .. +$ cd .. +$ cd qtwvdtsp +$ ls +289574 wctgtq +$ cd .. +$ cd .. +$ cd hsswswtq +$ ls +24935 gcwcp +dir jzpbdcmc +26834 mljfb.ggb +182501 phnmlsjp.pjc +dir pttnl +dir qdtbvqjj +dir vst +$ cd jzpbdcmc +$ ls +297521 grbwdwsm.znn +dir qwc +dir zzswd +$ cd qwc +$ ls +81143 hsswswtq.rjw +54843 mjvvfsz.rgz +273051 pfwgtmtt.ccs +$ cd .. +$ cd zzswd +$ ls +216062 vlbwz.zmh +$ cd .. +$ cd .. +$ cd pttnl +$ ls +257733 mljfb.ggb +250887 pfwgtmtt.ccs +$ cd .. +$ cd qdtbvqjj +$ ls +34667 gcwcp +$ cd .. +$ cd vst +$ ls +70250 pfwgtmtt.ccs +dir zpcqhml +$ cd zpcqhml +$ ls +219936 jdfwmhg.zbm +$ cd .. +$ cd .. +$ cd .. +$ cd sjw +$ ls +152311 nqjtvzff +157117 pfwgtmtt.ccs +118226 ptsd.vsm +$ cd .. +$ cd zpfdtl +$ ls +189042 gcwcp +$ cd .. +$ cd .. +$ cd qdtbvqjj +$ ls +dir ftz +dir hvlffb +dir lzbb +53335 ptsd +dir qdtbvqjj +$ cd ftz +$ ls +dir fft +256058 gcwcp +497 hsswswtq.vqs +103941 hvtcz.fsg +171587 ljlnz.ffg +115101 mljfb.ggb +dir qdtbvqjj +$ cd fft +$ ls +58845 bwtbht.dwq +136040 gcwcp +256973 mljfb.ggb +$ cd .. +$ cd qdtbvqjj +$ ls +dir fgqhdh +304573 ntm.wmc +$ cd fgqhdh +$ ls +317143 gcwcp +26010 lsfpfdqz +$ cd .. +$ cd .. +$ cd .. +$ cd hvlffb +$ ls +6682 vjt.mcf +$ cd .. +$ cd lzbb +$ ls +dir bbvml +324162 bwtbht.dwq +dir fjs +dir pffntc +dir pnltt +dir ptsd +$ cd bbvml +$ ls +dir qdtbvqjj +dir qssdcrp +dir tssm +$ cd qdtbvqjj +$ ls +246275 qdtbvqjj.cgn +$ cd .. +$ cd qssdcrp +$ ls +274399 hsswswtq +$ cd .. +$ cd tssm +$ ls +dir ssqc +$ cd ssqc +$ ls +178904 njrssmlm.gcm +$ cd .. +$ cd .. +$ cd .. +$ cd fjs +$ ls +dir dmvnp +121967 fqlzlvwt +204348 grbwdwsm.znn +102733 jdfwmhg.qsl +240279 ptsd.jwm +228793 ptsd.nsh +dir ssm +$ cd dmvnp +$ ls +dir psj +dir zjw +$ cd psj +$ ls +170665 gcwcp +56058 lsfzc.dcp +40658 tfsllqqw.fgv +$ cd .. +$ cd zjw +$ ls +79989 fggsl.dmz +$ cd .. +$ cd .. +$ cd ssm +$ ls +106263 bwtbht.dwq +106259 jdfwmhg.qtb +6246 rwbnr.tqv +$ cd .. +$ cd .. +$ cd pffntc +$ ls +111475 qbmrdms.ldm +$ cd .. +$ cd pnltt +$ ls +dir nptfhlf +dir zngmf +$ cd nptfhlf +$ ls +223065 qrb.drh +205674 rdgfz +$ cd .. +$ cd zngmf +$ ls +61655 bwtbht.dwq +$ cd .. +$ cd .. +$ cd ptsd +$ ls +dir hrvrt +dir thwtl +$ cd hrvrt +$ ls +152296 pfwgtmtt.ccs +$ cd .. +$ cd thwtl +$ ls +156783 pfwgtmtt.ccs +323304 sltc +$ cd .. +$ cd .. +$ cd .. +$ cd qdtbvqjj +$ ls +320175 pfwgtmtt.ccs +$ cd .. +$ cd .. +$ cd .. +$ cd jtgdv +$ ls +81164 ptsd.tpj +$ cd .. +$ cd vcbqdj +$ ls +dir crng +330203 gvlrg +152022 qdtbvqjj.slq +294095 rthwj.zrf +dir vjsbf +$ cd crng +$ ls +dir gznrh +$ cd gznrh +$ ls +259458 ptsd +$ cd .. +$ cd .. +$ cd vjsbf +$ ls +47331 hlld.fzf +147103 jdfwmhg +$ cd .. +$ cd .. +$ cd wtrsg +$ ls +144344 dtcc +$ cd .. +$ cd .. +$ cd jdfwmhg +$ ls +323973 qdtbvqjj +$ cd .. +$ cd jlcbpsr +$ ls +dir htrdwm +dir jdfwmhg +dir pwmvbhsl +dir vwfdfmcp +$ cd htrdwm +$ ls +dir btn +105731 dlncqrbm.dgl +158267 gqqghldt +242513 hsswswtq.drj +dir jdfwmhg +212816 swsgtv.wbb +228996 tgll.rcs +$ cd btn +$ ls +50419 pfwgtmtt.ccs +$ cd .. +$ cd jdfwmhg +$ ls +dir bwc +$ cd bwc +$ ls +184634 cfwg +$ cd .. +$ cd .. +$ cd .. +$ cd jdfwmhg +$ ls +319749 hsswswtq +dir jdfwmhg +271619 jdfwmhg.znz +dir jhmmt +181217 mljfb.ggb +11297 rcpl.tgf +83423 zwscbcvm.ths +$ cd jdfwmhg +$ ls +267171 cts.hlf +$ cd .. +$ cd jhmmt +$ ls +84473 jdfwmhg +$ cd .. +$ cd .. +$ cd pwmvbhsl +$ ls +dir jsg +171725 mljfb.ggb +152612 qjr +dir vfsqw +$ cd jsg +$ ls +176951 jdfwmhg.fhn +284927 ljvvtw.wcq +153109 vnvtt +$ cd .. +$ cd vfsqw +$ ls +104559 htsrns.gws +$ cd .. +$ cd .. +$ cd vwfdfmcp +$ ls +291404 csmvbjlt.tdf +$ cd .. +$ cd .. +$ cd tvcr +$ ls +dir djtwv +dir hsswswtq +272845 mdds +dir ndshbjzn +65929 scpltww.twm +dir tssm +30516 zdpscm +dir zqdrdzv +$ cd djtwv +$ ls +271696 cwjj.hjp +$ cd .. +$ cd hsswswtq +$ ls +dir djngm +dir hcz +dir ptsd +$ cd djngm +$ ls +317775 ltwjzpjb.rcj +37776 qdtbvqjj.lzf +$ cd .. +$ cd hcz +$ ls +217741 pgdmr +128868 qdtbvqjj +306138 zbmrplsn +$ cd .. +$ cd ptsd +$ ls +304048 ftm +120236 mdcwvvng +$ cd .. +$ cd .. +$ cd ndshbjzn +$ ls +206408 pfwgtmtt.ccs +$ cd .. +$ cd tssm +$ ls +dir mlcnsf +dir nbgjm +204079 pdljvb +185465 rqgdmbjf.rhr +dir sfnlb +$ cd mlcnsf +$ ls +249868 fqrncwd +29146 zdz.jth +$ cd .. +$ cd nbgjm +$ ls +113314 mljfb.ggb +$ cd .. +$ cd sfnlb +$ ls +234917 tjp +$ cd .. +$ cd .. +$ cd zqdrdzv +$ ls +40790 vtdnhzm +$ cd .. +$ cd .. +$ cd vhjbjr +$ ls +dir glv +dir mvns +dir qbrnh +$ cd glv +$ ls +288849 bgvqll.sfj +259105 jdfwmhg +dir qcjlshcv +$ cd qcjlshcv +$ ls +dir nwqqjcmh +$ cd nwqqjcmh +$ ls +137244 grbwdwsm.znn +312904 mzh +dir qdtbvqjj +$ cd qdtbvqjj +$ ls +dir nlqbq +$ cd nlqbq +$ ls +307636 ptsd.vtr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mvns +$ ls +dir gzqlmrdh +dir qjhtlh +dir tssm +dir vthg +$ cd gzqlmrdh +$ ls +274950 mlzdqwm +$ cd .. +$ cd qjhtlh +$ ls +157835 ptsd.lqm +300380 wst.trp +$ cd .. +$ cd tssm +$ ls +15772 gcwcp +$ cd .. +$ cd vthg +$ ls +dir gdndtlnc +$ cd gdndtlnc +$ ls +3175 hsswswtq.bds +320462 mljfb.ggb +305508 mzvtzvqc +dir qdtbvqjj +154575 tssm.vgb +$ cd qdtbvqjj +$ ls +236889 drnnvh +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd qbrnh +$ ls +dir hsswswtq +4623 hsswswtq.rnf +266326 jrmq.ztg +295980 tssm.vzb +dir wnbfzd +dir zjzhncs +dir zttlggt +$ cd hsswswtq +$ ls +48277 gsqjdbhv +$ cd .. +$ cd wnbfzd +$ ls +97133 mljfb.ggb +$ cd .. +$ cd zjzhncs +$ ls +298303 gcwcp +dir ggr +113206 grbwdwsm.znn +$ cd ggr +$ ls +244876 ptsd.zvb +$ cd .. +$ cd .. +$ cd zttlggt +$ ls +dir hdbwrcm +dir mbvpd +dir mtd +dir ptsd +dir tcwqp +$ cd hdbwrcm +$ ls +267323 bwtbht.dwq +$ cd .. +$ cd mbvpd +$ ls +84087 frf.smv +$ cd .. +$ cd mtd +$ ls +158543 mljfb.ggb +$ cd .. +$ cd ptsd +$ ls +112797 vtschwnb.fnp +$ cd .. +$ cd tcwqp +$ ls +90637 lbsqcj.sfn +179097 tssm.dbl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd vvsg +$ ls +168715 bwtbht.dwq +dir bwv +dir hsswswtq +dir lqmnjrlb +dir mmrfrj +175244 vct.tsc +dir zwvlhs +$ cd bwv +$ ls +201509 gcwcp +62815 grbwdwsm.znn +dir gwdh +dir mfdvcn +166355 pfwgtmtt.ccs +dir ptsd +169681 qdtbvqjj.fgh +250573 wvndzgv +$ cd gwdh +$ ls +306377 sphrj.pjh +$ cd .. +$ cd mfdvcn +$ ls +27796 bvclvtrm.jlf +65045 cghr.vzg +dir hsswswtq +197145 jdqztgh.pvd +$ cd hsswswtq +$ ls +298155 bwtbht.dwq +$ cd .. +$ cd .. +$ cd ptsd +$ ls +27501 grbwdwsm.znn +231999 jdnsv +113528 rmfmb.zzw +dir tssm +dir vgjfsh +$ cd tssm +$ ls +dir dndv +226375 grbwdwsm.znn +$ cd dndv +$ ls +152739 sdjrzcv.tvs +$ cd .. +$ cd .. +$ cd vgjfsh +$ ls +211409 swtbttb.vrp +170879 vvfnf.hrp +$ cd .. +$ cd .. +$ cd .. +$ cd hsswswtq +$ ls +dir qdtbvqjj +dir tssm +86418 vhsgq +$ cd qdtbvqjj +$ ls +118588 bwtbht.dwq +$ cd .. +$ cd tssm +$ ls +113460 gml.wdg +$ cd .. +$ cd .. +$ cd lqmnjrlb +$ ls +dir tssm +$ cd tssm +$ ls +dir jdfwmhg +$ cd jdfwmhg +$ ls +64663 nswd.rwc +$ cd .. +$ cd .. +$ cd .. +$ cd mmrfrj +$ ls +319070 gltlwnlt.jzw +232039 hspr +104688 hsswswtq.jsr +dir jdfwmhg +88712 jdfwmhg.zcw +dir pfr +dir prnnpwcd +45488 qdtbvqjj +dir tssm +dir wcmwrtjn +$ cd jdfwmhg +$ ls +140910 bjjhtzct.stm +$ cd .. +$ cd pfr +$ ls +289538 qdtbvqjj +217502 vvpwf +$ cd .. +$ cd prnnpwcd +$ ls +dir qdtbvqjj +$ cd qdtbvqjj +$ ls +dir pqg +dir tssm +$ cd pqg +$ ls +222392 ptsd.ggr +$ cd .. +$ cd tssm +$ ls +158252 dcnvjj.zfd +10486 jdfwmhg.qmb +4374 qdtbvqjj.vqm +254229 vgqfw +$ cd .. +$ cd .. +$ cd .. +$ cd tssm +$ ls +dir ptsd +$ cd ptsd +$ ls +173766 fvlsgqb +35658 wtc.vvd +$ cd .. +$ cd .. +$ cd wcmwrtjn +$ ls +160089 chfhpc +76202 frgpdnd.ngw +138996 jsfsfpqg.nhf +dir mlm +dir nbdbzsn +dir ptsd +278574 vrnb +$ cd mlm +$ ls +dir gqwhhmvd +dir nrzvzgrt +dir nzplht +dir zzp +$ cd gqwhhmvd +$ ls +dir ddmvjpj +dir jdfwmhg +$ cd ddmvjpj +$ ls +273423 jdfwmhg +43605 pfwgtmtt.ccs +$ cd .. +$ cd jdfwmhg +$ ls +239406 qctw.vzb +$ cd .. +$ cd .. +$ cd nrzvzgrt +$ ls +20712 gcwcp +239372 gjgdvbwb.gcz +dir hdzhl +124814 jdfwmhg +dir jfzr +295071 qwjgwqp +221611 shrzpsj.dwh +dir tssm +dir wdlsvzvl +$ cd hdzhl +$ ls +dir gfwbd +184323 hsswswtq.mln +177147 nqgqz.tnf +4680 pfwgtmtt.ccs +$ cd gfwbd +$ ls +254870 cldm.fft +301411 tssm.cvn +$ cd .. +$ cd .. +$ cd jfzr +$ ls +dir dvvflnnw +dir jdfwmhg +216389 lwtwn.ttt +201727 pfwgtmtt.ccs +107829 prphc.ncb +5816 sdvq.jvn +$ cd dvvflnnw +$ ls +24741 brtrbwh.wwd +27700 mljfb.ggb +$ cd .. +$ cd jdfwmhg +$ ls +325218 bwtbht.dwq +63718 mvl.ngz +162645 vtd.vgp +$ cd .. +$ cd .. +$ cd tssm +$ ls +60903 pfwgtmtt.ccs +332768 qdtbvqjj.jwb +$ cd .. +$ cd wdlsvzvl +$ ls +142213 vgvd +$ cd .. +$ cd .. +$ cd nzplht +$ ls +275904 hsswswtq +157369 jdfwmhg +84363 jvcvmbm.fht +dir qbjqgg +$ cd qbjqgg +$ ls +331934 gcwcp +$ cd .. +$ cd .. +$ cd zzp +$ ls +151335 flsd.zmj +dir gwlhqlp +99086 jdfwmhg.hft +$ cd gwlhqlp +$ ls +201894 glcnpqzp.jvc +$ cd .. +$ cd .. +$ cd .. +$ cd nbdbzsn +$ ls +169929 bwtbht.dwq +$ cd .. +$ cd ptsd +$ ls +128999 bwtbht.dwq +dir jtlrn +dir pszlt +dir ptjnh +dir ptsd +2981 qdtbvqjj.qcn +dir rpb +dir tcjgpqj +dir tmddnh +dir tssm +$ cd jtlrn +$ ls +124888 grbwdwsm.znn +30046 jznz.dwf +$ cd .. +$ cd pszlt +$ ls +154368 dbblsg.mzr +$ cd .. +$ cd ptjnh +$ ls +306974 grbwdwsm.znn +82840 ptsd +$ cd .. +$ cd ptsd +$ ls +dir ftjhsb +dir jdfwmhg +304012 lqgtvmrl.qbj +96971 mljfb.ggb +$ cd ftjhsb +$ ls +56965 dhgds +$ cd .. +$ cd jdfwmhg +$ ls +dir lssbmtms +dir vmwshd +$ cd lssbmtms +$ ls +95453 gcwcp +198402 mljfb.ggb +1507 mzlmp +40526 twlqhml +$ cd .. +$ cd vmwshd +$ ls +267087 pfwgtmtt.ccs +$ cd .. +$ cd .. +$ cd .. +$ cd rpb +$ ls +dir lqbchlbp +dir ptsd +$ cd lqbchlbp +$ ls +151429 ptsd.tjz +$ cd .. +$ cd ptsd +$ ls +28900 gcwcp +55920 llt +$ cd .. +$ cd .. +$ cd tcjgpqj +$ ls +dir cvdlcvq +329232 hcmj.nvp +232764 nvtmgc.qgs +108056 ptsd.gcn +39056 qdtbvqjj +91792 tssm.wqz +$ cd cvdlcvq +$ ls +46978 grbwdwsm.znn +17760 qrdbsdpj.dhm +$ cd .. +$ cd .. +$ cd tmddnh +$ ls +238434 gggvq.tfc +$ cd .. +$ cd tssm +$ ls +dir tlllv +$ cd tlllv +$ ls +198184 trmf.qqw +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd zwvlhs +$ ls +19923 gcwcp +129179 grbwdwsm.znn +214660 pghcvh +101270 ptsd.gzl +dir srjlz +$ cd srjlz +$ ls +221301 nrcg.pqw \ No newline at end of file diff --git a/2022/day_7/rust/Cargo.toml b/2022/day_7/rust/Cargo.toml new file mode 100644 index 0000000..d31654b --- /dev/null +++ b/2022/day_7/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust_2022_07" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/day_7/rust/src/file_system/directory.rs b/2022/day_7/rust/src/file_system/directory.rs new file mode 100644 index 0000000..3590862 --- /dev/null +++ b/2022/day_7/rust/src/file_system/directory.rs @@ -0,0 +1,64 @@ +use super::File; + +#[derive(Debug)] +pub struct Directory<'a> { + pub name: &'a str, + pub parent_dir: Option<*mut Directory<'a>>, + pub files: Vec>, + pub dirs: Vec>, +} + +impl<'a> Directory<'a> { + pub fn new(name: &'a str, parent_dir: Option<*mut Directory<'a>>) -> Self { + Self { + name, + parent_dir, + dirs: Vec::new(), + files: Vec::new(), + } + } + + pub fn add_file(&mut self, file: File<'a>) { + self.files.push(file); + } + + pub fn add_dir(&mut self, dir: Directory<'a>) -> *mut Directory<'a> { + let dir_index = self.dirs.len(); + self.dirs.push(dir); + + return self.dirs.get_mut(dir_index).unwrap() as *mut Directory; + } + + pub fn get_dir_size(&self) -> u32 { + let mut size: u32 = 0; + + if self.files.len() != 0 { + size += self.files.iter().map(|f| f.size).sum::() + } + + if self.dirs.len() != 0 { + size += self.dirs.iter().map(|d| d.get_dir_size()).sum::(); + } + + return size; + } + + pub fn get_dir_size_max(&self, max_size: u32) -> u32 { + let mut size = 0; + + let dir_size = self.get_dir_size(); + if dir_size <= max_size { + size += dir_size; + } + + if self.dirs.len() != 0 { + size += self + .dirs + .iter() + .map(|d| d.get_dir_size_max(max_size)) + .sum::(); + } + + return size; + } +} diff --git a/2022/day_7/rust/src/file_system/file.rs b/2022/day_7/rust/src/file_system/file.rs new file mode 100644 index 0000000..a728302 --- /dev/null +++ b/2022/day_7/rust/src/file_system/file.rs @@ -0,0 +1,11 @@ +#[derive(Debug)] +pub struct File<'a> { + pub name: &'a str, + pub size: u32, +} + +impl<'a> File<'a> { + pub fn new(name: &'a str, size: u32) -> Self { + Self { name, size } + } +} diff --git a/2022/day_7/rust/src/file_system/mod.rs b/2022/day_7/rust/src/file_system/mod.rs new file mode 100644 index 0000000..b77f0ed --- /dev/null +++ b/2022/day_7/rust/src/file_system/mod.rs @@ -0,0 +1,7 @@ +mod directory; +mod file; +mod system; + +pub use directory::Directory; +pub use file::File; +pub use system::{ChangeDirectory, Command, FileSystem, ListItems}; diff --git a/2022/day_7/rust/src/file_system/system.rs b/2022/day_7/rust/src/file_system/system.rs new file mode 100644 index 0000000..2ba4b5f --- /dev/null +++ b/2022/day_7/rust/src/file_system/system.rs @@ -0,0 +1,78 @@ +use std::ptr::null_mut; + +use super::{Directory, File}; + +#[derive(Debug)] +pub enum Command<'a> { + CD(ChangeDirectory<'a>), + LS(ListItems<'a>), +} + +#[derive(Debug)] +pub enum ChangeDirectory<'a> { + Root, + Up, + Down(&'a str), +} + +#[derive(Debug)] +pub struct ListItems<'a> { + pub dirs: Vec<&'a str>, + pub files: Vec>, +} + +#[derive(Debug)] +pub struct FileSystem<'a> { + root_dir: Directory<'a>, + pub cwd: *mut Directory<'a>, +} + +impl<'a> FileSystem<'a> { + pub fn new() -> Self { + let mut fs = Self { + root_dir: Directory::new("", None), + cwd: null_mut(), + }; + + fs.cwd = &mut fs.root_dir as *mut Directory<'a>; + + return fs; + } + + pub fn chage_dir(&mut self, command: ChangeDirectory) { + let next_dir = match command { + ChangeDirectory::Down(dir_name) => unsafe { + let dir_index = self + .cwd + .as_mut() + .unwrap() + .dirs + .iter() + .position(|d| d.name == dir_name) + .unwrap(); + + self.cwd.as_mut().unwrap().dirs.get_mut(dir_index).unwrap() as *mut Directory<'a> + }, + ChangeDirectory::Root => &mut self.root_dir as *mut Directory<'a>, + ChangeDirectory::Up => unsafe { self.cwd.as_mut().unwrap().parent_dir.unwrap() }, + }; + + self.cwd = next_dir; + } + + pub fn make_dir(&mut self, name: &'a str) -> *mut Directory<'a> { + unsafe { + if self.cwd.is_null() { + return null_mut(); + } + + let dir = Directory::new(name, Some(self.cwd)); + + return self.cwd.as_mut().unwrap().add_dir(dir); + } + } + + pub fn get_system_size_max(&self, max_size: u32) -> u32 { + self.root_dir.get_dir_size_max(max_size) + } +} diff --git a/2022/day_7/rust/src/main.rs b/2022/day_7/rust/src/main.rs new file mode 100644 index 0000000..422c975 --- /dev/null +++ b/2022/day_7/rust/src/main.rs @@ -0,0 +1,96 @@ +mod file_system; + +use core::panic; + +use file_system::{ChangeDirectory, Command, Directory, File, FileSystem, ListItems}; + +struct FileSystemParser<'a> { + fs: FileSystem<'a>, +} + +impl<'a> FileSystemParser<'a> { + fn new(fs: FileSystem<'a>) -> Self { + Self { fs } + } + + fn parse_commands_string(&self, string: &'a str) -> Vec> { + let commands: Vec = string + .split("$ ") + .skip(1) + .map(|command_str| { + let command_vector = command_str.trim().split("\n").collect::>(); + + if command_vector[0] == "ls" { + let ls_output = command_vector.iter().skip(1); + let mut ls = ListItems { + dirs: Vec::new(), + files: Vec::new(), + }; + + for &line in ls_output { + let mut iter = line.split(" "); + + match iter.next() { + Some("dir") => { + ls.dirs.push(iter.next().unwrap()); + } + Some(num) => { + ls.files + .push(File::new(iter.next().unwrap(), num.parse::().unwrap())); + } + None => panic!(), + } + } + + return Command::LS(ls); + } + + let first_line_parts = command_vector[0].split(" ").collect::>(); + + if first_line_parts[0] == "cd" { + let cd_command = match first_line_parts[1] { + ".." => ChangeDirectory::Up, + "/" => ChangeDirectory::Root, + path => ChangeDirectory::Down(path), + }; + return Command::CD(cd_command); + } + + panic!("Invalid command"); + }) + .collect(); + + return commands; + } + + fn apply_command(&mut self, command: Command<'a>) { + match command { + Command::CD(cd) => { + self.fs.chage_dir(cd); + } + Command::LS(ls) => { + for dir_name in ls.dirs { + self.fs.make_dir(dir_name); + } + + for file in ls.files { + unsafe { + self.fs.cwd.as_mut().unwrap().add_file(file); + } + } + } + } + } +} + +fn main() { + let mut fs_parser = FileSystemParser::new(FileSystem::new()); + + let commands = fs_parser.parse_commands_string(include_str!("../../input.txt")); + + for command in commands { + fs_parser.apply_command(command); + } + + println!("{:#?}", fs_parser.fs.get_system_size_max(100_000)); +} diff --git a/Cargo.lock b/Cargo.lock index 7bea00e..34f4e18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -25,3 +25,7 @@ version = "0.1.0" [[package]] name = "rust_2022_06" version = "0.1.0" + +[[package]] +name = "rust_2022_07" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 6ae8a70..ab7056d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ members = [ "2022/day_3/rust", "2022/day_4/rust", "2022/day_5/rust", - "2022/day_6/rust" + "2022/day_6/rust", + "2022/day_7/rust" ] exclude = [ "2022" ] \ No newline at end of file