From e7892906d678209484d1169f7ee07b0d01452740 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 7 Dec 2022 07:51:21 -0600 Subject: [PATCH] 2022 Day 7 Complete! --- 2022/day07/input | 1091 ++++++++++++++++++++++++++++++++++++++++++ 2022/day07/main.go | 212 ++++++++ 2022/day07/problem | 151 ++++++ 2022/day07/testinput | 23 + 4 files changed, 1477 insertions(+) create mode 100644 2022/day07/input create mode 100644 2022/day07/main.go create mode 100644 2022/day07/problem create mode 100644 2022/day07/testinput diff --git a/2022/day07/input b/2022/day07/input new file mode 100644 index 0000000..3a8fce3 --- /dev/null +++ b/2022/day07/input @@ -0,0 +1,1091 @@ +$ cd / +$ ls +dir fts +dir jnwr +dir lrvl +dir nzgprvw +dir snwqjgj +16394 tllvcdr.sjl +195891 zbdp.gqb +dir zddrb +$ cd fts +$ ls +dir dlqtffw +dir rbfmmjvd +254713 wvwhrb.dhh +$ cd dlqtffw +$ ls +73533 nqbvg.fgd +$ cd .. +$ cd rbfmmjvd +$ ls +290697 zcgrgff.fnf +$ cd .. +$ cd .. +$ cd jnwr +$ ls +323577 ghmtnzr +57588 tdcbdpnr +dir wbv +dir zzbvdcf +$ cd wbv +$ ls +dir nmdwbnnr +253584 slzdbm.ncn +$ cd nmdwbnnr +$ ls +208370 scbcsb.pjg +$ cd .. +$ cd .. +$ cd zzbvdcf +$ ls +8052 ssssrhwz +$ cd .. +$ cd .. +$ cd lrvl +$ ls +dir bqqltcg +189288 cwpwh +90813 jhnddzml.lww +dir pwc +dir rjl +dir rzvqvv +dir slzdbm +dir tcbjmq +140665 zbdp.gqb +dir zhbpqlnd +$ cd bqqltcg +$ ls +dir dlbjblbf +dir fdlw +dir jnwr +dir slzdbm +dir zcgrgff +$ cd dlbjblbf +$ ls +11732 rnsrrf.rtv +$ cd .. +$ cd fdlw +$ ls +dir hlvpfw +228436 mzsfcvgv.pqw +$ cd hlvpfw +$ ls +dir dhwq +$ cd dhwq +$ ls +127459 cgdpqh.tct +58310 jnwr.nqt +$ cd .. +$ cd .. +$ cd .. +$ cd jnwr +$ ls +305998 ssssrhwz +129135 vrt.qdt +86204 wnvm.bld +$ cd .. +$ cd slzdbm +$ ls +40915 zbdp.gqb +120991 zsvffwlt.rbp +$ cd .. +$ cd zcgrgff +$ ls +94614 jnwr.mqm +191626 zbdp.gqb +dir ztrslh +$ cd ztrslh +$ ls +dir bhzn +201167 dvcjtzvs.rvd +dir slzdbm +dir szrth +dir zcp +$ cd bhzn +$ ls +119164 qbgmrqw +102090 zbdp.gqb +$ cd .. +$ cd slzdbm +$ ls +124928 gtdq +$ cd .. +$ cd szrth +$ ls +dir hpbbsq +dir vlwlsdjp +dir zcgrgff +$ cd hpbbsq +$ ls +151717 qsdhff.jsv +$ cd .. +$ cd vlwlsdjp +$ ls +15049 glpdjtq.mwm +302526 jnwr.tds +9550 lhwtbh +22857 ssssrhwz +$ cd .. +$ cd zcgrgff +$ ls +73640 mpq.cdn +dir zcgrgff +$ cd zcgrgff +$ ls +115955 rssmrfbq.trs +$ cd .. +$ cd .. +$ cd .. +$ cd zcp +$ ls +dir qdjtmwrr +dir wpdjttm +$ cd qdjtmwrr +$ ls +138185 jnwr +$ cd .. +$ cd wpdjttm +$ ls +207755 vvwtghjb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pwc +$ ls +15911 fqw +$ cd .. +$ cd rjl +$ ls +119274 ssssrhwz +$ cd .. +$ cd rzvqvv +$ ls +273935 ssssrhwz +$ cd .. +$ cd slzdbm +$ ls +dir hlvpfw +290414 lgzbzjvd.glj +dir ljpmphn +316440 slzdbm.tsj +$ cd hlvpfw +$ ls +dir mhvmszgh +107004 slzdbm +$ cd mhvmszgh +$ ls +dir tftstdp +$ cd tftstdp +$ ls +176794 mpq.cdn +$ cd .. +$ cd .. +$ cd .. +$ cd ljpmphn +$ ls +37528 slfdb.bqt +$ cd .. +$ cd .. +$ cd tcbjmq +$ ls +96506 lrz.nhb +$ cd .. +$ cd zhbpqlnd +$ ls +14027 ccswrthv.pfd +dir clnqtjz +dir fdsqmnn +dir fwdt +dir nljb +dir npmsdrgp +57812 slfdb.bqt +184299 wmlmg +241025 zcgrgff.wbh +dir zggqfj +$ cd clnqtjz +$ ls +62391 cbhw.wgr +309318 jlm.lfq +dir mzsfcvgv +dir rrn +307583 ssssrhwz +$ cd mzsfcvgv +$ ls +1356 hrbh.wpz +dir vnwqstw +$ cd vnwqstw +$ ls +184434 hnhzdshl.lrl +150624 wsrnprdb.pjf +$ cd .. +$ cd .. +$ cd rrn +$ ls +105792 dzprqqc.rbh +107482 ffdjdbc +dir hnr +dir rdmgtf +dir rgrwp +325147 shqr.pdj +43186 slfdb.bqt +236667 zcgrgff +$ cd hnr +$ ls +dir gljrlhjm +250526 mzsfcvgv.nsb +43164 ssssrhwz +$ cd gljrlhjm +$ ls +dir wjwqrnj +$ cd wjwqrnj +$ ls +142366 gshl.qfc +$ cd .. +$ cd .. +$ cd .. +$ cd rdmgtf +$ ls +193562 gdrdnc.vml +123723 hmqfdht +dir lzfntb +dir mjfwsmgd +208819 nqbgcn.qfq +dir tqh +$ cd lzfntb +$ ls +dir gwnpsvw +dir rsgwzhp +103487 scvllbjh.pnw +$ cd gwnpsvw +$ ls +dir lqnz +81937 zbdp.gqb +$ cd lqnz +$ ls +27250 zcgrgff +$ cd .. +$ cd .. +$ cd rsgwzhp +$ ls +dir hhz +$ cd hhz +$ ls +46435 djvfz +$ cd .. +$ cd .. +$ cd .. +$ cd mjfwsmgd +$ ls +25726 clcvm +170085 zbdp.gqb +$ cd .. +$ cd tqh +$ ls +111272 gdq.llg +3215 hdghs +dir lpdcdr +dir vhfr +$ cd lpdcdr +$ ls +203902 hmqfdht +$ cd .. +$ cd vhfr +$ ls +91584 hmqfdht +dir stmvj +$ cd stmvj +$ ls +315660 wwpq.ffq +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rgrwp +$ ls +dir dhwtfld +187439 hmqfdht +dir jnwr +dir npr +dir sdsppq +228581 twd.nnc +42349 wbf.cwb +194162 zbdp.gqb +224441 zcgrgff.qpg +$ cd dhwtfld +$ ls +152381 dqdrd +$ cd .. +$ cd jnwr +$ ls +229758 hmvw.gdz +92710 mzsfcvgv.cdd +79954 stjtn.gft +89831 zbdp.gqb +$ cd .. +$ cd npr +$ ls +52767 hlvpfw.rqb +dir nrbjzvgq +270579 pbsq.msg +240181 slfdb.bqt +dir znbwnh +$ cd nrbjzvgq +$ ls +295237 bwqqvn +229608 dhrjnvtt.lvm +55391 nlvn +dir zcgrgff +$ cd zcgrgff +$ ls +124604 hlvpfw.mlw +$ cd .. +$ cd .. +$ cd znbwnh +$ ls +228293 zcgrgff +$ cd .. +$ cd .. +$ cd sdsppq +$ ls +235741 bcsdzpfj.lvd +dir jnwr +dir njtrhrfm +dir tvq +dir wshgn +$ cd jnwr +$ ls +273541 brcps +dir gjt +dir hlvpfw +dir nbsdvpnj +dir zcgrgff +$ cd gjt +$ ls +184707 zbdp.gqb +$ cd .. +$ cd hlvpfw +$ ls +242810 zcgrgff +$ cd .. +$ cd nbsdvpnj +$ ls +181797 hlvpfw.gsd +294284 hmqfdht +215098 mqbclwwq +$ cd .. +$ cd zcgrgff +$ ls +188814 hmqfdht +$ cd .. +$ cd .. +$ cd njtrhrfm +$ ls +dir dqpjdztt +$ cd dqpjdztt +$ ls +178036 vwg +$ cd .. +$ cd .. +$ cd tvq +$ ls +233085 rmq.zgq +215613 slzdbm.wvf +$ cd .. +$ cd wshgn +$ ls +209007 jnwr.hsr +dir ntrlll +$ cd ntrlll +$ ls +245558 jnwr.tbr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd fdsqmnn +$ ls +dir dfl +dir hqffnmq +268076 zcgrgff.cfm +dir ztc +$ cd dfl +$ ls +5553 bbvqpgf +128390 lqtvvc.tdj +190189 vnmfcdws +$ cd .. +$ cd hqffnmq +$ ls +28438 ssssrhwz +$ cd .. +$ cd ztc +$ ls +247470 slfdb.bqt +$ cd .. +$ cd .. +$ cd fwdt +$ ls +60342 bdwbzpzz +dir cdcc +dir jldlzttm +261042 ssssrhwz +dir sws +293090 zbdp.gqb +93624 zcgrgff.vgh +$ cd cdcc +$ ls +dir cwpfczr +dir lsrgfml +$ cd cwpfczr +$ ls +303853 wzz.chs +$ cd .. +$ cd lsrgfml +$ ls +63205 hmqfdht +$ cd .. +$ cd .. +$ cd jldlzttm +$ ls +184715 brclwptp +309967 gdbfh.vhn +5460 hlvpfw.fwb +46159 jnwr.ggw +173896 zbdp.gqb +$ cd .. +$ cd sws +$ ls +dir hlvpfw +75133 nqrp.jdg +$ cd hlvpfw +$ ls +71403 cvdd.whl +$ cd .. +$ cd .. +$ cd .. +$ cd nljb +$ ls +105380 lrhmdl +$ cd .. +$ cd npmsdrgp +$ ls +176062 bhm.gfd +dir dgdl +24535 fdfmntpt.qvp +dir jnwr +279503 ssssrhwz +4671 zbdp.gqb +$ cd dgdl +$ ls +4624 qqcp.rcg +$ cd .. +$ cd jnwr +$ ls +dir cvw +122648 hmqfdht +26565 jnwr.fst +271544 jnwr.tqb +170968 mpq.cdn +dir ncqflwrb +103826 qgzlff.frl +dir wclwg +dir wtcfswt +$ cd cvw +$ ls +58583 pjthqdbr.zzh +398 ssssrhwz +$ cd .. +$ cd ncqflwrb +$ ls +105747 wwstfng.cdl +$ cd .. +$ cd wclwg +$ ls +75020 wbq +$ cd .. +$ cd wtcfswt +$ ls +182813 dnln +255923 hmqfdht +$ cd .. +$ cd .. +$ cd .. +$ cd zggqfj +$ ls +dir gpnfpv +dir gzfsnbv +186684 jnwr +dir msrz +$ cd gpnfpv +$ ls +dir hlvpfw +$ cd hlvpfw +$ ls +186814 bgqpn +41444 mzsfcvgv.qpp +292879 zcgrgff +$ cd .. +$ cd .. +$ cd gzfsnbv +$ ls +321796 jnwr.ghb +dir sdgwjtf +$ cd sdgwjtf +$ ls +127216 mzsfcvgv.qlv +$ cd .. +$ cd .. +$ cd msrz +$ ls +dir fzl +32371 jqtgpzw.thg +271389 slfdb.bqt +270845 tchtnp.rsw +146371 zmwbmj +$ cd fzl +$ ls +140610 ssssrhwz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd nzgprvw +$ ls +dir hgfdbf +218138 mpq.cdn +73419 slfdb.bqt +dir vfztvnjm +269866 wldptfv.bbb +dir zgdsgh +$ cd hgfdbf +$ ls +163792 ssssrhwz +262305 zcgrgff +$ cd .. +$ cd vfztvnjm +$ ls +139863 hlvpfw +138410 hmqfdht +$ cd .. +$ cd zgdsgh +$ ls +194820 dhp.tgt +dir hlvpfw +dir sdllphqd +dir zcgrgff +$ cd hlvpfw +$ ls +44509 hmqfdht +$ cd .. +$ cd sdllphqd +$ ls +47133 mpq.cdn +$ cd .. +$ cd zcgrgff +$ ls +186613 jrrpljrc +$ cd .. +$ cd .. +$ cd .. +$ cd snwqjgj +$ ls +dir slzdbm +dir zcgrgff +dir zldscsc +$ cd slzdbm +$ ls +21422 dplgjzvs.nrn +175310 zcgrgff.dfq +$ cd .. +$ cd zcgrgff +$ ls +dir rrfhvjf +275455 vwnvj.nhb +dir zcgrgff +$ cd rrfhvjf +$ ls +69149 mqmwv.jmr +$ cd .. +$ cd zcgrgff +$ ls +dir zcgrgff +$ cd zcgrgff +$ ls +183398 hmqfdht +93968 sdhmwd +$ cd .. +$ cd .. +$ cd .. +$ cd zldscsc +$ ls +dir fmzvccvg +299187 mpq.cdn +324288 mzsfcvgv.srz +dir prdfldrf +dir sgpdqw +289150 ssssrhwz +3330 vwwj +133537 wbb.fdl +dir wjdpws +$ cd fmzvccvg +$ ls +dir brrlg +dir crm +dir mzsfcvgv +$ cd brrlg +$ ls +17776 jnwr.qqz +$ cd .. +$ cd crm +$ ls +220545 mltrj +177044 mzsfcvgv.thj +$ cd .. +$ cd mzsfcvgv +$ ls +51223 pdnbml.qpg +$ cd .. +$ cd .. +$ cd prdfldrf +$ ls +213872 ftw +$ cd .. +$ cd sgpdqw +$ ls +205684 whrfvw.dph +$ cd .. +$ cd wjdpws +$ ls +dir hlvpfw +$ cd hlvpfw +$ ls +324522 zbdp.gqb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd zddrb +$ ls +238213 dhrdjfzl.npj +28938 hlvpfw +313900 hlvpfw.bwd +dir pgbgpn +132967 rrhsr +dir slzdbm +142210 wlvpwjjz +dir zdhcp +$ cd pgbgpn +$ ls +dir jpmvrvt +154268 mpq.cdn +dir mzsfcvgv +dir vvmwgngv +$ cd jpmvrvt +$ ls +150157 ghcvqm +$ cd .. +$ cd mzsfcvgv +$ ls +93810 mjglpwbc.tvt +dir slzdbm +$ cd slzdbm +$ ls +dir jnwr +174522 slzdbm.ncl +$ cd jnwr +$ ls +183686 rpwjvc +$ cd .. +$ cd .. +$ cd .. +$ cd vvmwgngv +$ ls +dir bqc +dir mzsfcvgv +dir qgj +dir wfjzls +$ cd bqc +$ ls +191442 bqsn +dir lmnvtg +278861 mpq.cdn +191779 mthgd.mjp +dir mzsfcvgv +dir twzq +$ cd lmnvtg +$ ls +dir dzdd +7920 hmqfdht +dir rnlctfm +dir slzdbm +213184 zcgrgff.vln +$ cd dzdd +$ ls +dir jnwr +$ cd jnwr +$ ls +71772 hlvpfw +$ cd .. +$ cd .. +$ cd rnlctfm +$ ls +282289 jvnmblr.mdc +24454 ssssrhwz +$ cd .. +$ cd slzdbm +$ ls +316131 mzsfcvgv.czz +$ cd .. +$ cd .. +$ cd mzsfcvgv +$ ls +dir nsvn +dir plj +dir tvmv +$ cd nsvn +$ ls +dir bcmd +dir jcjsp +dir jnwr +$ cd bcmd +$ ls +206778 cpljbvm.vws +86096 lfgjnzhw.rtm +$ cd .. +$ cd jcjsp +$ ls +188389 zbdp.gqb +$ cd .. +$ cd jnwr +$ ls +293633 jhh.wtj +$ cd .. +$ cd .. +$ cd plj +$ ls +323119 qgs +221100 ssssrhwz +264549 zbdp.gqb +$ cd .. +$ cd tvmv +$ ls +198197 mpq.cdn +$ cd .. +$ cd .. +$ cd twzq +$ ls +dir hwgndwpj +$ cd hwgndwpj +$ ls +dir srdzqqf +$ cd srdzqqf +$ ls +101738 slzdbm +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mzsfcvgv +$ ls +dir hlvpfw +dir vbdw +$ cd hlvpfw +$ ls +270309 wncrt +$ cd .. +$ cd vbdw +$ ls +dir csw +$ cd csw +$ ls +231750 dcjr.jgb +297504 mpq.cdn +215564 plm.rtl +$ cd .. +$ cd .. +$ cd .. +$ cd qgj +$ ls +192181 hmqfdht +dir lmp +250495 mpq.cdn +dir slzdbm +253262 ssssrhwz +dir tzth +$ cd lmp +$ ls +dir jnwr +$ cd jnwr +$ ls +284951 zcgrgff.slq +$ cd .. +$ cd .. +$ cd slzdbm +$ ls +266715 hzbq.plc +dir jnwr +11379 mzsfcvgv.rbw +dir slzdbm +$ cd jnwr +$ ls +108636 mpq.cdn +324035 zzvmlrj.rrl +$ cd .. +$ cd slzdbm +$ ls +68393 zbdp.gqb +$ cd .. +$ cd .. +$ cd tzth +$ ls +12884 hlvpfw.gwl +146470 jnwr +$ cd .. +$ cd .. +$ cd wfjzls +$ ls +191088 hlvpfw.prw +64712 mpq.cdn +103828 pjhg.qmc +dir qvmdt +$ cd qvmdt +$ ls +244001 ssssrhwz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd slzdbm +$ ls +dir hsvbbbp +dir jdtrdgqs +dir lrldsqcr +194137 mzsfcvgv.hnv +dir nzcbqzdz +$ cd hsvbbbp +$ ls +104752 rcfpnh.vlr +185473 slzdbm.dbr +dir vpwf +dir zhngz +$ cd vpwf +$ ls +266988 frhvp.ltf +82931 mpq.cdn +$ cd .. +$ cd zhngz +$ ls +181282 drtfhrhp +$ cd .. +$ cd .. +$ cd jdtrdgqs +$ ls +37106 hdjl.rrr +$ cd .. +$ cd lrldsqcr +$ ls +dir brwcprn +dir hlvpfw +dir jnwr +dir slzdbm +dir wrmr +dir zjrdgf +dir zrpqf +$ cd brwcprn +$ ls +dir bmjwts +$ cd bmjwts +$ ls +157148 jwbd +251396 qtgc +$ cd .. +$ cd .. +$ cd hlvpfw +$ ls +121723 zbdp.gqb +$ cd .. +$ cd jnwr +$ ls +240747 gfrrtdh +$ cd .. +$ cd slzdbm +$ ls +176832 jnwr +36125 ltwmzw.fdl +132059 plqmwhr.hhv +85540 slzdbm.wzm +23459 zhbwh.cds +$ cd .. +$ cd wrmr +$ ls +90357 zbdp.gqb +$ cd .. +$ cd zjrdgf +$ ls +301679 qbmt.vhh +12942 slfdb.bqt +$ cd .. +$ cd zrpqf +$ ls +241851 pnsdlbs.brw +$ cd .. +$ cd .. +$ cd nzcbqzdz +$ ls +182963 qgg.zsc +dir qwjgb +301344 slfdb.bqt +299029 zbdp.gqb +$ cd qwjgb +$ ls +223978 rprrjp.hfd +164106 wlq.rcq +$ cd .. +$ cd .. +$ cd .. +$ cd zdhcp +$ ls +dir hbggp +dir mzsfcvgv +dir zdsvth +$ cd hbggp +$ ls +302339 qcmfdhvm +112806 rpb.vfl +$ cd .. +$ cd mzsfcvgv +$ ls +dir gpz +dir slzdbm +dir zvjv +$ cd gpz +$ ls +290264 fdl +158548 slfdb.bqt +$ cd .. +$ cd slzdbm +$ ls +dir hlvpfw +dir mzsfcvgv +dir zdcthssc +$ cd hlvpfw +$ ls +49287 zbdp.gqb +$ cd .. +$ cd mzsfcvgv +$ ls +dir gnmtggs +103244 jnwr.gtn +263736 slfdb.bqt +$ cd gnmtggs +$ ls +31665 qhthp.lfh +$ cd .. +$ cd .. +$ cd zdcthssc +$ ls +64408 cgbnzc.cgn +$ cd .. +$ cd .. +$ cd zvjv +$ ls +6088 gnpsb.dml +145405 llp.cbm +dir mgcg +dir nwl +dir rqznjmt +128194 zbdp.gqb +$ cd mgcg +$ ls +dir ngwvm +dir slzdbm +dir smd +dir smr +dir zcgrgff +$ cd ngwvm +$ ls +49186 hmqfdht +$ cd .. +$ cd slzdbm +$ ls +44434 rdcb +$ cd .. +$ cd smd +$ ls +dir fjd +dir rscdvnwt +dir wbhp +318166 zbdp.gqb +$ cd fjd +$ ls +14084 hlvpfw.ghn +$ cd .. +$ cd rscdvnwt +$ ls +6432 fzwpmjm.ntl +$ cd .. +$ cd wbhp +$ ls +dir czv +217474 dnq.cpq +36009 jnwr.zdv +32123 mpq.cdn +27607 mrm.sgs +107593 zbdp.gqb +$ cd czv +$ ls +60750 zcgrgff.mnr +$ cd .. +$ cd .. +$ cd .. +$ cd smr +$ ls +47204 zcgrgff.vsl +$ cd .. +$ cd zcgrgff +$ ls +317050 hjtfb +110272 jrbcbvjw +290867 ltfj.dvl +dir mzsfcvgv +130694 wnc.rnr +206448 zbdp.gqb +$ cd mzsfcvgv +$ ls +dir jnwr +$ cd jnwr +$ ls +20405 bjpvssjt +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd nwl +$ ls +254439 dlv +193910 hlvpfw +13603 hnsdntn.trz +2990 mpq.cdn +$ cd .. +$ cd rqznjmt +$ ls +250114 hlvpfw +189732 mzsfcvgv.vqv +144688 zcgrgff +$ cd .. +$ cd .. +$ cd .. +$ cd zdsvth +$ ls +150609 jnwr.rtr +173402 nftm.nwd +dir njhjrgmf +$ cd njhjrgmf +$ ls +295762 nchztlh.lcs diff --git a/2022/day07/main.go b/2022/day07/main.go new file mode 100644 index 0000000..7262d50 --- /dev/null +++ b/2022/day07/main.go @@ -0,0 +1,212 @@ +package main + +import ( + "errors" + "fmt" + "log" + "sort" + "strings" + + h "git.bullercodeworks.com/brian/adventofcode/helpers" +) + +func main() { + inp := h.StdinToStringSlice() + part1(inp) + fmt.Println("") + part2(inp) +} + +func buildFileSystem(inp []string) *dir { + root := NewDir(nil, []string{}, "/") + cwd := root + for i := range inp { + if i == 0 { + // We've already done this + continue + } + pts := strings.Fields(inp[i]) + switch pts[0] { + case "$": + if pts[1] == "cd" { + if pts[2] == ".." { + cwd = cwd.Parent() + } else { + ch, err := cwd.ChangeDir(pts[2]) + if err != nil { + log.Fatalf("Failed to change directories: %s/%s, %s", strings.Join(cwd.Path(), "/"), pts[2], err.Error()) + } + cwd = ch + } + } + case "dir": + cwd.AddDir(pts[1]) + default: + cwd.AddFile(pts[1], h.Atoi(pts[0])) + } + } + return root +} + +func part1(inp []string) { + fmt.Println("# Part 1") + root := buildFileSystem(inp) + smallDirs := root.FindDirectoriesMaxSize(100000) + var sum int + for i := range smallDirs { + sum += smallDirs[i].Size() + } + fmt.Printf("Naive Size of Small Dirs: %d\n", sum) +} + +func part2(inp []string) { + fmt.Println("# Part 2") + root := buildFileSystem(inp) + total := 70000000 + required := 30000000 + used := root.Size() + needed := required - (total - used) + dir := root.FindSmallestDirectoryAtLeast(needed) + if dir == nil { + log.Fatal("No suitable directories found") + } + fmt.Printf("Disk Space %d / %d. Need %d more for update.\n", (total - used), total, needed) + fmt.Println("Auto-deleting smallest directory that will permit update... Don't panic.") + fmt.Printf("Freed space by deleting %s: %d\n", dir.FullPath(), dir.Size()) +} + +const ( + TpFile = iota + TpDir +) + +type node interface { + Parent() *dir + Path() []string + Name() string + Type() int + Size() int + FullPath() []string + Print(depth int) +} + +type file struct { + parent *dir + path []string + name string + size int +} + +func NewFile(parent *dir, path []string, nm string, size int) *file { + return &file{ + parent: parent, + path: path, + name: nm, + size: size, + } +} +func (f file) Parent() *dir { return f.parent } +func (f file) Path() []string { return f.path } +func (f file) Name() string { return f.name } +func (f file) Type() int { return TpFile } +func (f file) Size() int { return f.size } +func (f file) FullPath() []string { return append(f.path, f.name) } +func (f file) Print(depth int) { + fmt.Printf("%s- %s (file, size=%d)\n", strings.Repeat(" ", depth), f.Name(), f.Size()) +} + +type dir struct { + parent *dir + path []string + name string + contents map[string]node + contentsList []string +} + +func NewDir(parent *dir, path []string, name string) *dir { + return &dir{ + parent: parent, + path: path, + name: name, + contents: make(map[string]node), + } +} +func (d dir) Parent() *dir { return d.parent } +func (d dir) Path() []string { return d.path } +func (d dir) Name() string { return d.name } +func (d dir) Type() int { return TpDir } +func (d dir) Size() int { + var size int + for i := range d.contents { + size += d.contents[i].Size() + } + return size +} +func (d dir) FullPath() []string { return append(d.path, d.name) } +func (d *dir) ChangeDir(to string) (*dir, error) { + if v, ok := d.contents[to]; !ok { + return nil, errors.New("Directory not found") + } else { + switch d := v.(type) { + case *dir: + return d, nil + default: + return nil, errors.New("Not a directory") + } + } +} +func (d *dir) AddFile(nm string, size int) { + d.contents[nm] = NewFile(d, d.FullPath(), nm, size) + d.contentsList = append(d.contentsList, nm) + sort.Strings(d.contentsList) +} +func (d *dir) AddDir(nm string) { + d.contents[nm] = NewDir(d, d.FullPath(), nm) + d.contentsList = append(d.contentsList, nm) + sort.Strings(d.contentsList) +} +func (d dir) Print(depth int) { + fmt.Printf("%s- %s (dir)\n", strings.Repeat(" ", depth), d.Name()) + for _, nm := range d.contentsList { + n, ok := d.contents[nm] + if !ok { + log.Fatalf("Error printing directory %s: %s not found.", d.FullPath(), nm) + } + n.Print(depth + 1) + } +} +func (d *dir) FreeSpace(driveSize int) int { return driveSize - d.Size() } + +func (d *dir) FindDirectoriesMaxSize(size int) []*dir { + var ret []*dir + for _, node := range d.contents { + switch child := node.(type) { + case *dir: + if child.Size() <= size { + ret = append(ret, child) + } + ret = append(ret, child.FindDirectoriesMaxSize(size)...) + } + } + return ret +} +func (d *dir) FindSmallestDirectoryAtLeast(needed int) *dir { + var ret *dir + retSize := d.Size() + for _, node := range d.contents { + switch child := node.(type) { + case *dir: + sz := child.Size() + if sz >= needed && sz < retSize { + ret = child + retSize = sz + rec := child.FindSmallestDirectoryAtLeast(needed) + if rec != nil { + ret = rec + retSize = rec.Size() + } + } + } + } + return ret +} diff --git a/2022/day07/problem b/2022/day07/problem new file mode 100644 index 0000000..cb4a255 --- /dev/null +++ b/2022/day07/problem @@ -0,0 +1,151 @@ +Advent of Code +br0xen (AoC++) 14* + +--- Day 7: No Space Left On Device --- + + You can hear birds chirping and raindrops hitting leaves as the expedition proceeds. Occasionally, you can even hear + much louder sounds in the distance; how big do the animals get out here, anyway? + + The device the Elves gave you has problems with more than just its communication system. You try to run a system update: + + $ system-update --please --pretty-please-with-sugar-on-top + Error: No space left on device + + Perhaps you can delete some files to make space for the update? + + You browse around the filesystem to assess the situation and save the resulting terminal output (your puzzle input). For + example: + + $ cd / + $ ls + dir a + 14848514 b.txt + 8504156 c.dat + dir d + $ cd a + $ ls + dir e + 29116 f + 2557 g + 62596 h.lst + $ cd e + $ ls + 584 i + $ cd .. + $ cd .. + $ cd d + $ ls + 4060174 j + 8033020 d.log + 5626152 d.ext + 7214296 k + + The filesystem consists of a tree of files (plain data) and directories (which can contain other directories or files). + The outermost directory is called /. You can navigate around the filesystem, moving into or out of directories and + listing the contents of the directory you're currently in. + + Within the terminal output, lines that begin with $ are commands you executed, very much like some modern computers: + + • cd means change directory. This changes which directory is the current directory, but the specific result depends on + the argument: + + • cd x moves in one level: it looks in the current directory for the directory named x and makes it the current + directory. + • cd .. moves out one level: it finds the directory that contains the current directory, then makes that + directory the current directory. + • cd / switches the current directory to the outermost directory, /. + + • ls means list. It prints out all of the files and directories immediately contained by the current directory: + + • 123 abc means that the current directory contains a file named abc with size 123. + • dir xyz means that the current directory contains a directory named xyz. + + Given the commands and output in the example above, you can determine that the filesystem looks visually like this: + + - / (dir) + - a (dir) + - e (dir) + - i (file, size=584) + - f (file, size=29116) + - g (file, size=2557) + - h.lst (file, size=62596) + - b.txt (file, size=14848514) + - c.dat (file, size=8504156) + - d (dir) + - j (file, size=4060174) + - d.log (file, size=8033020) + - d.ext (file, size=5626152) + - k (file, size=7214296) + + Here, there are four directories: / (the outermost directory), a and d (which are in /), and e (which is in a). These + directories also contain files of various sizes. + + Since the disk is full, your first step should probably be to find directories that are good candidates for deletion. To + do this, you need to determine the total size of each directory. The total size of a directory is the sum of the sizes + of the files it contains, directly or indirectly. (Directories themselves do not count as having any intrinsic size.) + + The total sizes of the directories above can be found as follows: + + • The total size of directory e is 584 because it contains a single file i of size 584 and no other directories. + • The directory a has total size 94853 because it contains files f (size 29116), g (size 2557), and h.lst (size + 62596), plus file i indirectly (a contains e which contains i). + • Directory d has total size 24933642. + • As the outermost directory, / contains every file. Its total size is 48381165, the sum of the size of every file. + + To begin, find all of the directories with a total size of at most 100000, then calculate the sum of their total sizes. + In the example above, these directories are a and e; the sum of their total sizes is 95437 (94853 + 584). (As in this + example, this process can count files more than once!) + + Find all of the directories with a total size of at most 100000. What is the sum of the total sizes of those + directories? + + Your puzzle answer was 1642503. + +--- Part Two --- + + Now, you're ready to choose a directory to delete. + + The total disk space available to the filesystem is 70000000. To run the update, you need unused space of at least + 30000000. You need to find a directory you can delete that will free up enough space to run the update. + + In the example above, the total size of the outermost directory (and thus the total amount of used space) is 48381165; + this means that the size of the unused space must currently be 21618835, which isn't quite the 30000000 required by the + update. Therefore, the update still requires a directory with total size of at least 8381165 to be deleted before it can + run. + + To achieve this, you have the following options: + + • Delete directory e, which would increase unused space by 584. + • Delete directory a, which would increase unused space by 94853. + • Delete directory d, which would increase unused space by 24933642. + • Delete directory /, which would increase unused space by 48381165. + + Directories e and a are both too small; deleting them would not free up enough space. However, directories d and / are + both big enough! Between these, choose the smallest: d, increasing unused space by 24933642. + + Find the smallest directory that, if deleted, would free up enough space on the filesystem to run the update. What is + the total size of that directory? + + Your puzzle answer was 6999588. + + Both parts of this puzzle are complete! They provide two gold stars: ** + +References + + Visible links + . https://adventofcode.com/ + . https://adventofcode.com/2022/about + . https://adventofcode.com/2022/events + . https://adventofcode.com/2022/settings + . https://adventofcode.com/2022/auth/logout + . Advent of Code Supporter + https://adventofcode.com/2022/support + . https://adventofcode.com/2022 + . https://adventofcode.com/2022 + . https://adventofcode.com/2022/support + . https://adventofcode.com/2022/sponsors + . https://adventofcode.com/2022/leaderboard + . https://adventofcode.com/2022/stats + . https://adventofcode.com/2022/sponsors + . https://adventofcode.com/2022 + . https://adventofcode.com/2022/day/7/input diff --git a/2022/day07/testinput b/2022/day07/testinput new file mode 100644 index 0000000..09a921e --- /dev/null +++ b/2022/day07/testinput @@ -0,0 +1,23 @@ +$ cd / +$ ls +dir a +14848514 b.txt +8504156 c.dat +dir d +$ cd a +$ ls +dir e +29116 f +2557 g +62596 h.lst +$ cd e +$ ls +584 i +$ cd .. +$ cd .. +$ cd d +$ ls +4060174 j +8033020 d.log +5626152 d.ext +7214296 k