From 7a2d757d601b7e3ee367741dc0fc4740e6f033bd Mon Sep 17 00:00:00 2001 From: Guillaume Dott Date: Thu, 8 Dec 2022 15:03:36 +0100 Subject: [PATCH] Resolve day 7 and 8 --- 07/input | 1017 ++++++++++++++++++++++++++++++++++++++++++++++++++ 07/script.rb | 72 ++++ 08/input | 99 +++++ 08/script.rb | 56 +++ 4 files changed, 1244 insertions(+) create mode 100644 07/input create mode 100755 07/script.rb create mode 100644 08/input create mode 100755 08/script.rb diff --git a/07/input b/07/input new file mode 100644 index 0000000..0d274fd --- /dev/null +++ b/07/input @@ -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 diff --git a/07/script.rb b/07/script.rb new file mode 100755 index 0000000..2755f94 --- /dev/null +++ b/07/script.rb @@ -0,0 +1,72 @@ +#!/usr/bin/env ruby + +require_relative '../common' + +class Day07 < Day + class Tree + attr_reader :tree + + def self.parse(input) + input.each_with_object(Tree.new) do |command, tree| + if command.start_with?('$ ') + cmd, directory = command.match(/\A\$ (cd|ls) ?([A-Za-z\/.]*)\z/).captures + tree.cd directory if cmd == "cd" + else + if command.start_with?('dir ') + tree.directory command.split.last + else + size, file = command.match(/\A([0-9]+) (.+)\z/).captures + tree.file file, size + end + end + end + end + + def initialize + @current_directory = [] + @tree = {} + end + + def cd(directory) + if directory == ".." + @current_directory.pop + else + @current_directory << directory + if @tree.dig(*@current_directory).nil? + @current_directory.inject(@tree) do |tree, directory| + tree[directory] = {} if tree[directory].nil? + tree[directory] + end + end + end + end + + def directory(name) + @tree.dig(*@current_directory)[name] = {} + end + + def file(name, size) + @tree.dig(*@current_directory)[name] = size.to_i + end + + def sizes(directory = ["/"]) + @tree.dig(*directory).each_with_object({directory.join('/') => 0}) do |(name, content), res| + if content.is_a?(Hash) + res.merge!(sizes(directory + [name])) + res[directory.join('/')] += res[(directory + [name]).join('/')] + else + res[directory.join('/')] += content + end + end + end + end + def part1 + Tree.parse(stdin).sizes.values.select { |v| v <= 100000 }.sum + end + + def part2 + Tree.parse(stdin).sizes.values.select { |v| v >= 8381165 }.min + end +end + +Day07.run diff --git a/08/input b/08/input new file mode 100644 index 0000000..ffdd5b1 --- /dev/null +++ b/08/input @@ -0,0 +1,99 @@ +011201001000123232122300403211332202122424332111151523152310403222120311202224013311113203311122001 +102201211220103202323002411123324024134251423232343141424431144431022143443131141222323122010012220 +002022232232102013311311301321044421352134513235443522243113311323213013301030440423203311103101020 +002212021110130112400112141310424411421435324412235335534245212325313312413434430114302030310200000 +202000312203303110042324124431114212153432535412115323131323541522511253002131401212012131031321110 +120203002030304244102444143021314124552253521343321513511124144454343454102412220143321013220111212 +012133330030200444414333325255125142322121412245551424242135123513411533153323130432231002121211210 +012300220003100211444401144353345522135453435223464636451554321531323513234331131114224102232333310 +000201310302031301344341134512444312432566266533252332656654251123535525135213300040211242011230031 +121002133124022203240512452433331223524622643252532325346635555552514352434233454024134201432001111 +112213110140442401434531255323512525265655352543623565556265545555235551143135124203142220020302000 +033001202421424143555311243322416262636322654432542644633525453666655354514351121150231101002002310 +133303044321221102531154151541224245536464535622566355532543644552554644312231415325142303020031312 +112333344411024045335422225146442243536464622553556234523445362524655623435243115242532313102020021 +123023422310204444121415255225254435644446326664473564435422565325224635623542352154120001224144312 +102303024142131515433355242322223363453633455657436436574636544336652564626641215232314243234004001 +021344140434455245134415553542352643537665534644574553367446664254532523624521243554252411433032112 +100140101131444142414254644644434663647646555637737475474473636562243265243536422253531252202124322 +111043301134142345422324342454235374733474673456744577746755765343355535622342432251442244140231224 +232120141224122324244532255543256467566454337465647735433663734756446566663442525425254525142212311 +102310340244433433553362546534535757453376643763344566566557634664535744445254533545223521213213014 +330111232252114431226334564636467643777466445537346735563633337744734474466254662225321443542401243 +344311321253412213666443354643647347656735478884748547564466577774737677363226422622515344334010341 +222340105234132533423442424376674757757675647476557547564445666676437755355626455436313331225343402 +334241221121414455435265324646757456774784864846746867457546648735456677555624445353555434433340341 +333430021343121666326363537633737767788775478777445558858444854755434544735362564626265444342330020 +332224425333452333556456745375454657668644877658744844547774765844454475434765533256545152435214410 +411322431421116436336347456774356844455867864644844878664785864444475567664473365652253123453220003 +130345455115155526426437663665665668576686587847778476557678655576775553535745643522532344433523042 +241333521455132466523664543556384776587787745658797755565556576546847447434775733626365325451531301 +202135433134452323646546565545646885747548679876767658578565476458486465755545465664435422532454541 +344025243154435524235475655536845545775797795959686975588855546544875685664377465455353661444452422 +120511445324333353265334766654858656857755669695597899959957757545465687447345336332322634434414522 +041425252513666643357433333888555778459755889566967799659865567757457567576463765323236366123221323 +142343534134652443554434768544658774587669979986775985989879787975445856456554364645323653454511323 +443155144123223566654765746758688875856899955996667659755976778586644774777456744653322566354532232 +013242522162435447764573774877757879868788876799896979576555597985885468586473474464455426633333452 +101431333345553353553337548577478656967866886877887889699999856979976485874455544477534422463331314 +422325541533664573334736464857446675995799977868697688998787757585668584775766563477536636621514142 +313315332565562573637376866486547776557979868979996867779966955885566785455767644565336236452541411 +231122455364326235655644685888786685587796897688777698977986787596655865857448333576665523356143332 +314415154446535337443754887576795586569678967879977696896667866768856785586774365336636426455324541 +251324556334435537567468544665859588888869687767876697967887867856679658788866355353346553442543353 +134353225423362456734666455757877769577678987866887877799886886885859796767664444665534363236114533 +251241346545626367667578575687955957988796678987999979977897978877579787844755554533763646262143315 +353543555544353753474346565789555799696999667888997899777669899867697878476845756644734243432245123 +221521465632632737777766744748678869769669668898887888987898666789869785645678734643552655525624335 +351552456455622567337644647486678857766866879798779999878779869885869788874885873744772665326352324 +153242153665224557353455465857685566797887699998988788877767696668668675666664465757646436665554315 +321113152666424547657348876876678597898779877779998999978978777885976658558676664444674243656333212 +421511245364456777655754476646885699898678788797778788778778999795687589454774563737333365434325525 +213532265252653474345766566877669898969767779777879797899977669868595997784466766655547455234435452 +125232235356525353764645486577668857868788699799787978898988669878688867586564457353673236664223343 +441242562562223643773466848558599657669669968987979987787969676777796877655876456745562442232541525 +135122156635652746574578664849659556998698797778797889988788869797687977855447474475546334662522444 +144315413442434666777478887855885568968799966889978797797867786966685589646488554737453225335213325 +511213234443636433646678657654587788667696879878989799867699988765895758788675765445656425425334544 +052145146243336363474565857687976889776768867898889967686967668565998666768575534675445244546424155 +235513424332444756467574556745997958679678999799789777998968876679956958746554677637762355253554335 +331155256654246447545776676487485655676699678967897779688898989999768655488874564764434223445132152 +152152253254255234473347878744485766678897879689897888667968795675655864748584644653323363464154251 +245154344235223346354765457746867586969968687876867899766986679689585778787473555663424264553541344 +235253541263666477544377746867785766579566868896689699876766695956667455774677646576554255521144355 +033522343456423455634354575764856898657669957896677776766596688857787474488436433455326554242331222 +321532413246333226435563556578864759978657759897896799958558598588975755858856537673332523244522141 +101422552442534324467345475684574877989759655788785857697957976679848768474665346435664653421242444 +103425135453353666736674677847548667967885667765576995797857589796866646565443377775564436443333543 +041131245522253556554636537878457787468577768876778599999689667978487755747676763666266652435545351 +424232344536333364556466767555646468677996965895798968676966868665587584664535544755645232211324114 +334034423343366322247357747635787857555667756956768657779596877655676555367754633345545442422145333 +424344345121532564223643563774744574746844686888885779565686455664565643634566756543362551431252401 +232123114442152332546734767375678475664877645798695799654765488755876545334345452455463633534433233 +414423121145222454332477456654436757485685646546884446667744645744675377357654436554353221451345404 +234130223314353235262655676637577858558554556467788664555466588778677546456334642623666524333250443 +414001252524152463224524734765775677644765886656877578666575855788653533364773455446565352432424320 +024323134311552535243564667476365737764685474446876486685546667685363735646643655435355533243440034 +331141044111451522226436327536773637654887585478885586588745847444466365767232265352211455413531431 +241321311312323233252665352455436545735388846445565487667644555744667753556266645332452431541341044 +234030032312154414563442653244476576544654676884777445766434443476675746365245632242515342521301442 +202112004552434344564624422526757574735736566643654737673776743354737744242533546523434111443034202 +323422334452422412134566522262537334374446646774476534347655656336555632626663364214333341322443300 +031243421344143515452456444553266345437737337675574563437533573575456463665443432215213541123241240 +002114441013421154331235436253442576764555535635775735635377573353666526333623554524421352131443020 +111342301013125124521245222663363363467436357434736773473777464564655622533254352444544553311114233 +222323000331131531351433363334345533653474663466435557365553376535246564324662454422135233103003200 +002103300342403311123432563445263435443255635353333564444573456633466424623313351545133022013122320 +300304042333200223123432352234525626625234236453763536563235242224645265643515312312520000242200000 +210121244033001141551213125554433445222532265463624234562324443435344362451243522525243002401410013 +303323342040431313415255233251226626334264553655346266323665655652522363324341123235410333230402230 +300112203021311400115231251543524354554262365526643622264625434236554412121342322522123434120120203 +011001023222004142422445443143212526654365444643442536365453622222634154551441553400220414300301332 +230032020342121242113341435444543253355352433246243543645634456552525315313143243431133302112320020 +232132202131312144424224413332214322543242664652323324456222615212244414145113442320023403200012111 +112222030320432400004224215552515412325221525356363643351335211141344255554442403013303120331312202 +110321230332323212301432002424211134155124241552254334221154111545254253122214132321141212032221001 +021020121321302320401011302153145251455114411155452245422422135453412153112033343412340101230302010 +122203012232330222213342133012334131225335225325343355122151154444535354414423434423220001330003020 +221120213213221333210131242440001425551432522221141211124544343151315104244333311124033221102312011 +202201033331203122413021242022211002455531321354252412214332215513311231404032244320020330332301120 diff --git a/08/script.rb b/08/script.rb new file mode 100755 index 0000000..27cf95b --- /dev/null +++ b/08/script.rb @@ -0,0 +1,56 @@ +#!/usr/bin/env ruby + +require_relative '../common' + +class Day08 < Day + def part1 + grid = stdin.map { |r| r.chars.map(&:to_i) } + + grid.each_with_index.inject(0) do |visible, (row, i)| + visible + row.each_with_index.inject(0) do |row_visible, (tree, j)| + if i == 0 || i == grid.size - 1 || j == 0 || j == row.size - 1 || + [row[0...j], row[j + 1..], grid[0...i].map { |k| k[j] }, grid[i + 1..].map { |k| k[j] }].map(&:max).any? { |k| k < tree } + row_visible + 1 + else + row_visible + end + end + end + end + + def part2 + grid = stdin.map { |r| r.chars.map(&:to_i) } + + grid.each_with_index.inject(0) do |max_scenic_score, (row, i)| + scenic_score = row.each_with_index.inject(0) do |max_scenic_score_row, (tree, j)| + scenic_score_row = [ + row[0...j].reverse, + row[j + 1..], + grid[0...i].map { |k| k[j] }.reverse, + grid[i + 1..].map { |k| k[j] } + ].map do |direction| + taller_tree = direction.detect { |t| t >= tree } + if taller_tree + direction.index(taller_tree) + 1 + else + direction.size + end + end.inject(:*) + + if scenic_score_row > max_scenic_score_row + scenic_score_row + else + max_scenic_score_row + end + end + + if scenic_score > max_scenic_score + scenic_score + else + max_scenic_score + end + end + end +end + +Day08.run