diff --git a/day07/README.md b/day07/README.md new file mode 100644 index 0000000..b32319b --- /dev/null +++ b/day07/README.md @@ -0,0 +1,7 @@ +Run with command: + +``` +mvn package && java -cp target/aoc07-1.0-SNAPSHOT.jar com.felixalb.app.App input.txt +``` + +Code is located in `src/main/java/com/felixalb/app/` diff --git a/day07/input.txt b/day07/input.txt new file mode 100644 index 0000000..59838bc --- /dev/null +++ b/day07/input.txt @@ -0,0 +1,1032 @@ +$ cd / +$ ls +dir bfqzjjct +dir cgcqpjpn +dir frrjh +dir jjl +293559 jztrccm.hvd +dir mqvn +$ cd bfqzjjct +$ ls +dir cgcqpjpn +dir frrjh +dir gzpbchq +12679 lzrgqrgc +dir mzm +dir phslrcw +240839 phslrcw.ljl +dir qftq +dir qlpg +169962 vntqgq.tps +114950 vzq.qvv +$ cd cgcqpjpn +$ ls +305598 hjrv +134990 jlqcf.sjc +dir phslrcw +dir qsdlvr +129733 rznmd.snf +dir sgcg +dir swfsblvv +dir vprjf +dir wchtzbdv +dir wfvj +229649 zdm.rpl +$ cd phslrcw +$ ls +dir jnzjq +dir lfd +79014 lzrgqrgc.sqh +209222 wfnmqrtr.zbq +$ cd jnzjq +$ ls +163133 qsdcrvz.zmn +287008 zdm.rpl +$ cd .. +$ cd lfd +$ ls +4442 cgcqpjpn +61281 jmr +212696 jztrccm.hvd +dir mlt +dir qsdlvr +$ cd mlt +$ ls +183050 zdm.rpl +$ cd .. +$ cd qsdlvr +$ ls +dir nzjfwdt +dir qsdlvr +$ cd nzjfwdt +$ ls +49044 jdqz.bqd +$ cd .. +$ cd qsdlvr +$ ls +13838 fpfmhq.rbp +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +167042 jdqz.bqd +132914 lzrgqrgc +$ cd .. +$ cd sgcg +$ ls +dir bjwzfr +111567 jdqz.bqd +33253 jztrccm.hvd +75760 qsdlvr +$ cd bjwzfr +$ ls +268763 npjd.glz +dir phslrcw +121486 vbfsrwf.mbc +dir vtwtmnvl +$ cd phslrcw +$ ls +114641 cgsrlq +235130 npjd.glz +$ cd .. +$ cd vtwtmnvl +$ ls +7771 hnrpgsn +290862 rtrljz.vbf +220102 rztn.vqq +$ cd .. +$ cd .. +$ cd .. +$ cd swfsblvv +$ ls +231282 vclmcf.vpq +16727 vjhmb.ftn +$ cd .. +$ cd vprjf +$ ls +dir fppsf +56173 npjd.glz +dir phslrcw +dir pwjdcdff +dir qsdlvr +dir qvh +dir sbmtlz +$ cd fppsf +$ ls +dir hltnf +dir qrmggjs +dir qsdlvr +$ cd hltnf +$ ls +201448 npjd.glz +23616 zdm.rpl +$ cd .. +$ cd qrmggjs +$ ls +dir blhwqwd +dir szf +$ cd blhwqwd +$ ls +142887 vzq.qvv +$ cd .. +$ cd szf +$ ls +225491 tvsf.tpv +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +92209 brmrwznr +dir frrjh +212979 qsdlvr.bwh +dir vpvnb +$ cd frrjh +$ ls +143811 qvwmpj.gnc +$ cd .. +$ cd vpvnb +$ ls +dir wwsw +$ cd wwsw +$ ls +13671 qsdlvr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +dir bpfhc +dir fjvgwbj +127950 hjngrw.rtr +37045 lfpjb.ftz +dir qsn +$ cd bpfhc +$ ls +232351 frrjh.szf +14824 jztrccm.hvd +162270 vzq.qvv +$ cd .. +$ cd fjvgwbj +$ ls +dir cgcqpjpn +4290 jrfsmghw.vqr +dir lqnpdd +$ cd cgcqpjpn +$ ls +208104 btrcq.vpn +149676 bwvp.jlf +76056 gvvrvqdf.zdc +272686 pqhtjpv +dir zjntbw +$ cd zjntbw +$ ls +140092 jdqz.bqd +$ cd .. +$ cd .. +$ cd lqnpdd +$ ls +dir brf +197899 jdqz.bqd +dir phslrcw +236823 qcvgnlv.ngn +dir szrfcwtw +88974 vzq.qvv +$ cd brf +$ ls +112059 jdqz.bqd +217412 vzq.qvv +$ cd .. +$ cd phslrcw +$ ls +84285 jnzf +282660 zdm.rpl +$ cd .. +$ cd szrfcwtw +$ ls +84415 gcp +163787 wffb.wbq +$ cd .. +$ cd .. +$ cd .. +$ cd qsn +$ ls +139676 zdm.rpl +$ cd .. +$ cd .. +$ cd pwjdcdff +$ ls +dir cgcqpjpn +54163 lzrgqrgc +dir qsdlvr +51246 qsdlvr.cmq +5082 rvmv.tqj +112712 vzq.qvv +$ cd cgcqpjpn +$ ls +92895 dmf.bsv +dir lhqzc +$ cd lhqzc +$ ls +13443 hwlsmmjn.wpj +232141 tlsn +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +216144 dghbz.lnl +241311 jztrccm.hvd +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +109764 qsdlvr.tbj +$ cd .. +$ cd qvh +$ ls +dir frrjh +51428 hdtmm.zzl +$ cd frrjh +$ ls +271661 brff.mgp +$ cd .. +$ cd .. +$ cd sbmtlz +$ ls +277255 jztrccm.hvd +$ cd .. +$ cd .. +$ cd wchtzbdv +$ ls +198310 cgcqpjpn +300478 fcfvgpdr.lzv +145860 jdqz.bqd +20145 mggg +$ cd .. +$ cd wfvj +$ ls +270522 phslrcw.mwt +213919 tczvr +19226 zdm.rpl +$ cd .. +$ cd .. +$ cd frrjh +$ ls +dir cgcqpjpn +246391 hnc.bzl +dir lzrgqrgc +dir mnrbzwr +239337 npjd.glz +dir qsdlvr +$ cd cgcqpjpn +$ ls +2423 pflhrgjg.gch +$ cd .. +$ cd lzrgqrgc +$ ls +265708 vzq.qvv +107406 zwnjb +$ cd .. +$ cd mnrbzwr +$ ls +287289 jgs.bdb +$ cd .. +$ cd qsdlvr +$ ls +dir qsdlvr +$ cd qsdlvr +$ ls +71204 gnfz +$ cd .. +$ cd .. +$ cd .. +$ cd gzpbchq +$ ls +dir cmqgph +dir fwf +146975 jztrccm.hvd +284515 lzrgqrgc.qsf +27455 qbsbvscc +$ cd cmqgph +$ ls +177740 zdm.rpl +$ cd .. +$ cd fwf +$ ls +210658 dnmvdgwn.wzt +22013 rqcspw.qmf +244013 zdm.rpl +$ cd .. +$ cd .. +$ cd mzm +$ ls +262805 gnfsbdcd.tvp +$ cd .. +$ cd phslrcw +$ ls +74949 wncqjc.tzd +$ cd .. +$ cd qftq +$ ls +dir jrds +$ cd jrds +$ ls +dir dvht +dir qsdlvr +$ cd dvht +$ ls +57354 phslrcw +57174 qsdlvr.ftm +$ cd .. +$ cd qsdlvr +$ ls +132030 fbsmfspp +$ cd .. +$ cd .. +$ cd .. +$ cd qlpg +$ ls +dir cgcqpjpn +dir frrjh +dir jjsfmg +186178 jmvcvw +dir qgjhlnmf +100234 qltp.vdr +dir tbwt +dir vjjvfbdw +dir wjz +162428 zlbnsg +$ cd cgcqpjpn +$ ls +dir gvmgcftp +dir hsgvgc +dir nps +dir phslrcw +dir scdglb +$ cd gvmgcftp +$ ls +31869 dsddwbbp +dir fbsrjgzj +53280 tzn.fvp +$ cd fbsrjgzj +$ ls +89267 qsdlvr.lhs +$ cd .. +$ cd .. +$ cd hsgvgc +$ ls +dir lzrgqrgc +210774 wpspf.vrd +$ cd lzrgqrgc +$ ls +289086 gfhwwslq.tlz +$ cd .. +$ cd .. +$ cd nps +$ ls +74713 fnqzcr +dir srjvfq +94736 wbrcn.nng +dir wmddln +dir zsgnrmcc +$ cd srjvfq +$ ls +dir grqft +dir pjnjlf +254618 wmfnmfrc.wng +$ cd grqft +$ ls +3742 zmpzwh.nvz +$ cd .. +$ cd pjnjlf +$ ls +dir mlrsrs +$ cd mlrsrs +$ ls +167149 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd wmddln +$ ls +222831 cgcqpjpn.dcv +140188 gsvngqp +174466 sfngrqgj.gvz +$ cd .. +$ cd zsgnrmcc +$ ls +143043 vzq.qvv +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +dir cdfsrvnc +103523 hzsjc +239089 jvwdjbq +229835 npjd.glz +189778 wwcccwmb.bdp +$ cd cdfsrvnc +$ ls +dir vzsvcnlz +$ cd vzsvcnlz +$ ls +dir wwn +$ cd wwn +$ ls +60279 frngbwm.wwf +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd scdglb +$ ls +176557 vzq.qvv +$ cd .. +$ cd .. +$ cd frrjh +$ ls +130811 jdqz.bqd +$ cd .. +$ cd jjsfmg +$ ls +dir nffq +22842 vzt.nwh +$ cd nffq +$ ls +dir wmq +$ cd wmq +$ ls +196322 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd qgjhlnmf +$ ls +dir jhqqjt +dir phslrcw +246296 pzvmcq +219308 tlhqgtcl.twb +dir vfgb +300258 zdm.rpl +221663 zsqh.dph +$ cd jhqqjt +$ ls +90068 fctpjdlm.ggg +dir frrjh +dir ngmzbqb +dir phslrcw +64341 qsdlvr.pnq +dir vlgpcg +$ cd frrjh +$ ls +49247 qrrhdhld.sjr +$ cd .. +$ cd ngmzbqb +$ ls +247800 cgcqpjpn.wgv +$ cd .. +$ cd phslrcw +$ ls +dir qsdlvr +dir vjrrrm +$ cd qsdlvr +$ ls +255302 ddvc +182817 dzd.trp +$ cd .. +$ cd vjrrrm +$ ls +38983 hjbsqd.pdl +$ cd .. +$ cd .. +$ cd vlgpcg +$ ls +dir frrjh +83387 jztrccm.hvd +$ cd frrjh +$ ls +dir rmlc +$ cd rmlc +$ ls +273584 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +96286 jztrccm.hvd +224425 npjd.glz +$ cd .. +$ cd vfgb +$ ls +dir fgl +dir fllsgwjt +dir frrjh +dir qsdlvr +$ cd fgl +$ ls +61972 lzrgqrgc.qrs +$ cd .. +$ cd fllsgwjt +$ ls +dir fqchdcpd +214477 zcr.spz +$ cd fqchdcpd +$ ls +156000 jccqlw.twl +$ cd .. +$ cd .. +$ cd frrjh +$ ls +210987 zdm.rpl +$ cd .. +$ cd qsdlvr +$ ls +dir phslrcw +dir znnsfvz +$ cd phslrcw +$ ls +214655 ddw +$ cd .. +$ cd znnsfvz +$ ls +126028 zpq.bgs +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd tbwt +$ ls +dir ltjjhr +$ cd ltjjhr +$ ls +158069 vplwrz.rzp +$ cd .. +$ cd .. +$ cd vjjvfbdw +$ ls +277114 fmndb.rth +241524 frrjh +212152 frrjh.dlq +61370 jztrccm.hvd +dir srvqm +237264 znt +$ cd srvqm +$ ls +dir fbzmddc +61958 jdqz.bqd +dir phslrcw +173063 vwvmhwzn.tvg +$ cd fbzmddc +$ ls +dir ccvtbpzw +201647 lzrgqrgc.sjs +192449 rhfgcgz +224557 sbcpcbpd +$ cd ccvtbpzw +$ ls +dir phslrcw +dir prpllzcn +$ cd phslrcw +$ ls +290780 lbl.qmm +$ cd .. +$ cd prpllzcn +$ ls +242024 bhq +118347 frrjh.tsz +dir hcqs +$ cd hcqs +$ ls +104706 csnllvcf +270940 npjd.glz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +244909 czfjsrns.fdm +dir frrjh +68252 lzrgqrgc +146640 npjd.glz +290259 pbzpthgs.bgf +99657 rmfpfmd +dir zllf +$ cd frrjh +$ ls +dir cgcqpjpn +65295 jdqz.bqd +dir phslrcw +$ cd cgcqpjpn +$ ls +dir mwfwrd +$ cd mwfwrd +$ ls +26481 sngtgc +dir zfzddl +$ cd zfzddl +$ ls +47854 fzfdrmbb.ngm +$ cd .. +$ cd .. +$ cd .. +$ cd phslrcw +$ ls +251021 fwfpd.vrt +230149 fwjznjtz +140480 gbsmhj +112221 lzrgqrgc.gcr +139564 npjd.glz +dir pvmvm +dir tpzr +$ cd pvmvm +$ ls +73785 tnbgmgt.wqg +$ cd .. +$ cd tpzr +$ ls +16759 tmcmp +$ cd .. +$ cd .. +$ cd .. +$ cd zllf +$ ls +281402 dzrsnzh.smb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd wjz +$ ls +266826 cgcqpjpn.spr +dir frvbfjj +111126 jdqz.bqd +257951 pbn.gdc +dir vjs +dir zrnvnb +$ cd frvbfjj +$ ls +257181 glfdplrm +$ cd .. +$ cd vjs +$ ls +dir jmpbwhtj +159996 lzrgqrgc.zcb +192135 phw +dir tdfz +dir wtm +$ cd jmpbwhtj +$ ls +dir fln +dir frrjh +dir nrtcmj +$ cd fln +$ ls +284574 frrjh.scb +$ cd .. +$ cd frrjh +$ ls +10020 npjd.glz +295395 vzq.qvv +$ cd .. +$ cd nrtcmj +$ ls +dir htvm +$ cd htvm +$ ls +dir lwh +$ cd lwh +$ ls +162494 npjd.glz +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd tdfz +$ ls +dir gtvlbhhw +dir hvssbj +dir phslrcw +$ cd gtvlbhhw +$ ls +172627 sjvcfz +dir tfnhsrc +76460 vvgcv +$ cd tfnhsrc +$ ls +dir hjrjmrwc +$ cd hjrjmrwc +$ ls +41459 jztrccm.hvd +$ cd .. +$ cd .. +$ cd .. +$ cd hvssbj +$ ls +64052 phslrcw +$ cd .. +$ cd phslrcw +$ ls +dir wtdpf +dir zctdqwbc +$ cd wtdpf +$ ls +75802 jztrccm.hvd +$ cd .. +$ cd zctdqwbc +$ ls +127648 vqzcf.wbl +$ cd .. +$ cd .. +$ cd .. +$ cd wtm +$ ls +265433 jztrccm.hvd +20952 mplbrzlp +123701 nwr.tjg +58492 qsdlvr.qzq +$ cd .. +$ cd .. +$ cd zrnvnb +$ ls +228205 ghllsc.fqn +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd cgcqpjpn +$ ls +dir cmnsbsqp +dir hzsn +221975 jdqz.bqd +177439 jztrccm.hvd +dir mqjrcw +dir tdp +211065 zdm.rpl +$ cd cmnsbsqp +$ ls +282903 qzlc +$ cd .. +$ cd hzsn +$ ls +221345 wtdnp.grd +$ cd .. +$ cd mqjrcw +$ ls +156244 gznmpr.dlr +45213 hdb.fjb +202993 pdt +dir znvdz +$ cd znvdz +$ ls +183973 jztrccm.hvd +$ cd .. +$ cd .. +$ cd tdp +$ ls +68564 nmntwl +259117 vzq.qvv +$ cd .. +$ cd .. +$ cd frrjh +$ ls +276034 lzrgqrgc.fgq +dir psqczq +dir qsdlvr +dir snrw +$ cd psqczq +$ ls +158415 jdqz.bqd +127684 lhctqnnz.hgn +123191 phslrcw.prl +dir wrftp +$ cd wrftp +$ ls +234327 qdbbl.czd +$ cd .. +$ cd .. +$ cd qsdlvr +$ ls +179591 chbns.sqf +88327 jdqz.bqd +dir mrf +$ cd mrf +$ ls +229849 phslrcw.rdr +$ cd .. +$ cd .. +$ cd snrw +$ ls +dir cbvr +dir lbbrtc +134786 lzrgqrgc.vrt +131987 vzq.qvv +$ cd cbvr +$ ls +120330 htmgqj.nbq +162963 lzrgqrgc.rwq +$ cd .. +$ cd lbbrtc +$ ls +dir tnqdj +$ cd tnqdj +$ ls +72261 lhqnwns.nhj +235520 qsdlvr.dtr +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd jjl +$ ls +dir fcfpptw +dir frrjh +dir gmdwgs +221258 jdqz.bqd +dir lzrgqrgc +dir mwnmjjj +297139 smjtpl.zzs +$ cd fcfpptw +$ ls +167264 frrjh +101587 hsmhhc.lvb +$ cd .. +$ cd frrjh +$ ls +268280 jztrccm.hvd +127939 nbqgvd +247875 nmnrsqq +dir phslrcw +dir rmgwbq +288264 znrrdmw +$ cd phslrcw +$ ls +dir cgcqpjpn +dir nbwjw +$ cd cgcqpjpn +$ ls +249503 phslrcw.dmf +$ cd .. +$ cd nbwjw +$ ls +dir phslrcw +$ cd phslrcw +$ ls +193336 sqpgwtgp.fpb +$ cd .. +$ cd .. +$ cd .. +$ cd rmgwbq +$ ls +90116 jztrccm.hvd +303005 phslrcw +$ cd .. +$ cd .. +$ cd gmdwgs +$ ls +dir lzrgqrgc +dir mwrj +dir mzqn +104570 qsqqbfg.njt +dir zmj +$ cd lzrgqrgc +$ ls +dir cmn +dir qsdlvr +dir rtz +$ cd cmn +$ ls +20043 bgm.tvn +41301 hqdl.rnr +32026 tsmmr.bfl +$ cd .. +$ cd qsdlvr +$ ls +dir dcsdmqt +$ cd dcsdmqt +$ ls +107938 zdm.rpl +$ cd .. +$ cd .. +$ cd rtz +$ ls +308963 jbwvpjvj.fgn +31573 jdqz.bqd +97775 phslrcw +dir qsdlvr +225929 wwznl.bpd +$ cd qsdlvr +$ ls +52056 bddqnlmg.hjm +236614 qsdlvr.vcz +$ cd .. +$ cd .. +$ cd .. +$ cd mwrj +$ ls +dir dfsm +dir mtfqs +$ cd dfsm +$ ls +32797 jztrccm.hvd +dir mthjq +241875 vzq.qvv +$ cd mthjq +$ ls +147247 zdm.rpl +$ cd .. +$ cd .. +$ cd mtfqs +$ ls +dir crpfs +dir phslrcw +$ cd crpfs +$ ls +24073 jdqz.bqd +269270 jztrccm.hvd +89807 lzrgqrgc.ddj +$ cd .. +$ cd phslrcw +$ ls +124727 cgcqpjpn.zfz +dir gttptlwj +dir phslrcw +dir qsdlvr +$ cd gttptlwj +$ ls +295546 frrjh.tmq +$ cd .. +$ cd phslrcw +$ ls +234122 zdm.rpl +$ cd .. +$ cd qsdlvr +$ ls +131491 zdm.rpl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd mzqn +$ ls +16352 qjllmbh.jfm +36853 zdm.rpl +$ cd .. +$ cd zmj +$ ls +dir dvrwbrbt +dir frrjh +217060 mftczjfs.zch +dir znhsrsfb +$ cd dvrwbrbt +$ ls +dir lzssrrq +$ cd lzssrrq +$ ls +dir qsdlvr +$ cd qsdlvr +$ ls +108975 npjd.glz +$ cd .. +$ cd .. +$ cd .. +$ cd frrjh +$ ls +dir phslrcw +dir zjstpvl +$ cd phslrcw +$ ls +244682 pgn.jqm +$ cd .. +$ cd zjstpvl +$ ls +300736 bsw.wzn +102742 jdqz.bqd +$ cd .. +$ cd .. +$ cd znhsrsfb +$ ls +121093 qsdlvr +$ cd .. +$ cd .. +$ cd .. +$ cd lzrgqrgc +$ ls +50942 jdqz.bqd +26201 ljqgqqrr +71462 lrrfvs.ghl +274208 tqhcl.hcl +$ cd .. +$ cd mwnmjjj +$ ls +184865 mzwmp.jbw +68016 thbh.nbg +$ cd .. +$ cd .. +$ cd mqvn +$ ls +287623 spfzctll diff --git a/day07/input_demo.txt b/day07/input_demo.txt new file mode 100644 index 0000000..09a921e --- /dev/null +++ b/day07/input_demo.txt @@ -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/day07/pom.xml b/day07/pom.xml new file mode 100644 index 0000000..7784afb --- /dev/null +++ b/day07/pom.xml @@ -0,0 +1,75 @@ + + + + 4.0.0 + + com.felixalb.app + aoc07 + 1.0-SNAPSHOT + + aoc07 + + http://www.example.com + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + test + + + + + + + + + maven-clean-plugin + 3.1.0 + + + + maven-resources-plugin + 3.0.2 + + + maven-compiler-plugin + 3.8.0 + + + maven-surefire-plugin + 2.22.1 + + + maven-jar-plugin + 3.0.2 + + + maven-install-plugin + 2.5.2 + + + maven-deploy-plugin + 2.8.2 + + + + maven-site-plugin + 3.7.1 + + + maven-project-info-reports-plugin + 3.0.0 + + + + + diff --git a/day07/src/main/java/com/felixalb/app/App.java b/day07/src/main/java/com/felixalb/app/App.java new file mode 100644 index 0000000..4a372b7 --- /dev/null +++ b/day07/src/main/java/com/felixalb/app/App.java @@ -0,0 +1,46 @@ +package com.felixalb.app; + +import java.io.*; +import java.util.*; +import java.nio.file.*; + + +public class App { + private static String[] readLines(String filename) { + try { + return Files.readAllLines(Paths.get(filename)).toArray(new String[0]); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + if (args.length < 1) { + System.out.println("Usage: java App "); + return; + } + + String filename = args[0]; + String[] lines = readLines(filename); + + System.out.println("Lines: " + lines.length); + + FileSystemReader fileSystemReader = new FileSystemReader(lines); + + + // Part 1 + FileNode p1_root = fileSystemReader.getRoot(); + Integer directorySizeSum = 0; + ArrayList p1_sizes = p1_root.getDirectorySizesFlat(new ArrayList()); + // System.out.println("Part 1: " + p1_sizes); + for (Integer size : p1_sizes) { + if (size <= 100000) { + directorySizeSum += size; + } + } + p1_root.printNode(""); + System.out.println("Part 1: " + directorySizeSum); + + + } +} diff --git a/day07/src/main/java/com/felixalb/app/FileNode.java b/day07/src/main/java/com/felixalb/app/FileNode.java new file mode 100644 index 0000000..6055890 --- /dev/null +++ b/day07/src/main/java/com/felixalb/app/FileNode.java @@ -0,0 +1,70 @@ +package com.felixalb.app; + +import java.util.ArrayList; + +class FileNode { + private String name; + private Integer size; + private Boolean isDirectory; + private ArrayList children = new ArrayList(); + + public FileNode(String name, Boolean isDirectory) { + this.name = name; + this.children = new ArrayList(); + this.isDirectory = isDirectory; + } + + public Integer getSize() { + if (this.isDirectory) { + Integer size = 0; + for (FileNode child : this.children) { + size += child.getSize(); + } + return size; + } else { + return this.size; + } + } + + public void setSize(Integer size) { + if (this.isDirectory) { + throw new RuntimeException("Cannot set size of directory"); + } + this.size = size; + } + + public String getName() { + return name; + } + + public ArrayList getChildren() { + if (!this.isDirectory) { + throw new RuntimeException("Cannot get children of file"); + } + return this.children; + } + + public void appendChild(FileNode child) { + this.children.add(child); + } + + public void printNode(String prefix) { + System.out.println(prefix + this.name + " (" + this.getSize() + ")"); + if (this.isDirectory) { + for (FileNode child : this.children) { + child.printNode(prefix + "| "); + } + } + } + + public ArrayList getDirectorySizesFlat(ArrayList sizes) { + if (!this.isDirectory) { + return sizes; + } + sizes.add(this.getSize()); + for (FileNode child : this.children) { + child.getDirectorySizesFlat(sizes); + } + return sizes; + } +} diff --git a/day07/src/main/java/com/felixalb/app/FileSystemReader.java b/day07/src/main/java/com/felixalb/app/FileSystemReader.java new file mode 100644 index 0000000..c4ceead --- /dev/null +++ b/day07/src/main/java/com/felixalb/app/FileSystemReader.java @@ -0,0 +1,116 @@ +package com.felixalb.app; + +import java.util.ArrayList; + +public class FileSystemReader { + ArrayList commands = new ArrayList(); + ArrayList workingDirectory = new ArrayList(); + FileNode root = new FileNode("/", true); + + public FileSystemReader(String[] lines) { + ArrayList currentCommand = new ArrayList(); + for (String line : lines) { + if (line.startsWith("$") && currentCommand.size() > 0) { + commands.add(currentCommand.toArray(new String[0])); + currentCommand = new ArrayList(); + } + + currentCommand.add(line); + } + if (currentCommand.size() > 0) { + commands.add(currentCommand.toArray(new String[0])); + } + + System.out.println("Commands: " + commands.size()); + + for (String[] command : commands) { + this.parseCommand(command); + } + } + + public void printState() { + System.out.println("Working directory: " + this.workingDirectory); + this.root.printNode(""); + } + + public FileNode getRoot() { + return this.root; + } + + private FileNode getNode(String path, Boolean create) { + String[] pathParts = path.split("/"); + FileNode currentNode = root; + for (String pathPart : pathParts) { + if (pathPart.length() == 0) { + continue; + } + ArrayList children = currentNode.getChildren(); + FileNode child = null; + for (FileNode childCandidate : children) { + if (childCandidate.getName().equals(pathPart)) { + child = childCandidate; + break; + } + } + + if (child == null) { + if (create) { + child = new FileNode(pathPart, false); + currentNode.appendChild(child); + } else { + return null; + } + } + currentNode = child; + } + return currentNode; + } + + public ArrayList getCommands() { + return commands; + } + + public void parseCommand(String[] command) { + String[] commandParts = command[0].split(" "); + String commandName = commandParts[1]; + // System.out.println("Command: " + commandName); + + if (commandName.equals("cd")) { + String path = commandParts[2]; + switch (path) { + case "/" : + workingDirectory = new ArrayList(); + break; + case ".." : + workingDirectory.remove(workingDirectory.size() - 1); + break; + default : + workingDirectory.add(path); + break; + } + // System.out.println("Working directory: /" + String.join("/", workingDirectory)); + } else if (commandName.equals("ls")) { + FileNode dir = getNode(String.join("/", workingDirectory), false); + if (dir == null) { + System.out.println("ls: cannot access " + String.join("/", workingDirectory) + ": No such file or directory"); + } else { + for (String line : command) { + String[] lineParts = line.split(" "); + if (lineParts[0].equals("$")) { + continue; + } + if (lineParts[0].equals("dir")) { + FileNode newDir = new FileNode(lineParts[1], true); + dir.appendChild(newDir); + } else { + FileNode newFile = new FileNode(lineParts[1], false); + newFile.setSize(Integer.parseInt(lineParts[0])); + dir.appendChild(newFile); + } + } + } + } else { + throw new RuntimeException("Unknown command: '" + commandName + "'"); + } + } +} diff --git a/day07/src/test/java/com/felixalb/app/AppTest.java b/day07/src/test/java/com/felixalb/app/AppTest.java new file mode 100644 index 0000000..9c7fc71 --- /dev/null +++ b/day07/src/test/java/com/felixalb/app/AppTest.java @@ -0,0 +1,20 @@ +package com.felixalb.app; + +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +/** + * Unit test for simple App. + */ +public class AppTest +{ + /** + * Rigorous Test :-) + */ + @Test + public void shouldAnswerWithTrue() + { + assertTrue( true ); + } +}