From 4a49127af3b9fb548dbfa5c55fdc8e15b180eb80 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 8 Dec 2022 17:36:38 +0100 Subject: day07 --- day07 | 1083 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day07.rkt | 52 +++ day07.zig | 93 ++++++ 3 files changed, 1228 insertions(+) create mode 100644 day07 create mode 100644 day07.rkt create mode 100644 day07.zig diff --git a/day07 b/day07 new file mode 100644 index 0000000..7b80965 --- /dev/null +++ b/day07 @@ -0,0 +1,1083 @@ +$ cd / +$ ls +dir btsgrbd +3868 cprq.fmm +dir gcbpcf +dir hfm +324644 lthcng.gnf +133181 nblfzrb.mrr +140568 sfrbjmmh.jnj +dir tfsh +dir vlsqgrw +202279 vmpgqbcd +$ cd btsgrbd +$ ls +dir cmfdm +dir cqd +dir gvwvs +dir nblfzrb +dir nfm +293979 qwnml.bqn +159220 sdwnsgwv.mjm +327978 vzgwwjq.zbp +155479 zvspnvfr.zbc +$ cd cmfdm +$ ls +dir gldnjj +dir vhf +$ cd gldnjj +$ ls +dir dvght +93750 lwvtzd.pws +176529 sdwnsgwv.mjm +100111 vmpgqbcd +$ cd dvght +$ ls +dir tfbzq +$ cd tfbzq +$ ls +276592 tcghw.srg +$ cd .. +$ cd .. +$ cd .. +$ cd vhf +$ ls +240217 hfm.rfp +dir nblfzrb +$ cd nblfzrb +$ ls +160378 jhc +$ cd .. +$ cd .. +$ cd .. +$ cd cqd +$ ls +305358 bnddfgrb +dir dwqncqp +dir hnnfdtbh +dir jhc +dir nblfzrb +327762 scnm.qbf +165080 vmpgqbcd +190041 vzgwwjq.zbp +dir zwv +$ cd dwqncqp +$ ls +122570 slpgmhv +278461 zlnbcwr +$ cd .. +$ cd hnnfdtbh +$ ls +334830 gfprhn.rjj +$ cd .. +$ cd jhc +$ ls +179593 fgb.btb +$ cd .. +$ cd nblfzrb +$ ls +dir clbcgvhc +dir jhc +dir lsrnz +dir mctd +$ cd clbcgvhc +$ ls +285825 hnn +238272 nblfzrb.wvr +$ cd .. +$ cd jhc +$ ls +99731 nblfzrb.svz +$ cd .. +$ cd lsrnz +$ ls +257843 fsthnpmd +$ cd .. +$ cd mctd +$ ls +278117 zlnbcwr +$ cd .. +$ cd .. +$ cd zwv +$ ls +40349 jhc +dir pqwml +173804 sdwnsgwv.mjm +$ cd pqwml +$ ls +193573 hbzvzwpr +$ cd .. +$ cd .. +$ cd .. +$ cd gvwvs +$ ls +dir gjslw +dir gwz +dir ljvrjp +dir sltlpb +dir vbsnq +$ cd gjslw +$ ls +dir gzbm +$ cd gzbm +$ ls +dir fst +dir gpjz +dir gzd +dir hfm +$ cd fst +$ ls +99806 mqpg +$ cd .. +$ cd gpjz +$ ls +dir dnsvsp +218828 jhc.dfd +$ cd dnsvsp +$ ls +dir vmdbpwj +dir zvspnvfr +$ cd vmdbpwj +$ ls +258373 jhc +$ cd .. +$ cd zvspnvfr +$ ls +18241 vzgwwjq.zbp +$ cd .. +$ cd .. +$ cd .. +$ cd gzd +$ ls +20383 chdfwj +63309 prrlv.rvn +$ cd .. +$ cd hfm +$ ls +291753 qhh +$ cd .. +$ cd .. +$ cd .. +$ cd gwz +$ ls +29042 hfm.hpn +184043 mpc +230539 sdwnsgwv.mjm +803 zlnbcwr +$ cd .. +$ cd ljvrjp +$ ls +44312 pfltqw.zvc +$ cd .. +$ cd sltlpb +$ ls +321945 sdwnsgwv.mjm +$ cd .. +$ cd vbsnq +$ ls +7774 twbbg.ftq +109546 zpqbp.cts +$ cd .. +$ cd .. +$ cd nblfzrb +$ ls +89034 fst +338143 sdwnsgwv.mjm +130661 vmpgqbcd +130071 vrj.zlb +$ cd .. +$ cd nfm +$ ls +327853 fwmfmtt.hdg +151272 vdjs +dir wznwjfw +75692 zlnbcwr +$ cd wznwjfw +$ ls +dir nblfzrb +dir zvspnvfr +$ cd nblfzrb +$ ls +271817 fpqfgs.gdl +$ cd .. +$ cd zvspnvfr +$ ls +dir hfm +dir mdftsf +$ cd hfm +$ ls +dir fsgdq +dir grj +$ cd fsgdq +$ ls +244565 sdwnsgwv.mjm +$ cd .. +$ cd grj +$ ls +200989 rdvvrjf.fwz +$ cd .. +$ cd .. +$ cd mdftsf +$ ls +dir fnzztppm +21425 jhc.mtl +$ cd fnzztppm +$ ls +159263 bvf +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd gcbpcf +$ ls +295086 gfprhn.rjj +dir ldwwls +dir nblfzrb +dir zvspnvfr +dir zwv +$ cd ldwwls +$ ls +175977 sdwnsgwv.mjm +$ cd .. +$ cd nblfzrb +$ ls +dir mzshzw +$ cd mzshzw +$ ls +dir mllqf +$ cd mllqf +$ ls +113563 hfm.svt +$ cd .. +$ cd .. +$ cd .. +$ cd zvspnvfr +$ ls +84524 fst +dir jhc +dir llnqc +126979 nnrwp.shv +dir npjd +dir nptwgdbn +133618 qtn +dir rddftb +dir thfhbhz +129882 vmpgqbcd +170834 zlnbcwr +$ cd jhc +$ ls +dir pjlj +dir tfljr +$ cd pjlj +$ ls +dir jhc +$ cd jhc +$ ls +dir hqzm +$ cd hqzm +$ ls +159505 zlnbcwr +$ cd .. +$ cd .. +$ cd .. +$ cd tfljr +$ ls +dir dwhjmg +$ cd dwhjmg +$ ls +dir nblfzrb +$ cd nblfzrb +$ ls +194723 ppptz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd llnqc +$ ls +194898 vzgwwjq.zbp +$ cd .. +$ cd npjd +$ ls +dir blvfljfn +dir dzpplzl +101118 gfprhn.rjj +116164 hbbnl.nvz +211685 hfm +dir jhc +dir mcnftw +dir nblfzrb +dir ncpchrj +dir qrdczsdf +165190 ttbj +190954 vzgwwjq.zbp +$ cd blvfljfn +$ ls +283460 gfprhn.rjj +148100 sqljld +$ cd .. +$ cd dzpplzl +$ ls +62592 jjlvdvq.mqp +$ cd .. +$ cd jhc +$ ls +90578 dvd.wvl +165955 zqplggf +$ cd .. +$ cd mcnftw +$ ls +157908 gbqpdt +dir jsnrcfbv +dir nblfzrb +322169 qlmd.dbq +dir zvspnvfr +$ cd jsnrcfbv +$ ls +dir gffj +75682 hfm +dir pmfwlzn +160494 vzgwwjq.zbp +$ cd gffj +$ ls +dir nblfzrb +9340 pcqs +$ cd nblfzrb +$ ls +216547 sdwnsgwv.mjm +135235 zvspnvfr.nvs +$ cd .. +$ cd .. +$ cd pmfwlzn +$ ls +137069 jbsmwm +$ cd .. +$ cd .. +$ cd nblfzrb +$ ls +dir hflnbmcd +11899 vzgwwjq.zbp +334607 whmq.ftc +347595 zlnbcwr +$ cd hflnbmcd +$ ls +275968 sdwnsgwv.mjm +$ cd .. +$ cd .. +$ cd zvspnvfr +$ ls +75213 vmpgqbcd +$ cd .. +$ cd .. +$ cd nblfzrb +$ ls +dir jhc +$ cd jhc +$ ls +dir jhc +$ cd jhc +$ ls +218236 nblfzrb.mbc +$ cd .. +$ cd .. +$ cd .. +$ cd ncpchrj +$ ls +dir cnswqjjd +dir gvmlgjnj +$ cd cnswqjjd +$ ls +134879 qpzfr +30171 zvspnvfr +$ cd .. +$ cd gvmlgjnj +$ ls +209285 fst +$ cd .. +$ cd .. +$ cd qrdczsdf +$ ls +201094 zlnbcwr +$ cd .. +$ cd .. +$ cd nptwgdbn +$ ls +217555 gfprhn.rjj +66582 mrhhpr.gdg +$ cd .. +$ cd rddftb +$ ls +205347 sdwnsgwv.mjm +195976 vzgwwjq.zbp +$ cd .. +$ cd thfhbhz +$ ls +321628 zlnbcwr +$ cd .. +$ cd .. +$ cd zwv +$ ls +dir lnqb +311249 mtv +dir tnrtjrlz +$ cd lnqb +$ ls +dir vjtnjt +$ cd vjtnjt +$ ls +66715 hfm +$ cd .. +$ cd .. +$ cd tnrtjrlz +$ ls +270833 jhc.vpj +$ cd .. +$ cd .. +$ cd .. +$ cd hfm +$ ls +220753 fst.tfn +dir hfm +154659 mnzlvc.jzw +dir nblfzrb +dir nrqhstc +dir vwc +dir wlpdphhg +dir zpbh +dir zvspnvfr +160091 zvspnvfr.zqt +$ cd hfm +$ ls +dir fst +dir mfmcqhbz +dir pdblpszb +$ cd fst +$ ls +dir cpll +dir fst +dir nblfzrb +dir sbnplvz +dir vsd +307662 wlzbsdh +194873 zlnbcwr +$ cd cpll +$ ls +13452 pwcmhcf.lwz +$ cd .. +$ cd fst +$ ls +dir nblfzrb +dir rqssg +$ cd nblfzrb +$ ls +dir ltrc +dir nblfzrb +326613 vhwnz +244908 vzgwwjq.zbp +$ cd ltrc +$ ls +163670 zlnbcwr +$ cd .. +$ cd nblfzrb +$ ls +dir brpzhf +61846 fst +66356 vccrmc.jbd +72767 vmpgqbcd +$ cd brpzhf +$ ls +224488 mttr.szf +$ cd .. +$ cd .. +$ cd .. +$ cd rqssg +$ ls +131534 bfmhl.cwj +261143 hhznnd +dir nmb +74816 vnmwgflj.rhp +251308 wdzfzcss.fdc +$ cd nmb +$ ls +264421 fst.twf +dir jng +81009 rnt.dlp +288053 sftvd +dir vfc +$ cd jng +$ ls +8014 gfprhn.rjj +168688 ppn.qbv +$ cd .. +$ cd vfc +$ ls +dir bhgstrdl +138108 dggwnqp.fpl +303194 gfprhn.rjj +$ cd bhgstrdl +$ ls +102559 fst.nrv +269697 trqrlrw.jmc +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd nblfzrb +$ ls +dir bcj +dir cqjmmr +256392 fst.tjf +7079 gfprhn.rjj +dir gqm +dir hbqfms +dir nmgbfhmq +57962 pvdtbdj +117035 sdwnsgwv.mjm +64208 zlnbcwr +$ cd bcj +$ ls +dir nblfzrb +$ cd nblfzrb +$ ls +149417 zvspnvfr.dqv +$ cd .. +$ cd .. +$ cd cqjmmr +$ ls +147054 fcq.mmz +dir fst +dir hfm +110840 jcsjjj.mvg +75746 vzgwwjq.zbp +333085 zlnbcwr +$ cd fst +$ ls +114747 vmpgqbcd +$ cd .. +$ cd hfm +$ ls +dir nvvppmls +dir phpq +dir rld +$ cd nvvppmls +$ ls +dir cdsmgjdb +$ cd cdsmgjdb +$ ls +158739 hfm.nmw +$ cd .. +$ cd .. +$ cd phpq +$ ls +274026 hfm.hjj +$ cd .. +$ cd rld +$ ls +237915 hsnngd.tqw +$ cd .. +$ cd .. +$ cd .. +$ cd gqm +$ ls +188469 vzgwwjq.zbp +$ cd .. +$ cd hbqfms +$ ls +127163 fst +$ cd .. +$ cd nmgbfhmq +$ ls +257647 gczqzbbd +dir hfm +dir zvspnvfr +$ cd hfm +$ ls +267774 gbtwdctn.drw +$ cd .. +$ cd zvspnvfr +$ ls +189769 gfprhn.rjj +$ cd .. +$ cd .. +$ cd .. +$ cd sbnplvz +$ ls +dir bln +dir fst +dir hdv +dir jhc +31790 jhc.zvm +102276 mmmjpp +dir zdwzgnb +$ cd bln +$ ls +161029 zlnbcwr +$ cd .. +$ cd fst +$ ls +333565 fst +$ cd .. +$ cd hdv +$ ls +dir zvpp +$ cd zvpp +$ ls +143723 gfprhn.rjj +$ cd .. +$ cd .. +$ cd jhc +$ ls +dir hfm +dir hnqpwnfb +dir jqgfcmjn +dir mqj +312345 psdd.ftf +dir slsq +319904 thvhdmm.rpq +317429 vzgwwjq.zbp +$ cd hfm +$ ls +249321 bwzrcghg.zwq +38324 zlnbcwr +149647 zvspnvfr.jwn +$ cd .. +$ cd hnqpwnfb +$ ls +113118 bdmtgt +299110 bthznc.bjt +$ cd .. +$ cd jqgfcmjn +$ ls +176495 hfm.wnw +333817 mqds +208755 vqsmz.cbd +298875 zltfds +$ cd .. +$ cd mqj +$ ls +186649 brfmpbwn +43322 cgdhzzl.bvg +56230 zlnbcwr +$ cd .. +$ cd slsq +$ ls +dir dtsg +343453 rjgvt.sgc +313784 vmpgqbcd +$ cd dtsg +$ ls +308070 zvspnvfr.dqq +$ cd .. +$ cd .. +$ cd .. +$ cd zdwzgnb +$ ls +dir zvspnvfr +$ cd zvspnvfr +$ ls +278754 glqszhnp +199713 sdwnsgwv.mjm +$ cd .. +$ cd .. +$ cd .. +$ cd vsd +$ ls +17978 fdrg.cft +320284 sdwnsgwv.mjm +230059 tpfmtww.jbs +168301 vmpgqbcd +$ cd .. +$ cd .. +$ cd mfmcqhbz +$ ls +dir fst +dir lwlsnt +dir mqqdst +$ cd fst +$ ls +dir cln +235621 hfm.mhr +4347 qwfwj +226297 zlnbcwr +$ cd cln +$ ls +dir cfz +dir njptzz +dir rcbps +161994 rjl.pfg +dir tcqttb +22809 vmpgqbcd +225782 zvspnvfr.gbw +$ cd cfz +$ ls +168184 zvspnvfr +$ cd .. +$ cd njptzz +$ ls +286155 hfm.mjh +$ cd .. +$ cd rcbps +$ ls +256675 vzgwwjq.zbp +$ cd .. +$ cd tcqttb +$ ls +91505 flmgglv.gjw +270377 ftbjzt.lcm +$ cd .. +$ cd .. +$ cd .. +$ cd lwlsnt +$ ls +dir ztrcwjw +$ cd ztrcwjw +$ ls +342059 vjcldgfj.twb +$ cd .. +$ cd .. +$ cd mqqdst +$ ls +326143 dhlhzzj.sbh +dir lmsbg +13108 nblfzrb.nnr +256325 vzgwwjq.zbp +146133 zlnbcwr +$ cd lmsbg +$ ls +282223 zvspnvfr.zwq +$ cd .. +$ cd .. +$ cd .. +$ cd pdblpszb +$ ls +167164 sdwnsgwv.mjm +$ cd .. +$ cd .. +$ cd nblfzrb +$ ls +161555 zlnbcwr +$ cd .. +$ cd nrqhstc +$ ls +316797 gfprhn.rjj +263838 nblfzrb.cjr +dir pppmp +dir shcwlggz +$ cd pppmp +$ ls +dir fst +328237 gfprhn.rjj +288094 hzv.prf +260275 pcnhmmw +214496 pptjlrn.pls +dir tqjgrc +315647 vmbvl.zvh +$ cd fst +$ ls +4441 dtt +115073 jhdqj.tpt +337681 zlnbcwr +$ cd .. +$ cd tqjgrc +$ ls +6491 zlnbcwr +$ cd .. +$ cd .. +$ cd shcwlggz +$ ls +251509 gfprhn.rjj +331578 slsnbzm +158534 wlchqtzj.ljz +341686 wmdvjs.tjb +$ cd .. +$ cd .. +$ cd vwc +$ ls +94599 gfprhn.rjj +298110 hql.hss +dir rsrfc +230201 vmpgqbcd +dir zcsqst +dir zrz +58810 zvspnvfr +$ cd rsrfc +$ ls +dir fst +$ cd fst +$ ls +259462 bflpjwd.wbj +177686 cvh.fnr +$ cd .. +$ cd .. +$ cd zcsqst +$ ls +344114 bbbd.ncl +282111 gfprhn.rjj +327171 jhc +43867 qwzwmffq.chl +$ cd .. +$ cd zrz +$ ls +dir zvspnvfr +$ cd zvspnvfr +$ ls +dir fst +$ cd fst +$ ls +218902 fst.jmg +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wlpdphhg +$ ls +dir lllph +41390 wmn +$ cd lllph +$ ls +167740 bbfqftlg.pll +$ cd .. +$ cd .. +$ cd zpbh +$ ls +63568 jhc.tfs +dir nblfzrb +dir rlsvrfrb +29977 vzgwwjq.zbp +dir zvspnvfr +$ cd nblfzrb +$ ls +154990 gbvpw.bpm +dir hfm +148488 jhc +301795 jhc.zlg +dir nblfzrb +115285 rvhcfsrw.crj +320034 wsqdt +$ cd hfm +$ ls +141530 vmpgqbcd +$ cd .. +$ cd nblfzrb +$ ls +dir rnhn +$ cd rnhn +$ ls +46251 sdwnsgwv.mjm +$ cd .. +$ cd .. +$ cd .. +$ cd rlsvrfrb +$ ls +140923 hfm +228981 zvspnvfr.mvl +$ cd .. +$ cd zvspnvfr +$ ls +182280 gnhh.hpw +95206 vvlt.pgf +$ cd .. +$ cd .. +$ cd zvspnvfr +$ ls +234551 wsfjhlqc.zsj +$ cd .. +$ cd .. +$ cd tfsh +$ ls +dir fbrqvwgr +$ cd fbrqvwgr +$ ls +244821 nrhm +$ cd .. +$ cd .. +$ cd vlsqgrw +$ ls +dir dzdd +18805 fst.rjm +50694 gfprhn.rjj +55025 jlnrm +dir pnsbfz +dir qjjjgd +dir whrtnh +28406 zggjjcct.fsz +$ cd dzdd +$ ls +dir hjmv +$ cd hjmv +$ ls +91558 hfm.qcd +$ cd .. +$ cd .. +$ cd pnsbfz +$ ls +dir bmgmh +dir nblfzrb +dir zvfg +$ cd bmgmh +$ ls +dir dvh +dir mwfbthpj +dir swqbph +$ cd dvh +$ ls +dir jhc +dir jtp +85638 rzlt.llb +$ cd jhc +$ ls +dir dltqbnpq +$ cd dltqbnpq +$ ls +133426 zlnbcwr +$ cd .. +$ cd .. +$ cd jtp +$ ls +dir clh +$ cd clh +$ ls +54117 sdwnsgwv.mjm +$ cd .. +$ cd .. +$ cd .. +$ cd mwfbthpj +$ ls +75900 zwslwbr.chm +$ cd .. +$ cd swqbph +$ ls +307258 jrlljc.ntl +$ cd .. +$ cd .. +$ cd nblfzrb +$ ls +106071 zdjg.qsj +$ cd .. +$ cd zvfg +$ ls +311338 zvspnvfr.dqj +$ cd .. +$ cd .. +$ cd qjjjgd +$ ls +dir cdmwgn +dir fqmln +285733 gfprhn.rjj +dir gswsc +dir htpzdb +261929 jwc +dir lvzpqqv +dir mlc +dir mzbpmhf +329303 sdwnsgwv.mjm +76120 vmpgqbcd +$ cd cdmwgn +$ ls +dir bttff +$ cd bttff +$ ls +dir nblfzrb +$ cd nblfzrb +$ ls +346048 jhc.hzf +dir nccq +dir rwtddj +149890 sdwnsgwv.mjm +$ cd nccq +$ ls +141144 gfprhn.rjj +65628 hfm +$ cd .. +$ cd rwtddj +$ ls +dir jctw +$ cd jctw +$ ls +141075 thcfz.frn +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd fqmln +$ ls +282233 cjgh +$ cd .. +$ cd gswsc +$ ls +153758 ccjg.zml +dir cllgt +257967 ctqdpq.clq +117673 jhc +258604 wqcz.tww +122135 zvspnvfr.grb +$ cd cllgt +$ ls +132862 vmpgqbcd +$ cd .. +$ cd .. +$ cd htpzdb +$ ls +dir hfm +dir mlplp +231759 nblfzrb +159823 pqpbjbqp +25382 vzgwwjq.zbp +$ cd hfm +$ ls +155413 bbbsd +288638 jhhmwq +dir lnss +$ cd lnss +$ ls +341786 zvspnvfr +$ cd .. +$ cd .. +$ cd mlplp +$ ls +256802 zhcq.gzj +$ cd .. +$ cd .. +$ cd lvzpqqv +$ ls +dir cgj +dir mdb +dir shpdtb +$ cd cgj +$ ls +74595 nblfzrb.lcc +$ cd .. +$ cd mdb +$ ls +167891 zvspnvfr.ldc +$ cd .. +$ cd shpdtb +$ ls +45889 bvff.hsf +92447 sdwnsgwv.mjm +$ cd .. +$ cd .. +$ cd mlc +$ ls +7978 mrblf +$ cd .. +$ cd mzbpmhf +$ ls +38713 jhbs +$ cd .. +$ cd .. +$ cd whrtnh +$ ls +dir jbbwzd +dir nblfzrb +$ cd jbbwzd +$ ls +88613 jhc.qqw +$ cd .. +$ cd nblfzrb +$ ls +164792 zlnbcwr diff --git a/day07.rkt b/day07.rkt new file mode 100644 index 0000000..7bcb3ca --- /dev/null +++ b/day07.rkt @@ -0,0 +1,52 @@ +#lang racket + +(define data (file->lines "day07")) + +(define fs (make-hash)) +(let loop ([data (cdr data)] + [cwd '()]) + (let ([cmd (car data)] + [rest (cdr data)]) + (cond [(string-prefix? cmd "$ cd") + (let ([dir (third (string-split cmd " "))]) + (if (equal? dir "..") + (loop rest (cdr cwd)) + (loop rest (cons dir cwd))))] + [(equal? cmd "$ ls") + (let dir-loop ([dir-list rest]) + (if (null? dir-list) + (void) ; done + (let ([line (car dir-list)] + [dir-rest (cdr dir-list)]) + (cond [(string-prefix? line "$") + (loop (cons line dir-rest) cwd)] ; push back line + [(string-prefix? line "dir") + (dir-loop dir-rest)] ; ignore + [else + (let* ([fields (string-split line " ")] + [size (string->number (first fields))]) + (let segment-loop ([dir cwd]) + (hash-update! fs + (string-join (reverse dir) "/" #:before-first "/") + (curry + size) + 0) + (unless (null? dir) + (segment-loop (cdr dir)))) + (dir-loop dir-rest))]))))] + [else (error "invalid line")]))) + +(for/sum ([(dir size) fs]) + (if (<= size 100000) + size + 0)) +; 1306611 + +(let* ([total (hash-ref fs "/")] + [unused (- 70000000 total)] + [needed (- 30000000 unused)]) + (for/fold ([smallest total]) + ([(dir size) fs]) + (if (>= size needed) + (min smallest size) + smallest))) +; 13210366 diff --git a/day07.zig b/day07.zig new file mode 100644 index 0000000..edc16df --- /dev/null +++ b/day07.zig @@ -0,0 +1,93 @@ +const std = @import("std"); +const data = @embedFile("day07"); + +var general_purpose_allocator = std.heap.GeneralPurposeAllocator(.{}){}; +const gpa = general_purpose_allocator.allocator(); + +const eql = std.mem.eql; + +pub fn main() !void { + defer std.debug.assert(!general_purpose_allocator.deinit()); + + var lines = std.mem.tokenize(u8, data, "\n"); + + var fs = std.StringHashMap(usize).init(gpa); + defer { + var keyiter = fs.keyIterator(); + while (keyiter.next()) |key| { + gpa.free(key.*); + } + fs.deinit(); + } + + var cwd = std.ArrayList([]const u8).init(gpa); + defer cwd.deinit(); + + _ = lines.next(); + var root = try gpa.dupe(u8, "/"); + + while (lines.next()) |line| { + if (line.len == 0) + break; + + if (eql(u8, line[0..4], "$ cd")) { + var parts = std.mem.tokenize(u8, line, " "); + _ = parts.next(); + _ = parts.next(); + var dir = parts.next().?; + + if (eql(u8, dir, "..")) { + _ = cwd.pop(); + } else { + try cwd.append(dir); + } + } else if (eql(u8, line, "$ ls")) { + while (lines.peek()) |dirline| { + if (dirline[0] == '$') break; + _ = lines.next(); + + if (eql(u8, dirline[0..4], "dir ")) continue; + + var parts = std.mem.split(u8, dirline, " "); + + var size = try std.fmt.parseInt(usize, parts.first(), 10); + + try fs.put(root, (fs.get(root) orelse 0) + size); + + var fullpath = std.ArrayList(u8).init(gpa); + defer fullpath.deinit(); + for (cwd.items) |segment| { + try fullpath.append('/'); + try fullpath.appendSlice(segment); + + var s = try gpa.dupe(u8, fullpath.items); + + var v = try fs.getOrPut(s); + if (!v.found_existing) { + v.value_ptr.* = size; + } else { + v.value_ptr.* += size; + gpa.free(s); + } + } + } + } + } + + var part1: usize = 0; + var iter = fs.iterator(); + while (iter.next()) |entry| { + if (entry.value_ptr.* <= 100_000) + part1 += entry.value_ptr.*; + } + + var needed: usize = 30000000 - (70000000 - fs.get(root).?); + var part2: usize = 70000000; + var iter2 = fs.iterator(); + while (iter2.next()) |entry| { + if (entry.value_ptr.* > needed) + part2 = std.math.min(part2, entry.value_ptr.*); + } + + std.debug.print("{}\n{}\n", .{part1, part2}); +} -- cgit 1.4.1