From: Brett Parker Date: Wed, 8 Dec 2021 21:16:08 +0000 (+0000) Subject: Day 8 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2021.git/commitdiff_plain/64d8769ea65405cdc0a1f8e65a8a1fa25bbf71df Day 8 --- diff --git a/day08/get_count.sh b/day08/get_count.sh new file mode 100755 index 0000000..4a89d5e --- /dev/null +++ b/day08/get_count.sh @@ -0,0 +1,156 @@ +#!/bin/bash + +set -u + +filename="${1:-input.txt}" + +exec 3<"$filename" + +declare -a all_inputs +declare -a all_outputs + +while read -u 3 -d "|" inputs; do + all_inputs+=("$inputs") + read -u 3 outputs + all_outputs+=("$outputs") +done + +one_count=0 # 2 on signals +seven_count=0 # 3 on signals +four_count=0 # 4 on signals +eight_count=0 # 7 on signals + +for output in "${all_outputs[@]}"; do + read -a seperates <<<$output + for s in "${seperates[@]}"; do + case ${#s} in + 2) + ((one_count+=1)) + ;; + 3) + ((seven_count+=1)) + ;; + 4) + ((four_count+=1)) + ;; + 7) + ((eight_count+=1)) + ;; + esac + done +done + +echo "1 count: $one_count" +echo "4 count: $four_count" +echo "7 count: $seven_count" +echo "8 count: $eight_count" +echo +echo "Total: $((one_count+$four_count+$seven_count+$eight_count))" + +declare -A seg_vals=( [a]=1 [b]=2 [c]=4 [d]=8 [e]=16 [f]=32 [g]=64 ) +declare -A char_map=( [119]=0 [36]=1 [93]=2 [109]=3 [46]=4 [107]=5 [123]=6 [37]=7 [127]=8 [111]=9 ) + +get_digits() { + local input="$1" + local output="$2" + local -A segcounts=( [a]=0 [b]=0 [c]=0 [d]=0 [e]=0 [f]=0 [g]=0 ) + local -A segmap=( [a]= [b]= [c]= [d]= [e]= [f]= [g]= ) + + read -a segs <<<"${input}" + for seg in "${segs[@]}"; do + for (( a=0; a<${#seg}; a++ )); do + ((segcounts[${seg:$a:1}]+=1)) + done + done + + # we've now got some counts, so we can do *some* of the mappings + for seg_name in "${!segcounts[@]}"; do + case ${segcounts[$seg_name]} in + 4) + segmap[$seg_name]=e + ;; + 6) + segmap[$seg_name]=b + ;; + 9) + segmap[$seg_name]=f + ;; + 7) + segmap[$seg_name]=dg + ;; + 8) + segmap[$seg_name]=ac + ;; + esac + done + + # now go through segs again, this time checking how long the string is, because from that + # we can find 7, which will give us a. + + for seg in "${segs[@]}"; do + case ${#seg} in + 2) + # this is number 1 - it has a c but no a + for (( a=0; a<${#seg}; a++ )); do + if [ ${segmap[${seg:$a:1}]} == "ac" ]; then + segmap[${seg:$a:1}]=c + break + fi + done + # because we've worked out where a is, we now go find the one that thinks it's a or c + for seg_name in "${!segmap[@]}"; do + if [ "${segmap[$seg_name]}" == "ac" ]; then + segmap[$seg_name]=a + break + fi + done + ;; + 4) + # this is number 4 + for (( a=0; a<${#seg}; a++ )); do + if [ ${segmap[${seg:$a:1}]} == "dg" ]; then + segmap[${seg:$a:1}]=d + break + fi + done + for seg_name in "${!segmap[@]}"; do + if [ "${segmap[$seg_name]}" == "dg" ]; then + segmap[$seg_name]=g + break + fi + done + ;; + esac + done + + output_digits="" + + # finally, we can get each of the digits in the output, so go through the output + read -a charsets <<<"${output}" + for chars in "${charsets[@]}"; do + char_value=0 + for (( a=0; a<${#chars}; a++ )); do + ((char_value+=${seg_vals[${segmap[${chars:$a:1}]}]})) + done + if [ "${char_map[$char_value]+abc}" ]; then + output_digits+=${char_map[$char_value]} + else + declare -p segmap >&2 + echo "Chars: $chars" >&2 + exit 1 + fi + done + + # remove any leading 0s the cheating way + echo $((10#$output_digits)) +} + +# Now lets start playing with the first input and try working out the outputs + +total_output_count=0 + +for (( a=0; a<${#all_inputs[@]}; a++ )); do + ((total_output_count+=$(get_digits "${all_inputs[$a]}" "${all_outputs[$a]}"))) +done + +echo "Sum of all output digits: $total_output_count" diff --git a/day08/input.txt b/day08/input.txt new file mode 100644 index 0000000..c8ef8e2 --- /dev/null +++ b/day08/input.txt @@ -0,0 +1,200 @@ +fdb bgfa cedfg abedcf defgba gbaed dagfbce fbged dgabce fb | bf dfbega fb baged +ce cgfdea acdfe befda afgdc fec gdfbca ebdcgf cgea adgcfeb | fgcadbe cefda aebdf degcfa +egca gdcaeb cbdfe bdaegf dgbafec ge agdcfb degcb cgadb ebg | edfbc bcdeg bfcde baefdg +dfbc bc facgd feagb bfadegc gacfbd acgfb bagedc gacdfe cbg | afdcegb bagfc egcafd bcg +edc aedcbf dgcf cgebf egdacbf bdega egcbfa gcedb cd edgcbf | gdcebf bedfgc dbcgfe cd +gcfabed fed facegd abdge gcdfb fbdge befa abgdfe dgbcae ef | bgdfe befa fgedb adcbeg +fca fbdcega cadfg ac dbca dbcgf cgfaeb dbefcg gcfbad gafde | gfbacd fdgea gdfcb bdac +eb bgafe bgcefd fegcadb daeb eafcg gbdaf bfe edgabf agfbcd | cfdebg aegbf dbagf dgbfea +aebfgd gbcead dfbecga gdceb decfbg aeg ae gacfd eacb edgac | ea aeg ea dfaegb +gdc fceagd cagf cgfdbe febcad gecad gc agdeb fcade bfaecgd | fdaecg gdc cegafd adebg +bdgcef acd bdgacf gbcfd da agbcd bfda eadgcf bcgea cfbgade | fcbaged bfdegc gecdabf adbf +bd faebcg cdgfa fcbda fbd fbcae aebd bgcdef gebcafd cdaebf | dbae fbd gcebfa gacfd +gbaefc gbfdec gfdbe edbgcaf fdab dfgabe gadec agbde ba gab | bag fegdb badecfg bag +feba fb fgbca acfdg gcfedb fdbecga eacgb abcgef fbc adgbce | gcfba afeb gbafc ebgfdc +fegbc abdfce egdba cea cgabe faegdcb edfgcb cefbag afcg ca | dbgcfe gfac bgead aegbcf +acdbf gfebc afe ea fdeagb fgbaced cfeadb eacd fgdcab bfaec | bgadfc bfaedc agcdbf ecgadbf +cefag dbgecf cfbga dfceag gdfcbea dfebac eg cge gdae cadfe | dagebfc ge ge cbdfae +cgefabd agbdc dgef bedfac gcf cafgd dfegac fg fadec afgebc | decabf febdacg bcdag fgc +dfe bdfa gfeabd dacbegf agcfeb edacg efagd gedcfb bfgea fd | aedgc fd febag acgde +dgcf efbcga adgfbc gbcfa adfebc eabgfdc dc acd dcabg egadb | fcdeba egcabf acfdgb dbgac +cagfdb eagdc ea gacedfb beac bagdec gdfbae eag cgfed dacbg | dafgbe adegc daegc adcge +adf ecgbda afegb dbacgf adfgb ecgfbda cgdf fd dacbfe cgdab | eadgbc adgbf gdaebc gdebcfa +abfecd gdbaf cdfg bfd gacfbd df badegcf fgcba acebgf eabgd | abcfde edbag cfegab fdegbca +cdfeba dbec dc dfcea bdfea cafebdg gadfcb cagfe fcd abgfed | gdabfc eacfg cabfde bdfgca +eadcgb acefd degac dgface bcfaegd baegdf cfdg df ecbfa def | cefad dcgf fde feadc +ceagbd bedgfac efbdc gd edgbc gbad gdc ebgca fgecba egcfad | ebgdca fcgade edgabc fagebc +aedbcgf fg agfdc gbcad edfcgb egcdaf daecfb fgd ceafd afge | defac fg gedfcb gdcfeba +egfbda dbcfeg gafc abdce dgfabec afged adegc cgdfea dgc cg | gcd degfa cbead bagefd +de aedbf aed acfgde ebgaf cafdgeb cdfab defabg bedg acefbg | efdba befag dbafc fcaged +cdgfba dfb efadbcg dfea ecfabg gdabef gbafe dcebg fegbd fd | fgaeb dfabge eabgf gdefb +bagcef fabeg ebca fbged afe cdefag gcdbfa bfagc agdfbce ae | fdgbe cbgaf bace gbdef +eafdg dfc cdfgbe bdce cebgfa gfceb dgcfe cd begafcd gdcfba | edbafgc ebgfc fcedg aefdg +aegf bgedc cfbega fgcbda cfgeb gf eafcdb feabgdc fgc feabc | cedgb facebd bgadfc gefcb +ebacgd faecb bd cdb cgaed bdeg fgbdca dcgafe eabcd afdbegc | dbc deacg dceba gfbacd +aeb dcabge ba edacf bdaf fcgeb cfabde agfbdce afcbe aefdgc | abfd fgecad gdaecf afcgde +dbgc gfcea cafgd fgcdab dg abdfc bcgeafd fgd eabfdc gdaefb | deabfg cgbd dcfbag cefag +cabgfe baedcg cgd dg acfeg adgf bfecd fbegcad egcdf fgdace | cdg egcfba defgc acbedg +ef fbcgd afde eafcgb bfgcdae dfacge egdabc eagdc gecfd cfe | caged efgdc gdeacf fe +ae dfeabc fgea fbcegd fcdge ace cgdae bcefadg dbagc dcafeg | cgadb becafd dcbag fgae +eafdbg fdgc fgeadc egacd egd ebcad bgfeca acefg fdgeabc gd | afebgd cfeadg gd ecbda +ebgfc agcbd bgcdef ea gfea eafgbc cae ecbfad cegba egdbcfa | cafbde dgcfbe aebcfd badefc +badfce cf ebgcd acgbfed afdgeb bfagec baedf cdbfe efc dcfa | debaf bgedfca facebgd ceabgf +ebfac dba ecgbaf ad cbfdega edcbaf feda cebgda cdafb dcbfg | dacbf adb fgacdeb fdcba +acdebgf gbdefa gaebd dgb gbfe aedcg aedfb afdebc gb cdagfb | geadb adfgceb eacgd debcaf +adfb adfgce ecafd dgbace bd cfegb bfeadc begdcaf fedbc bdc | bd ecdabf dceaf dbc +gaceb fg edbgca gafb fcbeg bfaegc eadcfg gcf bfdcega bcdfe | eacfgd becadfg gf fcbed +bdfcg ad begac gfbadc dga abcgd dcaf gcdaefb gcbdfe gefdba | dfbcg cdabg adcbg dagbc +febgdac fgadce fa gdfbc dcgfba gcfebd afc gabcf bagec bdfa | agbec bdfa gcbaf fbcgde +bdfgae fgead faebgc dabg bcfdge gde gefba gd edcbafg dcfae | cgfaeb fgcabe cafgeb gdcbfe +eadcbf agfdb bfa ab dfgbc faedg aegb dcfaebg gbfaed dfcaeg | fagbd dfgab fgadb adfceg +gaedc aegdcbf bdgf feagbc cedfg dbfec ecdbgf fge fg cbdaef | agedc dbfegc fg efcdg +db becfag abd agebd bgcae bced efdag gbcade gbcadf gcbeafd | bd bdgefca dgeabc bd +febag deg gefdab becdag defa dbfge gbcfd gcbeaf bfdgaec de | fead bagef abfeg aefd +dfac aegcbfd aefcg daebgc eadcg fa bfeadg adgcef fae fgceb | cdeagfb fea cedgab bfcge +ed dbfgce efdcb bafcged dcge bcadf afecbg dfeagb fegbc edf | debgaf ebcdf de fdgbec +fadg dgefc da eafcd adc gdecbf bcgdae afecdbg dgafec caefb | caedf eagbcd gdaf gefdbca +degbcf gfdbace fbced bedg abgdfc gecfa fceadb ecbfg fgb gb | gb gbdfca dceabf fcegb +bagfce gfbac fbdcga ceab edagf eb afgeb gfdbec cfdbgea gbe | fdgceb fdage fgbac bfeag +afbecg aecdg bfcd dfecgb febgad efdcg fed aedgfbc df efbgc | dfcb dfcb cbgef fd +defca bgac egdfbc gfbea fgabde ceagdbf bfaecg cb afebc fcb | cb acfeb ecabf ebfca +cdfbga befa daebc afcdbe fdabceg aed gdcbe acfdge cafbd ea | dae cedbaf dcgeb afbe +dbefg gecad ecafgd aedgcb fecdba cb cbedg cbd ceabgfd bacg | fegdac eadgcf geabdc cedag +abdcfg eagf cgf ecgbd badfce cabgefd gbcef aecfb fcabeg gf | gf gabfdc cgebf abcfed +agdceb ebadc dfebc gbdac fegdac adbfgc cae ea befdgca ageb | edfbc acbefdg cdgefa bdcga +efca fgbca gabfe eab ae cfaegb caebdg abfgcd dfgbe cadbgef | bfacge fabcgd eafbgc cfgbad +cedbgf efbagcd aegdcb dabef feb ef badgf cdeba acfe ecdfab | ef eadfb baedcg edfacb +af gfa egbdf ecgabd afcgdb fbadg facegd cbaf gcbda eacbdgf | abdcg gfadbec bfedg cdbga +dgbea afeg ef cbedfg cbgdea fcabd adfbe efd efdgba gecfbda | cfbaged ecbagd fbgecd ebgda +gcbeaf bf ebadfg dafb bcgde daegf cagfde bdacefg bfe dgebf | gceadf gefacb fgdeba gfbdea +dgafbe dgfce cb cbafde eadbf dbca cbf cedfb gfaceb fdegbca | bedacgf bfc cdab ecbagf +cadbef dfgac gfb bcedf gbdafe cgbe gdcfb cagbdef egfbcd bg | gdfbc ecbfdga bdcgf bacgfed +dfcb cd gdbae gabdfce bfceg bdcefg ced cfdeag ebdcg efgcba | fgecba edbgcf cbgfed fgdeac +ebgdcf cdfa cgafe ace gfedc cadfge aebgf adgceb fdecgab ac | cegdf gfeba edcfg ac +agbefd bdgfe fdcegb dce ebgcd bcdga decbafg ec eacfgd cbfe | ec edgbc cgdba ec +bgadc adbecfg gdbfce fdg dfeac agfb fdgac fg cbfgad dcgeba | gf dcfbge bgadc fg +badegf abegc dfgc gbdaf fdgaebc cgbfa cf dcbaef cdbgaf bfc | dabgf cageb adfgb bdfagc +ce abgdef ecdg fgbca fdage cadgef eca acfge acfbde bedfgca | cfage bagfc gbcfa eac +gdfbe gcfeb febgda cfb fedc baceg ecgdfab dagbfc cf fbcdge | gfebc cgdefb bcf gebca +gceabd bc ecb bdcef ebfacd fgeadcb abfc dgfec bdafge fdeba | gcbefda egcdba ecbgad gfbead +gabec gaefbcd bgeaf ebf dbfag ef edgafb fbdgec fade bdfgac | bef abdgf fe ceagb +bcfaged fdbgc dafcg edfacg dfb fb bdagef cfba dgbec gfdacb | fadgc cgbfad dcbeg cafb +bdg bfdcae eagdb eacgd fagb afebd ebfcdg fcaegbd bg egdafb | fbedgc ebadf dgeabf aefbdc +ad cgadbef eabcfd fdabgc dba dbace ecdbf fdae bdfecg acegb | bcgfda gfdcbe eafcbd cbgea +efbda dcbfe bc bcfg egfcd gdcefb agbcde bec ecfadbg caefdg | dcagebf bc bfcdaeg cabfedg +agde gdacef adgfc cbdfaeg cgfde cde badfec abcfdg fbcge ed | cde eafbdc cdfbae afbecd +fdg bgfeac afgcbd bcfag fgeacd cdfb gbead fgdba cgfdaeb fd | bgacf gecdfa fd dfg +ga agc cedgbfa bcfdea fdgcb geab gfbca ceabf edafgc cgefba | bgcfa fcagde edbgafc becaf +agceb ecfgba decgaf eaf ef cdbaf gdcafbe fgeb gcaebd ecabf | fbdcage aegbdfc bagce fdagce +cdage aebfdc bgacf fbeagc cfd bgfd defcgab acfgd gafbcd fd | cfadg dbface dgbf gdfac +cefbda afcdb dgecfb dab cdefb bafdge ad deca gaecdfb acfbg | cbdgfe fecdb bdfce efbgdc +aefbd ebcgfa ad ade egfbd abcefd cfeab bacd afedcg dbcagfe | edcbaf gfedcab dfgeb gefbd +agdb ecfgb fcbaed agfdc bcd fgdcba faegbdc deacfg db dgcfb | dcb gcedfa agdfc bdacgfe +gdfeab gcdfeb dg fdebc bdacef fbcegad gbced cdgf edg baecg | gcdf dg gbeac bedcf +eb degbc bead cbegfa cdgfb gecad gbacde bce adfecg eafbgdc | dfaceg dbgce gcadfeb bcgdea +dgcefb cad gcbad da dfcgb bcega bcfgda afdg dbfceag fcdeab | egabc bgfdce gcadb fbacgd +gfadce eabfgc df gedab cdgf debcfa eagdbcf acgfe edfag afd | fadeg cbeadf aecgbf adfeg +abcdg fecd ged aegcfd dgeca afegcb afcge debgfa de eafgcbd | ged efcgda gefcad aegfc +gbfecd gafcdeb ebcgd efgbad fg dfgbc egcf egbdca fadbc fdg | dbacf gf fbdecg gdceabf +ebdf dbafc gdcabf dagecbf cabedf afgbce ef cfaed gecad aef | ef dacbf febd gcbafe +daebfg gfceadb agbde aegbc ebagcd cdgb cg baefc egc dgafce | cadbeg agbdec eafcb gebac +dcfgbe ca acgde begdc dabc efdag aecdgbf ceadbg cae aecbfg | cgebd bfedgc defga bdac +cbdf dacge fadec df dbafge eafcb fbdace dfa bgacfe cgafdeb | df eadcfb cdefa fecad +fb feb cgeab cagdbe ebfadc gbcf adfge aebfgc acgbdef bfgea | efcadb gdcbae fbe afegd +dgcf gaecb gfdeba bcdef dgb fgadcbe dg fdebca dcegb begfdc | bdg ceabg gabce gd +dagc gaedb dgeabc ag ebcdg age fcaebg adfbe cdefagb gbcdfe | bdceg gecabfd acdfegb gacfdeb +acefdg gacebd fdga bgdcfae afceg ecadf fgebc cdfbae gea ag | cfagde ega dcefab afdg +bcgadf defgb cfea ec gdeacb efadcbg ecd ecgadf edcgf fdcag | dce fdgbe egacbfd efcagd +dcagb dagbcef aedcgf deafb adbfg bfgc bgafdc gcedab gfa gf | gdfba fedba eafdcbg fg +fedacb gdbcae gefda fbcae gecafb afegc gca gc edfbgac fbcg | bgcf gac ecafgb egadf +ecfgb fdbega cbga gce bacfeg agfedc cbfgaed cefdb bgefa gc | dgacfe daefgcb gebfa ecg +gefadb dcfbge ag abg fbcad cfgbea edga dbegf dgfba egbcdaf | ebfdga adgcebf gbdecf dbgfeca +ecfgbd agde eacfgdb dgcabe edbcg facbd cabfeg bdace ea aeb | aeb fabdc egdcb ebacd +ecgfda af fac cgfde cgefbd fagecb daceb gafd ebfadcg efadc | af fcegd bgceaf fca +ca cgade adefgb dgfea edcbg dgeafc cfga dgbceaf dfaebc dca | cedga bdceg degfa ca +gfced dgecfb ga ceafbdg afedb acdg gfeadc cebgaf fag fdgae | cgfeab ga dcfeg dbafe +bcefda fcega dbga abgcdfe bgfdc dfgbce dca ad fgdac cadgbf | ad bacdgf da gcdeafb +gdcea caebgdf agfdeb cfabge cbdafg gef bcef acgfb ef fagec | ecgaf fcage acged fe +egc bdfge cedbg befc dabgecf dgfeac cgdab ce dcfebg bgfdea | dcgeaf ec egbfd ec +gcbdaef fd dbecag fde gcdf acebf fgdbea gbdcfe dcgbe cefbd | egfdab fd cgfabed cbdegf +abec baf ecgaf facgbe fecbdga ba fagecd bgcfda bfage dfbge | bgceadf ebdgf dfabgc acegfd +dca eafc dafcgb cbead efabd cfbade faedbgc gdecb gfbdae ac | adefb adfecb cfbeda acfe +eac becfgd fgadcbe geafd fcade eagbfc bcad ca abedcf fcbed | fcbed bacd fdgecb abcgfe +bfad cbged efdgbac gdefac fd acbgf bdfgc cafbdg acgebf cdf | fdcgae gabdcf gcbdaf cabfg +ebg bdfce gfecb dgceafb cafeg dgfcea cafegb gb cgadbe bgaf | cebgaf dcebag gbfa egacdfb +edbacf eba dbgcfea dbef abdcge bcgfad gcaef be ebfca cdafb | gbedca cbafd edfcab bdfe +fcdbae efacd aebd gbedfc afgdc gfcdeba aef efdbc ae cfbeag | bcfde abcgfe dcagebf gfbdce +abg gcdfb ag afegdbc bfagd bcdefa fedgab eagbcf deag adebf | ga bdgfc efabd edbagcf +fb befc egafcd gdbcf gecbafd fgb dgfce dcgba cdbgfe adebgf | cbgfde bf bdfceg edgbfa +gefab efdabcg eagcbf de eabdfg geda egcfdb faebd cadbf edb | aefcgb fbade de afbge +edfcgb ecadgb adbcf decbf fcgaed dfegc ebd dbagcef egfb be | gbef fgeb eacdgf dfacb +dfc ecfgad abcfd befgca cgfabd cagbf gbdc dcbaefg dc bdefa | cd ebafd dc fdc +aefgc cfdga acbefd gebdacf efg cbefga fceba eg begfdc bega | gbfdce dfbegc geba dagcf +cfgbad fadebgc cga efgcd afecgb ac acegf ebgfa fbgeda ebac | fgbedac ac ecba beca +dc gbcfda dbc egabc gcadbef agbcde bgdef deac bdecg gecafb | ecad gfbaec dc cegbd +cadgef eb cgdeab ceb cbgdf befgc egcbfa cefdgab fbae cfaeg | cgdbea acbefg gaefdc gbecf +cdfbea gbceda cgdfb cdbge geb acgefb dage eg afcgedb beacd | aebdcg gbdeafc ebdcga bcged +cgbfed ba agefdb acbedgf adegb dcega abdf abg gfbde gbcefa | bgedf dbgfe dfab edbfcg +ga geabc bfedacg efga bfgaec bgecf agb cdbfag abdec fcbegd | egfdbc cefabg ga gaebc +cfebg cfgbea acdgf edgbfc dgfabec db fdebac bedg dgfcb dbf | dcebfa egbd dfb fdcgeb +ecdgf ad ebdgac dca caebgf gebacfd fbace dabf bdecfa fecda | bagfecd dca abgcde bcefga +de def dbeafc dcbaf efacdgb cfagdb afcegd ecdb gfeba dabfe | bdec fadeb de febag +bdgfac ec cde fegc fdbeca fcgdb cedgfb gbecd fecbgda edagb | fcgabd ecfg bfadgc ec +gfc fcbagd fg dfeg gbeadcf becaf bcgeda cgaed acgfed ecgaf | eadcgb cbfea gdef ceagf +degac bcegd cedagb ebc agfbedc dfcgb adeb be cgadfe agcebf | eb daeb cgbed fcgdb +aebcfd feabgc fgbdca bfcag df fagbd fdagceb fcdg baedg fad | debag dcfbea bgdfa gbacfd +bef fe agebd fgacbe dcfba aebdf edfg gecdab fgcbdea afdebg | fdeba dbeaf bdceag adgbef +cdefb gcabdf cgbdefa eg ebg fbegd bafgce bdaegf dabgf dgae | geb bfgacd gdea abgfdc +acegdb fcge gceabf fadbe fgcba eagfdbc ce ceafb cafdbg ace | bfead dfcbgea faebd dfaeb +agbfe bcfgdae bca gcfb gefabd bdecfa deagc cb egfcab begac | becag gedac bfgc caedbf +adgef egcd gda beafd gcfbda cefdag gd dgcfeba egbcfa aefcg | abgdcef ebdfa edgc eafdb +adecf faebd gaefcb ebf bfdc badge efadbc bcfeagd agcedf bf | dgaeb edcfag fb cbegfa +gcfa becda dabgfe af ecfda efgadbc gcfaed egfcd fcgbde eaf | fae fecgd adfebg afedcg +gbacdfe gfaecb fgeab cg fecdbg bcafg aecg abcdf fgadeb cgf | ebcfag cbdgfe bgfecd gecfbd +adgcef gfbde dafcgb bceagd edbgc dbc baec bc eadfbgc ecgda | cb gdbeca facdeg fdbge +fgabe ad dfbce fad facebg fbeadg fcgaed ebafd bgad gfceadb | dcfeb egdbaf fbecd efcdb +bgfdc gbacef fgdeba abefg afec bcgfe aebfdgc ceg degacb ec | ec fegab fgcbe ec +egfdc cbagdef fcaed fdg dcefbg dbcge fg dcbega bcfgad befg | cdeaf dgf abcgdf edfcg +cdfgaeb eagcd decf efdcga ed fagdc gabec egd bfedag bdgacf | cdef eabcg acegdf egbac +badfecg gdfabc bd dba acgbd fbdaec gbdf agbfc gecda fbcega | dbgac bcgfa ebgacf gcdea +bacgfd dabec ebdgc fgcbade fdeg fbgdec dg begcf dcg cafgbe | gaefbdc dcbae fedbgc acebd +bdcgf aedbgc befa gcfeb fgebac efagc eb efagcd efdgbac egb | gfabce acfged egdafc ecfag +fade facbged defcb fe gdbace fgbcd cagbef bceafd feb edcba | ef cdbea fbe becdf +edcag gaefd edcf cfegad egdbafc cfaegb aec bdcag ce gbefad | bdfgae abcgd cae cdbgaef +cegbf ebfca gbc gc debagf cdgbef becagdf bfdge dabceg gcdf | ebfca cegbf ecbaf eafbc +dgafe gbcdef efcdab bcga eafcdgb cefgba abe gcefb ba egbaf | gaefbc cfagbe fcbeg cedgbf +befgac bed dfcabe db fcgeb gfeda decafbg egcbdf bfdge cbdg | eafdg agfceb febagc gbefd +cdbfgae gcdfea agb gbfad dgfcb faegd ebda eabgfc ba bdgaef | gdfab cbaefg bdgaf fbgeac +cabgd cbfagd fd gecbda fceadgb fgadbe fgdc ebcfa cbdfa dfa | gefabd fd cadfb gcbdea +efga gfbce abgce cbgead cagbdf egdfcab bafecg bfecd fg gbf | fcebd geacb gadfcb fbcgae +efg eagfd gcea bafgecd gdfebc bdaef cdafgb afgdec dgafc eg | eafdb dafbcg cadfg caeg +ebcgad dbe efcgd caegb cgbefda dagb fabdce ebcdg cebagf bd | cfbaeg defgc fgced becfga +bcadge fbedg aegc dbfcag ebgad gdbac gcfdeba ae eba bcdafe | gace agcefdb gbade eba +eabd bfcagde egacd cgebfd ad dcgeba aecfg ebcdg cfdagb acd | dcgbaf cafeg egfac bfcged +ebaf gcdeabf edgba dfacge gfbde fgdcb aecbgd bedgaf edf ef | bdegf ef adgbef dgface +fbedca dgefa adebcgf bacfg efc fgeca fagced ce degc gaebdf | gcfea cfgeda ebfdcga ec +ecba fedcabg bgedf age dcfgae feacgb ae agcfb ebagf cadbgf | fegba efagb afecdg afebg +ea edbgf efadgc bdacfeg bgafe bcafg deba edbcfg debfga fea | fgedba bade gacbf adbe +fdbage cabgfe edb efabg aegd gdecbf fbdae de fabedcg bdcaf | begdfc adefb gfdbce aged +dafcbge febadg cfdea fdgcae ga gaf gcda bfcade egcbf cgeaf | ceagf fcegb afg adefc +adfebc dfgb bg befacg daebf bfegda daegc bdgae gfacbde bag | aedcg gab abgde debag +daegfb dgec aedcfbg fdcgeb bdcef fcadeb beg ge bgfce bcagf | fcgbed dbfega beg febdc +fgcead edgbcf adebf cfb bfacge bcfae cfeag bagc gfcaebd bc | efbad abgc edgabcf acbg +bcafegd eafdg dbfgc edgfc cdgfab geabdc gce ec egfbcd fbce | facbdg badgce dfegc aegfd +bcdafge bedfag cfgbad abgc dgabf dgfcb cb cdb dcefg fabdec | dcb bdfagce cbfgd cdb +afebdc adebc gdbca fbeadcg bacfeg eafd de bcdegf edc abefc | ed cgdab feda efacb +agecfd bgcefd fecba ga egad agbfdc fcaebgd feacg gfedc fga | dega caefb fcgea fcaeb +bfcegad facbgd egbfa ca gcafbe afec cdebg fbeadg bca acgeb | bfgace acgfdb cgedb beafg +cefbad fd befd ecabfg gefacbd bcefa fdeca fad dgaec dbfgca | adf geadc fd dfaec +adfbe becagf geabf ge gacbf efg gfdcea cfbdag ecfbadg becg | fgbae afdcgeb eabdf cgbaf +fgdcae bgdfae cfdag ea egcfa gcbfe becfgda aef ceda dabgfc | gcfaed acdgbef efbgc cade +cgbfde cdb aefcdgb cegda dgbca bafdg fbac badcfg cb gfadbe | dcb bacf cdegfb bfdag +gdcbe bcdega geadc bfgdae fgcad eabc cgefdb gae acfbedg ea | gbdec gcdea befgdc gecda +df abcgdf gfeba bfagdec gbeacd bcaefd abdfg abdcg fdb dcgf | fd fdcg abgdfc gbaedfc +eg eadcf dfabeg cedg aecdfg afgcb cefdba debfcag egf aegfc | fecad fcagbed ecfag cagfb +fbeadc gfa fg fagbd gfdebac eafdgb dacbg fcbeag fedab gefd | agedfb fg eafcdb bgedaf +gbcadf begcfd fcbed caed dafeb eacdbf faebg cdeafgb abd ad | cbefd cfebd cead dab +dceba gbcaed ag gdcbf geab bdaegfc acg acdbg bcfaed afcdeg | gcdbf edbca agcbd cdgba +facbg fdgbca afc fgcade fegbacd af cgbad cgdeba fbda cefgb | fac caf fca cabfg +badecf acbdf efac agedfb bgedc aeb baedc bgcdfa ea fgbcead | face eafc bacfd gacbdf +bc ebfgcd gafbcd dgeacf gfbea bgc dcbe faebgdc ecfgb ecfgd | edbafgc bgefa bcfgde dfcage diff --git a/day08/notes.txt b/day08/notes.txt new file mode 100644 index 0000000..5016bb9 --- /dev/null +++ b/day08/notes.txt @@ -0,0 +1,109 @@ +0: 6 segments lit: abcefg + aaaa + b c + b c + .... + e f + e f + gggg + +1: 2 segments lit: cf + .... + . c + . c + .... + . f + . f + .... + +2: 5 segments lit: acdeg + aaaa + . c + . c + dddd + e . + e . + gggg + +3: 5 segments lit: acdfg -> 7+dg + aaaa + . c + . c + dddd + . f + . f + gggg + +4: 4 segments lit: bcdf -> 1+bd + .... + b c + b c + dddd + . f + . f + .... + +5: 5 segments lit: abdfg + aaaa + b . + b . + dddd + . f + . f + gggg + +6: 6 segments lit: abdefg + aaaa + b . + b . + dddd + e f + e f + gggg + +7: 3 segments lit: acf -> 1+a <-- this means we can work out a + aaaa + . c + . c + .... + . f + . f + .... + +8: 7 segments lit: abcdefg + aaaa + b c + b c + dddd + e f + e f + gggg + +9: 6 segments lit: abcdfg + aaaa + b c + b c + dddd + . f + . f + gggg + +0: abc efg -> 1 + 2 + 4 + 16 + 32 + 64 -> 119 +1: c f -> 4 + 32 -> 36 +2: a cde g -> 1 + 4 + 8 + 16 + 64 -> 93 +3: a cd fg -> 1 + 4 + 8 + 32 + 64 -> 109 +4: bcd f -> 2 + 4 + 8 + 32 -> 46 +5: ab d fg -> 1 + 2 + 8 + 32 + 64 -> 107 +6: ab defg -> 1 + 2 + 8 + 16 + 32 + 64 -> 123 +7: a c f -> 1 + 4 + 32 -> 37 +8: abcdefg -> 1 + 2 + 4 + 8 + 16 + 32 + 64 -> 127 +9: abcd fg -> 1 + 2 + 4 + 8 + 32 + 64 -> 111 + +a: 8 +b: 6 +c: 8 +d: 7 - only one that's exactly a 7 count +e: 4 - only one that's exactly a 4 count +f: 9 - only one that's exactly a 9 count +g: 7 + diff --git a/day08/task.txt b/day08/task.txt new file mode 100644 index 0000000..dbdd381 --- /dev/null +++ b/day08/task.txt @@ -0,0 +1,129 @@ +--- Day 8: Seven Segment Search --- +You barely reach the safety of the cave when the whale smashes into the cave mouth, collapsing it. Sensors indicate another exit to this cave at a much greater depth, so you have no choice but to press on. + +As your submarine slowly makes its way through the cave system, you notice that the four-digit seven-segment displays in your submarine are malfunctioning; they must have been damaged during the escape. You'll be in a lot of trouble without them, so you'd better figure out what's wrong. + +Each digit of a seven-segment display is rendered by turning on or off any of seven segments named a through g: + + 0: 1: 2: 3: 4: + aaaa .... aaaa aaaa .... +b c . c . c . c b c +b c . c . c . c b c + .... .... dddd dddd dddd +e f . f e . . f . f +e f . f e . . f . f + gggg .... gggg gggg .... + + 5: 6: 7: 8: 9: + aaaa aaaa aaaa aaaa aaaa +b . b . . c b c b c +b . b . . c b c b c + dddd dddd .... dddd dddd +. f e f . f e f . f +. f e f . f e f . f + gggg gggg .... gggg gggg +So, to render a 1, only segments c and f would be turned on; the rest would be off. To render a 7, only segments a, c, and f would be turned on. + +The problem is that the signals which control the segments have been mixed up on each display. The submarine is still trying to display numbers by producing output on signal wires a through g, but those wires are connected to segments randomly. Worse, the wire/segment connections are mixed up separately for each four-digit display! (All of the digits within a display use the same connections, though.) + +So, you might know that only signal wires b and g are turned on, but that doesn't mean segments b and g are turned on: the only digit that uses two segments is 1, so it must mean segments c and f are meant to be on. With just that information, you still can't tell which wire (b/g) goes to which segment (c/f). For that, you'll need to collect more information. + +For each display, you watch the changing signals for a while, make a note of all ten unique signal patterns you see, and then write down a single four digit output value (your puzzle input). Using the signal patterns, you should be able to work out which pattern corresponds to which digit. + +For example, here is what you might see in a single entry in your notes: + +acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | +cdfeb fcadb cdfeb cdbaf +(The entry is wrapped here to two lines so it fits; in your notes, it will all be on a single line.) + +Each entry consists of ten unique signal patterns, a | delimiter, and finally the four digit output value. Within an entry, the same wire/segment connections are used (but you don't know what the connections actually are). The unique signal patterns correspond to the ten different ways the submarine tries to render a digit using the current wire/segment connections. Because 7 is the only digit that uses three segments, dab in the above example means that to render a 7, signal lines d, a, and b are on. Because 4 is the only digit that uses four segments, eafb means that to render a 4, signal lines e, a, f, and b are on. + +Using this information, you should be able to work out which combination of signal wires corresponds to each of the ten digits. Then, you can decode the four digit output value. Unfortunately, in the above example, all of the digits in the output value (cdfeb fcadb cdfeb cdbaf) use five segments and are more difficult to deduce. + +For now, focus on the easy digits. Consider this larger example: + +be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | +fdgacbe cefdb cefbgd gcbe +edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | +fcgedb cgb dgebacf gc +fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | +cg cg fdcagb cbg +fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | +efabcd cedba gadfec cb +aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | +gecf egdcabf bgf bfgea +fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | +gebdcfa ecba ca fadegcb +dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | +cefg dcbef fcge gbcadfe +bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | +ed bcgafe cdgba cbgef +egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | +gbdfcae bgc cg cgb +gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | +fgae cfgab fg bagce +Because the digits 1, 4, 7, and 8 each use a unique number of segments, you should be able to tell which combinations of signals correspond to those digits. Counting only digits in the output values (the part after | on each line), in the above example, there are 26 instances of digits that use a unique number of segments (highlighted above). + +In the output values, how many times do digits 1, 4, 7, or 8 appear? + +Your puzzle answer was 247. + +--- Part Two --- +Through a little deduction, you should now be able to determine the remaining digits. Consider again the first example above: + +acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | +cdfeb fcadb cdfeb cdbaf +After some careful analysis, the mapping between signal wires and segments only make sense in the following configuration: + + dddd +e a +e a + ffff +g b +g b + cccc +So, the unique signal patterns would correspond to the following digits: + +acedgfb: 8 +cdfbe: 5 +gcdfa: 2 +fbcad: 3 +dab: 7 +cefabd: 9 +cdfgeb: 6 +eafb: 4 +cagedb: 0 +ab: 1 +Then, the four digits of the output value can be decoded: + +cdfeb: 5 +fcadb: 3 +cdfeb: 5 +cdbaf: 3 +Therefore, the output value for this entry is 5353. + +Following this same process for each entry in the second, larger example above, the output value of each entry can be determined: + +fdgacbe cefdb cefbgd gcbe: 8394 +fcgedb cgb dgebacf gc: 9781 +cg cg fdcagb cbg: 1197 +efabcd cedba gadfec cb: 9361 +gecf egdcabf bgf bfgea: 4873 +gebdcfa ecba ca fadegcb: 8418 +cefg dcbef fcge gbcadfe: 4548 +ed bcgafe cdgba cbgef: 1625 +gbdfcae bgc cg cgb: 8717 +fgae cfgab fg bagce: 4315 +Adding all of the output values in this larger example produces 61229. + +For each entry, determine all of the wire/segment connections and decode the four-digit output values. What do you get if you add up all of the output values? + +Your puzzle answer was 933305. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle.