From 461a4ca15c3861e0035b3c57503eef23d37ef087 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Wed, 7 Dec 2022 18:43:09 -0500 Subject: [PATCH] day 7 --- 2022/07/input | 1079 +++++++++++++++++++++++++++++++++++++++++++++ 2022/07/part1.js | 68 +++ 2022/07/part2.js | 25 ++ 2022/07/puzzle.md | 97 ++++ 2022/07/sample | 23 + 2022/07/test.js | 36 ++ 6 files changed, 1328 insertions(+) create mode 100644 2022/07/input create mode 100644 2022/07/part1.js create mode 100644 2022/07/part2.js create mode 100644 2022/07/puzzle.md create mode 100644 2022/07/sample create mode 100644 2022/07/test.js diff --git a/2022/07/input b/2022/07/input new file mode 100644 index 0000000..8f22e2f --- /dev/null +++ b/2022/07/input @@ -0,0 +1,1079 @@ +$ cd / +$ ls +dir dpbwg +dir dvwfscw +dir hccpl +dir jsgbg +dir lhjmzsl +63532 mwvbpw.mmg +239480 npj +dir pngs +dir qhs +303649 shvgmwn.vhv +236905 sjrrgd.phh +dir sntcp +dir sqs +$ cd dpbwg +$ ls +dir dgh +100731 dpbwg +dir rpwnv +$ cd dgh +$ ls +197049 lhjmzsl.hzj +$ cd .. +$ cd rpwnv +$ ls +10702 qsgv.fmf +$ cd .. +$ cd .. +$ cd dvwfscw +$ ls +dir bvg +dir fbfjs +115450 gjftb.mgd +dir gsmnprgz +dir hdwdcvv +dir mhjtrlqz +75437 qsctddrw +171722 qsgv.zqz +$ cd bvg +$ ls +56335 cgtzb.szt +139481 shvgmwn.vhv +255200 wzqlgr.mhl +$ cd .. +$ cd fbfjs +$ ls +252977 hmcj +256083 mbgfn.pmh +dir qsgv +$ cd qsgv +$ ls +271506 dchsdfz.bbg +202650 hmcj +32623 lqgmfcp +57614 mgp.fbn +220895 qwzqrrq.wjf +$ cd .. +$ cd .. +$ cd gsmnprgz +$ ls +dir dzcsldzw +dir hrjmfd +dir lcwv +dir sdp +62355 zrncdmd.lmj +$ cd dzcsldzw +$ ls +dir hmcj +$ cd hmcj +$ ls +151947 hgtzldbg +$ cd .. +$ cd .. +$ cd hrjmfd +$ ls +dir drjlhbqf +65599 npj +$ cd drjlhbqf +$ ls +263623 hzfmzs.mlj +13866 npj +173713 wtnf.qps +$ cd .. +$ cd .. +$ cd lcwv +$ ls +44150 hmcj.lds +200694 mpbb +$ cd .. +$ cd sdp +$ ls +dir gcwjj +dir qzdczvwn +$ cd gcwjj +$ ls +149603 qsgv.srr +$ cd .. +$ cd qzdczvwn +$ ls +dir gnvbm +291187 hmcj.rgm +dir msdt +dir mwvbpw +dir shpr +$ cd gnvbm +$ ls +259516 dpbwg +120868 mpbb +dir vqrcd +$ cd vqrcd +$ ls +306804 fqqg +34290 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd msdt +$ ls +168438 mpbb +67435 nbqcrdjs +$ cd .. +$ cd mwvbpw +$ ls +dir btzqzvbl +308719 npj +$ cd btzqzvbl +$ ls +177311 bdnrf.jtw +122356 qwhmd.vcd +169153 vzzzccg.hlb +$ cd .. +$ cd .. +$ cd shpr +$ ls +290591 nblzc.nmp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd hdwdcvv +$ ls +96864 hmcj.tjn +$ cd .. +$ cd mhjtrlqz +$ ls +dir bzrbsfjp +238772 fpggqqj +dir hqzgs +20155 shvgmwn.vhv +$ cd bzrbsfjp +$ ls +313691 fnscbhfc +17630 llwfdzgg.bsp +dir lthr +$ cd lthr +$ ls +237053 bhbbzt.bmt +$ cd .. +$ cd .. +$ cd hqzgs +$ ls +295258 gllsgr.nnz +70743 ptpqd +dir rnmsdpmj +205022 rpqh.rpn +158287 tsm.tdq +154025 wmfwr.bcm +$ cd rnmsdpmj +$ ls +218043 dpbwg.mls +149072 mbgfn.pmh +89388 mwvbpw.qfm +57207 rszcvm.mqc +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd hccpl +$ ls +dir blqdjh +dir hfqw +dir qsgv +$ cd blqdjh +$ ls +dir fbpg +$ cd fbpg +$ ls +231357 dpbwg +$ cd .. +$ cd .. +$ cd hfqw +$ ls +210898 fjblghm.gtg +286252 hmcj.dgz +258768 mpbb +225743 qsgv.pqf +191717 shvgmwn.vhv +$ cd .. +$ cd qsgv +$ ls +dir bslqpr +dir dpbwg +dir lhjmzsl +210707 npj +64435 qhpnrbhq +dir rpj +301426 shvgmwn.vhv +dir stbgbrw +dir vhzmg +$ cd bslqpr +$ ls +4365 dpbwg +dir fpj +dir lhjmzsl +dir lncmt +dir mwvbpw +35725 ncbjtpcb.svf +dir njhsmb +dir rjzsddlw +41533 shvgmwn.vhv +$ cd fpj +$ ls +88146 dpbwg +$ cd .. +$ cd lhjmzsl +$ ls +57400 fzngdsh.sbn +198711 mnqz.npt +$ cd .. +$ cd lncmt +$ ls +192228 qsgv.jss +$ cd .. +$ cd mwvbpw +$ ls +65217 hmcj.drs +$ cd .. +$ cd njhsmb +$ ls +157177 dpbwg.wsl +10919 jhfs +$ cd .. +$ cd rjzsddlw +$ ls +dir cftp +dir flcfwml +dir vpdbl +$ cd cftp +$ ls +89075 dpbwg +57259 mbgfn.pmh +237771 zwglrhh +$ cd .. +$ cd flcfwml +$ ls +51498 pbbgmqn.gfg +$ cd .. +$ cd vpdbl +$ ls +98690 npj +$ cd .. +$ cd .. +$ cd .. +$ cd dpbwg +$ ls +dir wlmdbjh +$ cd wlmdbjh +$ ls +dir pmldd +245468 rdgldw.tzb +$ cd pmldd +$ ls +145032 mwvbpw.tpc +$ cd .. +$ cd .. +$ cd .. +$ cd lhjmzsl +$ ls +233642 dpbwg.tbq +dir frsggjl +dir hmcj +dir jmhzlq +dir jvdtpzd +251765 npj +169647 shvgmwn.vhv +$ cd frsggjl +$ ls +dir dshv +dir fqvpc +dir lhjmzsl +313045 npj +dir rngqmwgr +$ cd dshv +$ ls +dir pptfqn +85609 wpgz +dir zfmqls +$ cd pptfqn +$ ls +258817 mtctcdgd.nmb +39899 npj +$ cd .. +$ cd zfmqls +$ ls +119006 hlw.mzg +295107 wstvdqn.wgw +$ cd .. +$ cd .. +$ cd fqvpc +$ ls +dir pqpf +$ cd pqpf +$ ls +312566 mpbb +$ cd .. +$ cd .. +$ cd lhjmzsl +$ ls +157261 dpbwg.bgc +76700 mpbb +$ cd .. +$ cd rngqmwgr +$ ls +42626 dpbwg.dtt +78765 gjsnmzn.fzb +$ cd .. +$ cd .. +$ cd hmcj +$ ls +166183 jntzn +dir qsgv +254851 rbcgrdr.vqp +$ cd qsgv +$ ls +256449 dhj.mrm +49207 fbrhl +69922 lhjmzsl +121778 tpdvnb +$ cd .. +$ cd .. +$ cd jmhzlq +$ ls +dir bbpqsf +245813 ftw.jwq +$ cd bbpqsf +$ ls +169373 mwvbpw.tjt +$ cd .. +$ cd .. +$ cd jvdtpzd +$ ls +dir brfln +dir dpbwg +263586 fsqzfhj.bzh +85956 lhjmzsl +dir mwvbpw +dir tcgwhp +91473 vjgt.twz +200413 zlnnrrpn.qqd +$ cd brfln +$ ls +65066 fpvnm +$ cd .. +$ cd dpbwg +$ ls +dir jtqwgc +dir lrdjdqn +281885 mpbb +$ cd jtqwgc +$ ls +219022 spbqn +$ cd .. +$ cd lrdjdqn +$ ls +116830 hmcj.ptr +dir mwvbpw +$ cd mwvbpw +$ ls +251737 ccqlb +$ cd .. +$ cd .. +$ cd .. +$ cd mwvbpw +$ ls +226329 fztjpfb +$ cd .. +$ cd tcgwhp +$ ls +dir grmsl +$ cd grmsl +$ ls +dir hmcj +197995 wzqlr.fqj +$ cd hmcj +$ ls +239775 mbgfn.pmh +dir rrvccjp +95381 shvgmwn.vhv +$ cd rrvccjp +$ ls +dir qsgv +$ cd qsgv +$ ls +192956 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rpj +$ ls +dir bwc +dir ctjwjlzc +dir dpbwg +dir lhjmzsl +249767 npj +dir wqmlz +$ cd bwc +$ ls +56181 hmcj.dhd +105111 hvw +63869 rqw.srq +8030 shvgmwn.vhv +dir stff +$ cd stff +$ ls +dir gsjsc +$ cd gsjsc +$ ls +514 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd .. +$ cd ctjwjlzc +$ ls +39823 qjgnjm.hzn +$ cd .. +$ cd dpbwg +$ ls +dir lhqrjr +$ cd lhqrjr +$ ls +179949 tmvl.zqf +$ cd .. +$ cd .. +$ cd lhjmzsl +$ ls +305735 shvgmwn.vhv +$ cd .. +$ cd wqmlz +$ ls +141757 fstftggh +$ cd .. +$ cd .. +$ cd stbgbrw +$ ls +196003 dpbwg.shs +dir gbr +104091 hmcj +232145 lhjmzsl.nnc +dir lhv +dir qqqbtpq +51208 qsgv.dbm +235090 vbpzgnr +dir vqmnsdrt +$ cd gbr +$ ls +101809 npj +121922 pcqrmmlt.ghh +125915 ptffhc +128293 wdz.nsd +$ cd .. +$ cd lhv +$ ls +75506 qtwlnvv.nbm +28413 rbwbp +$ cd .. +$ cd qqqbtpq +$ ls +96300 gdf +$ cd .. +$ cd vqmnsdrt +$ ls +146229 mwvbpw.qrc +189540 plldv.vtv +dir rtng +$ cd rtng +$ ls +42730 hrbs.zpc +$ cd .. +$ cd .. +$ cd .. +$ cd vhzmg +$ ls +dir dcmjvhtt +dir dpbwg +$ cd dcmjvhtt +$ ls +85508 hhlctr.bbs +296657 lhjmzsl.zjt +255803 mbgfn.pmh +170803 mtctcdgd.nmb +$ cd .. +$ cd dpbwg +$ ls +156142 hmcj +dir lhjmzsl +$ cd lhjmzsl +$ ls +13590 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jsgbg +$ ls +202682 hmcj.tbl +$ cd .. +$ cd lhjmzsl +$ ls +197308 lhjmzsl +$ cd .. +$ cd pngs +$ ls +115979 hfpzqf.hjw +dir lhjmzsl +125414 mpbb +dir mwbffchr +$ cd lhjmzsl +$ ls +55108 hwhsjr +$ cd .. +$ cd mwbffchr +$ ls +249138 czv.lgd +$ cd .. +$ cd .. +$ cd qhs +$ ls +108106 ccmhjzp.ppf +197911 shvgmwn.vhv +$ cd .. +$ cd sntcp +$ ls +dir cvgtwsbw +dir dpbwg +dir dqbzfcq +dir gfvtsjmz +dir ljsmjsp +dir mwvbpw +dir phfqzwp +dir rpsnfndl +dir rrqwcbqm +dir rtv +dir vnvft +dir vqcvbncp +dir vsgjds +$ cd cvgtwsbw +$ ls +dir bprbr +dir bpw +dir dpbwg +dir jvjs +dir mwvbpw +dir nbfzn +dir pcr +dir rhgltw +$ cd bprbr +$ ls +126107 dpbwg.jqq +140931 ljhc.gsm +120326 pnjv +dir wbrb +$ cd wbrb +$ ls +28887 ccr.rvd +dir gwfpgws +77898 mwvbpw.qrc +280487 pbfbtb.qjp +$ cd gwfpgws +$ ls +205904 dgm +313203 vpgzr.jfw +$ cd .. +$ cd .. +$ cd .. +$ cd bpw +$ ls +dir lgstpgrn +173334 npj +$ cd lgstpgrn +$ ls +dir rngr +$ cd rngr +$ ls +291856 czjz.fzp +246123 mpbb +200301 qqfvpnz +$ cd .. +$ cd .. +$ cd .. +$ cd dpbwg +$ ls +265739 mtctcdgd.nmb +dir mwvbpw +76505 ntjn.mnp +227157 pqznms +312879 qzlmsht +$ cd mwvbpw +$ ls +144523 dpbwg.lgr +39441 mmtrnzqw.mnv +285772 mpbb +306936 rpmgcmqd.qht +$ cd .. +$ cd .. +$ cd jvjs +$ ls +51134 lsnvhd.gsj +$ cd .. +$ cd mwvbpw +$ ls +125428 hmcj.bvd +40162 npj +117658 pmzfj.crj +$ cd .. +$ cd nbfzn +$ ls +11429 mwvbpw.swl +5582 qsgv.jrm +$ cd .. +$ cd pcr +$ ls +dir dpbwg +95858 mtctcdgd.nmb +$ cd dpbwg +$ ls +dir wmsb +$ cd wmsb +$ ls +154498 npj +$ cd .. +$ cd .. +$ cd .. +$ cd rhgltw +$ ls +188447 qsgv.rzs +$ cd .. +$ cd .. +$ cd dpbwg +$ ls +261326 cjfszwr +88528 glp.fgc +dir lhjmzsl +270470 shvgmwn.vhv +310081 vdhmgb +dir wsc +$ cd lhjmzsl +$ ls +245410 dnnfljbs +$ cd .. +$ cd wsc +$ ls +dir lhjmzsl +311302 mbs.tqc +309985 mpbb +dir qgpqzr +20812 vhcc.wpw +$ cd lhjmzsl +$ ls +dir dpbwg +$ cd dpbwg +$ ls +273340 fmvbcphl.crp +$ cd .. +$ cd .. +$ cd qgpqzr +$ ls +dir fdwgdhrf +$ cd fdwgdhrf +$ ls +275421 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd dqbzfcq +$ ls +30930 njnz +$ cd .. +$ cd gfvtsjmz +$ ls +290134 mbgfn.pmh +154689 vql +$ cd .. +$ cd ljsmjsp +$ ls +59894 hwggw.rpd +26336 mbgfn.pmh +dir mvzchpq +dir mwvbpw +122061 zlcr.jsw +$ cd mvzchpq +$ ls +294618 lhjmzsl.bqm +dir mwvbpw +204294 nljhbh.cfc +79337 ntvt.dbw +$ cd mwvbpw +$ ls +dir njwcqcjh +309971 nqdv.vmp +$ cd njwcqcjh +$ ls +155159 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd .. +$ cd mwvbpw +$ ls +43451 lhjmzsl.vjt +$ cd .. +$ cd .. +$ cd mwvbpw +$ ls +3144 phvjzs.prf +$ cd .. +$ cd phfqzwp +$ ls +dir hmcj +dir hnnrw +196279 lhjmzsl +90948 mwvbpw.hvm +275082 shvgmwn.vhv +$ cd hmcj +$ ls +4346 mtctcdgd.nmb +$ cd .. +$ cd hnnrw +$ ls +dir lhjmzsl +103070 qsbrf.tpv +dir rdf +$ cd lhjmzsl +$ ls +158692 dpbwg.bvl +$ cd .. +$ cd rdf +$ ls +dir gpj +dir lbgpm +$ cd gpj +$ ls +136508 shvgmwn.vhv +$ cd .. +$ cd lbgpm +$ ls +154457 ccpvr +232065 lhjmzsl +43787 npj +189920 rpgmvv +96362 tlmvpc +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rpsnfndl +$ ls +dir pcbtmcwf +$ cd pcbtmcwf +$ ls +dir qsgv +$ cd qsgv +$ ls +45662 npj +$ cd .. +$ cd .. +$ cd .. +$ cd rrqwcbqm +$ ls +dir vjm +$ cd vjm +$ ls +263137 npj +$ cd .. +$ cd .. +$ cd rtv +$ ls +dir lhjmzsl +dir pcwsn +dir qsgv +dir vvzbsg +$ cd lhjmzsl +$ ls +dir qzsjhsfn +$ cd qzsjhsfn +$ ls +26909 mbgfn.pmh +$ cd .. +$ cd .. +$ cd pcwsn +$ ls +160033 ftz.nbg +145165 wgdjsgsb.hhm +$ cd .. +$ cd qsgv +$ ls +dir jqm +136209 mbgfn.pmh +79664 mghdlnh.chv +$ cd jqm +$ ls +222831 npj +$ cd .. +$ cd .. +$ cd vvzbsg +$ ls +84246 dpbwg.tsq +$ cd .. +$ cd .. +$ cd vnvft +$ ls +dir crqrl +dir scdvzsd +$ cd crqrl +$ ls +13185 dpbwg.twz +109021 qcdqnhg.jjj +$ cd .. +$ cd scdvzsd +$ ls +dir hmcj +$ cd hmcj +$ ls +252155 npj +$ cd .. +$ cd .. +$ cd .. +$ cd vqcvbncp +$ ls +72128 crnhb +dir ddz +dir dvhzvdn +dir ffsbcmg +dir gfs +dir jghlb +dir llq +dir pvn +dir rvqmmjgr +$ cd ddz +$ ls +232302 gdfqpnj.nhf +274940 lhjmzsl.mzv +286217 mpbb +$ cd .. +$ cd dvhzvdn +$ ls +273183 cfnqn.qpz +$ cd .. +$ cd ffsbcmg +$ ls +6544 mtctcdgd.nmb +52511 wndzt.lqb +$ cd .. +$ cd gfs +$ ls +228312 mwvbpw.tdj +dir rqjrfzgt +62100 shc.rtj +313984 szsplzq.dpd +dir tqtnp +$ cd rqjrfzgt +$ ls +296460 bvp +272324 mbgfn.pmh +221044 npj +110232 qvcszzv +$ cd .. +$ cd tqtnp +$ ls +98376 tzwh +212247 wzjj.scp +$ cd .. +$ cd .. +$ cd jghlb +$ ls +dir ddrmsv +$ cd ddrmsv +$ ls +252143 mbgfn.pmh +$ cd .. +$ cd .. +$ cd llq +$ ls +dir lhjmzsl +67341 mbgfn.pmh +155159 npj +dir qsgv +dir vcvnzc +$ cd lhjmzsl +$ ls +151930 ssldrsf.mzm +$ cd .. +$ cd qsgv +$ ls +233702 ztdmtqb.mhf +$ cd .. +$ cd vcvnzc +$ ls +168179 lhjmzsl.cwf +$ cd .. +$ cd .. +$ cd pvn +$ ls +240160 lbd.slf +$ cd .. +$ cd rvqmmjgr +$ ls +24809 gwgcrzp.zhj +dir hmcj +dir lhjmzsl +dir mwvbpw +121997 nbmf +77886 ntp +dir qcqrgdm +$ cd hmcj +$ ls +126073 dpbwg.lhh +dir jchccghv +dir lhjmzsl +144075 qsgv.lbw +288076 swrngbsq.qzw +$ cd jchccghv +$ ls +310277 mpbb +$ cd .. +$ cd lhjmzsl +$ ls +193576 bwsz.crv +16002 mbgfn.pmh +57961 mtctcdgd.nmb +$ cd .. +$ cd .. +$ cd lhjmzsl +$ ls +dir bsfwmcz +dir dpbwg +121640 mwvbpw +dir pmrfvzw +dir qsgv +$ cd bsfwmcz +$ ls +dir fjqqblz +51952 gqbj.fwd +dir hmcj +dir lctt +81316 lhjmzsl.qqr +6372 mbgfn.pmh +22669 shvgmwn.vhv +130990 zgq.fpv +$ cd fjqqblz +$ ls +26064 zrqrnb.nwf +$ cd .. +$ cd hmcj +$ ls +14385 mwvbpw.vzq +$ cd .. +$ cd lctt +$ ls +137397 fbgbs.fjf +dir jgbzqzq +dir qrr +272639 qsgv.vwm +$ cd jgbzqzq +$ ls +135320 mpbb +$ cd .. +$ cd qrr +$ ls +dir dpbwg +dir mwvbpw +54143 pnsl.bbm +dir wbpzpg +$ cd dpbwg +$ ls +250653 shvgmwn.vhv +$ cd .. +$ cd mwvbpw +$ ls +2682 fhdh.hmt +$ cd .. +$ cd wbpzpg +$ ls +dir bqshqpvm +141398 mtctcdgd.nmb +130729 vtnfmjqm.mgp +$ cd bqshqpvm +$ ls +252449 shvgmwn.vhv +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd dpbwg +$ ls +82216 gwjwmcbf.gwl +$ cd .. +$ cd pmrfvzw +$ ls +dir mwvbpw +dir wtzgjspz +dir zmb +$ cd mwvbpw +$ ls +16736 wbzjzz.mqq +$ cd .. +$ cd wtzgjspz +$ ls +dir vjb +$ cd vjb +$ ls +301763 drchphtm +$ cd .. +$ cd .. +$ cd zmb +$ ls +201978 mdj +dir qgnffw +147889 qzbmwpnm.gjp +$ cd qgnffw +$ ls +17740 cwz.dhs +222345 mbgfn.pmh +81554 npj +206597 tsnlnzh +$ cd .. +$ cd .. +$ cd .. +$ cd qsgv +$ ls +dir mmsnzr +$ cd mmsnzr +$ ls +160502 tgrztm +$ cd .. +$ cd .. +$ cd .. +$ cd mwvbpw +$ ls +40049 qmmdv +313418 qsgv.srp +$ cd .. +$ cd qcqrgdm +$ ls +202848 wtl.qbj +$ cd .. +$ cd .. +$ cd .. +$ cd vsgjds +$ ls +138690 npj +22984 tpfbnz.sgj +$ cd .. +$ cd .. +$ cd sqs +$ ls +dir zms +$ cd zms +$ ls +152096 cvtqph.wwp +dir mmwzg +$ cd mmwzg +$ ls +100870 qsgv diff --git a/2022/07/part1.js b/2022/07/part1.js new file mode 100644 index 0000000..ad3ef03 --- /dev/null +++ b/2022/07/part1.js @@ -0,0 +1,68 @@ +import * as R from "remeda"; +import { readFile } from "../05/part1.js"; +import path from "path"; + +const readInput = (...args) => + readFile(...args) + .split("\n") + .filter((x) => x); + +export const puzzleInput = readInput(import.meta.url, "input"); +export const sample = readInput(import.meta.url, "sample"); + +export function parseInput(lines) { + lines = [...lines]; + + let currentDir = "/"; + + const data = {}; + + while (lines.length > 0) { + const line = lines.shift(); + + let match = line.match(/\$ cd (.*)/); + if (match) { + currentDir = match[1].startsWith("/") + ? match[1] + : path.join(currentDir, match[1]); + if (!data[currentDir]) data[currentDir] = 0; + continue; + } + + if (/^\d+/.test(line)) { + const [size] = line.match(/\d+/); + data[currentDir] += parseInt(size); + } + } + + return data; +} + +export function cumulative(individual) { + const isIn = (k, targetKey) => { + const result = + k === targetKey || + k.startsWith(targetKey.endsWith("/") ? targetKey : targetKey + "/"); + return result; + }; + + return R.pipe( + individual, + R.mapValues((v, targetKey) => { + const sum = R.sumBy( + Object.values(R.pickBy(individual, (v, k) => isIn(k, targetKey))), + R.identity + ); + return sum; + } + ) + ); +} + +export default R.createPipe( + parseInput, + cumulative, + R.pickBy((v) => v <= 100000), + R.values, + R.sumBy(R.identity) +); diff --git a/2022/07/part2.js b/2022/07/part2.js new file mode 100644 index 0000000..c9f337c --- /dev/null +++ b/2022/07/part2.js @@ -0,0 +1,25 @@ +import * as R from "remeda"; + +import { +cumulative, parseInput +} from './part1.js'; + + +export default (lines) => { + let cumu = R.pipe( lines, parseInput, cumulative); + + console.log(cumu); + + const spaceNeeded = 30000000 - 70000000 + cumu['/']; + + cumu = R.pickBy( + cumu, (v) => v >= spaceNeeded + ); + + return R.pipe( + R.keys(cumu), + R.sortBy( k => cumu[k] ), + R.first, + (k => cumu[k]) + ) +} diff --git a/2022/07/puzzle.md b/2022/07/puzzle.md new file mode 100644 index 0000000..e89e6f0 --- /dev/null +++ b/2022/07/puzzle.md @@ -0,0 +1,97 @@ +## \--- 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?_ + +To begin, [get your puzzle input](7/input). + +Answer: + +You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=%22No+Space+Left+On+Device%22+%2D+Day+7+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F7&related=ericwastl&hashtags=AdventOfCode) [Mastodon]()] this puzzle. diff --git a/2022/07/sample b/2022/07/sample new file mode 100644 index 0000000..09a921e --- /dev/null +++ b/2022/07/sample @@ -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 diff --git a/2022/07/test.js b/2022/07/test.js new file mode 100644 index 0000000..66637fb --- /dev/null +++ b/2022/07/test.js @@ -0,0 +1,36 @@ +import { test, expect, describe } from "vitest"; + +import { expectSolution } from "../01/main.js"; +import part1, { puzzleInput, sample, parseInput,cumulative } from "./part1.js"; +import part2 from "./part2.js"; + +describe("part 1", () => { + const expected = { '/': 23352670, '/a': 94269, '/a/e': 584, '/d': 24933642 }; + + test( "parseInput", () => { + const data = parseInput(sample); + + + expect(data).toEqual(expected); + }); + + test( "cumulative", () => { + + expect(cumulative(expected)).toEqual({ '/':48381165 , '/a': 94853, '/a/e': 584, '/d': 24933642 }); + }); + + + test("solution", () => { + expectSolution(part1(puzzleInput)).toEqual(1350966); + }); +}); + +describe("part 2", () => { + test("sample", () => { + expect(part2(sample)).toEqual(24933642); + }); + test("solution", () => { + expectSolution(part2(puzzleInput)).toBeLessThan(46647330); + expectSolution(part2(puzzleInput)).toEqual(6296435); + }); +});