Added day 8 and day 9 solutions

This commit is contained in:
Riley Apeldoorn 2021-12-09 21:28:41 +01:00
parent 957ac97c0a
commit a4b889511a
6 changed files with 471 additions and 0 deletions

10
8/example.txt Normal file
View file

@ -0,0 +1,10 @@
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

200
8/input.txt Normal file
View file

@ -0,0 +1,200 @@
defabc gcb dbafcg gc gcbed fbecgd begfdac fcbde cfge debag | gfce fgce bdefgca aebgd
eabdc egbda bagcef eg fdageb beg bcfdag egdf agbfd cbgdafe | fdbga bge ebg eg
gfbda feg eg afgcbe fbdcag bfedg gead ecfdabg dfecb gedafb | gedbfa dgae degbf gefdb
ecadfb edg defgcb ecbda fbagcde ge dbeag gacbed agec bdfga | debcaf geafdbc gedab egabd
gcedbf bfeag bcgfdea bcgdaf eg gfe gace cgefab gabcf edfba | gbcfa gfe gef eg
bd bcdgefa dab gcdbaf dabfec cedab gcafde bgcae cafed debf | efcdab dba ebdca edafcb
cdaef fbcedg gdaeb gedcfa cgaed bcfaed gc ceg cgaf eadfbcg | cdfae acfg adbcefg cg
cagbed fbedca cfgdabe ecbda aedfg bcge dfagbc dceag cdg cg | abdec cgd bagdecf eadbc
adbfc fdcabg bg ceabfd bfg bcefga cgbd fgaed fdbag cfbeadg | bfadg bagfcd aefbgcd bg
agbefc aebc cbg cb dafgce deafbgc ebgdf ecfga cgfeb fgdbac | bdfge gefca dgefac ecbfg
cgb cbefgd debac ceadfbg bgceaf gbecd fgdeb cg dgfeab cfdg | fbeacg eafbgcd abcde bfcgdae
aedf fcged abfdcg fcadg ef bgcde efc dgacef fcgbea acebgfd | edbgc geacdf cagdfe bgcfea
gdeba bcafed efbacgd efdacg febadg eg gacdb fadeb bgef ega | bdgae cagbfde cdgba edfbac
gdcae dab bgac cegafdb ab ebafcd gbadce dcefag gfdeb degab | fedabc bcag adceg egbfd
fgcda bgedfa abec bcedfg dgeac dagbe cdeagb ecd ec cfegdba | ecd ce acgde aecdg
dfage becg dceabf beacgf eab acfdgbe gbdcaf bafge fgcab be | dfgcba gcfab eacbfd gaebf
dfabc ecgad fcdabe gfc bedfcg cdagbf dacgf bdgfeac bfag gf | dcfab fagb cgfda agbf
gefcd fad bgfea cgfdea da fbecgd bdgafc dgbfcea cdae edagf | cgfbda gefdc cdea adf
fg efgb fga bdgea adbegf cfeadg fbdag abgdce bdaefcg afbcd | bgfe gebf afgdcbe dbegaf
cagbe dbca gcedfa cga dabfcge ac dgbace fbage cdgeb edcfbg | adfecbg cagedb abcd gbdce
eabdf efb be efcda cabgfd gedfab cdefabg eagb cfedgb gdfba | aebdfcg gecbfd efb efb
ecd fadce acgfd cdgfeb ec edbafg dfcaeb fcegabd abdef ebca | fdeac adcfe dfgeabc dfacbe
egcadbf fca cafbd dceba fa agcfbe fgcdba defgcb afdg cgdfb | gfbcd efdbgc af caf
beca ea fbdegca cafdbg ecagf fae bfgaed efagbc gbafc ecdgf | aegfc adbegcf ea ebgfca
bgacfed gbc fbdeac gc bgfae adcg ecbad begac dabcge gfcdbe | gcb bceagd agcd daebc
ba fgbcead gbaefd ebcfag dagcfe dfeag bdcfg adeb gadfb afb | deba eadb gbefda aecdfg
ab cgfdea dgcbfa aedgc agb efbcadg bagced gbdfe gabde aecb | aefbdgc bfdcag egbda agb
gfcd fecdb fdbeag dcb dgefb ecfab bdecfag bgdeac cd cfgbde | cbaef ecgdbf gdfbe cd
bae dgeabc fdgae aedfb be gfbcad dbecaf cbfda gfcedab cfeb | gfdea fdabc abfcegd gfaed
cegfd dcefba daefc agdcfe cgebfda fg edgcb dgf debgfa cgfa | cfegd debcfa gfd edgfc
dceabg bgcfae fb cfgeb bfc dfceg bcfdea dcfbaeg acebg gfab | aegbdfc bcefg egbac egbfc
ceadgfb cfgadb eagfb eagbcd gebcfa ge age eabdf bacfg gecf | abdef ecagdb bgfea adgbfec
gdcaf egc cebfa cfdaeg cgdfbe eg eagfc gaed fabgced dbcgfa | afgce dcafbeg eadg dgfac
cfdbe dfcgeab ab abgdcf ageb cba eadgc cgadef cdaeb deabcg | efagdc cadeb ab eabg
bgfaedc gcdfe gfbdc fbga cbaedg dcagb gbdcaf bf bfd dacfbe | cgaebd fdgce bf fbdeca
gdabcf acgbefd egafc cda adbe gbadec fgdecb ad cdegb edgca | ecagd ad dcebg gbced
afdebc ebgcfda fabge ecfgdb caefdg dcbg cb fgecd gbcef cfb | eagfb fedgc ebgfc cbfedg
eb fgcade debgfc ebc gadec dfacb ecadb aegb ebcdga dagcfbe | efacbdg cdbaf fdcegb cbdage
gdefac gbdefca aec ce fecagb bfcgda gcafd dceg aefdb cdeaf | cbfaedg cdefag cgdfa cegd
bdegaf fda da ceda edfacg efbacg agdcf bfdgc faecg cgebafd | fdcga daf afd cdgbf
gec fbdgc abdceg febgdc gbacfed decgf facgdb fcbe fadge ec | agdfe cegdf gadcbef cfedg
fbeag bgcea cg cabfed agebcd acgd cadeb cge bgedfc cgaebfd | dfgbce efcbad cge bdcae
gdaef gbafdc gafdce fcadg gedc efbgdca de dfe baefg bcfdae | abcdfe gaefd cgfade dfe
gbcdf gcfa bgdaf dagbe dcegfb bfedca afb cgebdaf fa bafdcg | af fbegadc aegbd gfac
dbgacef ceagfb cfbd ebfdga bd edb bcged aecdg bcgfe fdbceg | db gbeafc befgad dfbegc
bdf bfadeg dbfgeac degcf aebf cgfadb gfdeb bf gdeab bgcdae | fb bgdaec gcfed feba
bgdeaf gbafe cdfeag gecfba debca adgcebf dfe df fdgb efabd | bfdeagc gdbaef fgeabc acdeb
edfcga fdab df fcbead bcagefd dbcage fgebc dcaeb befdc dfe | afbd caedb ebdac cdagef
dcbeg bcag bdaec fcdbeg adcef dfgaeb gcbaed ba begacfd bea | febgdc dbceg agecdbf dbceg
agbc eafbd dgcefb afdbg gaf gdecfa dfgbc fbgcad defcgba ag | edcgfb fadbg dfaeb abefd
abfdcge fadcge gde dcfgb cdgafb gbefd eg fcdgeb ecbg faebd | cgfdb ge dbgafc cegb
ab fbcge fegdbc gab fegad gebaf bgacef ceab fcdbega bcafdg | dgfbac gfacbe gba ab
bed fdage bcafge afebd cdegba fabce fdecab bd dfcb aebfcgd | dbe dgbeafc adebcgf eabfd
cbfdeg gbafe egf bdfgea dfea abceg ef gbdfa daecgfb bdgafc | edaf fabdge ebgac fgaeb
dgba bdcgaf bcdfg bg cgdfe cgb afcdeb bdfcega bdfca gcbeaf | aefdcb dbga cgfed cdfge
gefda gfbed cgfdbea feb dbcgf bgecfd becd fgabec be dfgbac | fgeda agcbdf bdce be
bcfge ab cagbed bea acdeg gdab aecbg fcegbad bcefda adfcge | bgadce dcgea febcg aeb
dagc dcefg gfcdaeb da fgecdb bfeac ead dgfcae eacdf begfda | bdeagf cfedgab dcbgaef ead
dg eadcb fcgd eafgbd fcdgbe bfegc gecbd deg dgcbafe cfegba | ebcad gecfbd ebgcd gfcbeda
bc bgc edbcfg gbafd ecgbfa ebdc gdcaef dfbecag bgfdc egfdc | bgfda fcdgb cgeafb fcbgd
abgf dbfcgae adegf gdabe cbdeg ab bae dgecfa caefdb aedbgf | daefg fbag badfce gbdefac
cabed gabcdf eb cegad fabcgde ebd ebaf fcdeba badfc egdbcf | dbeac cdafb edbfacg eb
degfac adgcf fbgade fae ef egcabfd egacb decf efagc agbcdf | ef fcadeg edcf cgafdb
adcefg agbefcd acdfe cdfba ecfdbg ed eagfc aged edf cegbfa | fcdea gbafec efd fecdgb
dfeacb fge fbgca gfcbe dbeg eg cdefb fbedgc fdbcaeg adecgf | gef dbeg efg becfgd
ae fbdegc gfcdae fbgda fecabd gfecd gdafe edgfcab afe acge | facged adcgfe cgae bacdgef
gfbac cfebda dfg degfca dfgbc cbgdfe dg dcefb edagcbf gbed | caedgfb gdf gd ecgdbf
gcdfea dafbcg gadfc aecdbf cab ba gcbaf abdg cebfgad ecbgf | bca gcfad dcfbage efbcg
gbceaf baegd gab ga agcd gecdb deabgc afedb agebcdf fdbgec | cdgeb gab eagfbc aebdg
ebagcd dgacef gbc gdefcba cbeagf bfadc bedg acdeg bgcda bg | aegdbfc cbg fagecbd agfedc
fcbage dfbage acdebgf ed faed bafge fdegb dge bfdcg egcabd | deg dgfbc bgdefca abcgef
aeg acegd degb dagfcb fadec bgeafc bfagcde eg bdecag gbdac | acefd defca degb cgeabd
bfce fadeg fegdb cdgeb cfdabge bf acdfgb bfd edbcga cebgfd | begadc edfcbag dcefbg bgfed
gc gfac fegcd fedcagb gaedbc cafed edbfg gedafc fecadb gec | adcfe cgdeba egfdc cfedg
egcfb dbac beafcdg cbgea fdeabg gadecb cdgaef cga gadbe ca | dfcabeg cbdgea ca egbca
edgbc fdeb dcfegb cgeafdb dfcaeg gebfc daebcg bfagc fe fec | bgdefc ebdgc ebfd dgbec
bgacde abfgecd caedg ba ebfdc ecfadg bagc eba dbeac gdebfa | cdefag bafedg agbc bcfed
febacg fegda gdbacf ceab cfa cbfeg fgabcde cgaef dgefbc ac | gbcfe beca deafg eagbfcd
debg fgbade gecabf dfaeg fdaegbc gad eagbf fedac dfabcg dg | egbacfd cfead fegadb gd
agfeb cf gcead fgcea gfcdba edagfc bcadeg dfec fca cdfabeg | geacf fbage gdfacb cf
dbfae geacfd eabgc cebfga gd cbgd dfbaecg edbgac adgbe gda | dag gd caefgd gfdceab
edabcg dgaf cgbfe bcdfa efdabc edcfgba ga cag dbgfca gacfb | fdcbag gac ag bdaecg
bf efb efcabd gfba agebdfc deafcg afegd gfeadb gebcd gdbef | bfga bfeadc bcegd fbe
edfgbc fgdc cf bgecf dcaefb efagdcb gdecb bfage cfe dcgeab | dfcg ebgdc fgdc aecbgd
bdgea dgebf ebcdaf ebf fe fabcgd gcfe bgedcf ecgdfab bgfdc | egfc gbcdef bfe fdbcg
ca gdbaef adecg acdfbe cefadg caedbgf afedg eac afcg ecdbg | eadgf dgefab dgeac gadce
dfgecb adcf becfadg cgfdea gfdae dcgae fd dgcaeb dfe egbaf | efcdbg caedfg eacdg dgeafc
agcbe fba dfag fgbca cdagbef fgdcb gcefdb acfdeb af dcbgfa | dcbafe afbcg bfa cdbfg
cead ce dcefga fce afgedb cfebagd agfebc fecgd dafeg cbgfd | fedagb cgbaef egcfba gadfec
dfebga bcadge gdfebac ed beagf eabdf gbafce gdef bdfca ade | ade abcdf bagcde fabcd
adg fgdbce abfgcd ecdgaf cebgdaf gcbad ga bgaf fcbgd edbac | beacd dag fgdcb gda
dbeacf edfcbga bgcde edbac gbac dgb dcegf bcgdea gdafeb bg | cbdge bg cbag ebcda
gafcd dcae cd cgaef bfedcga gfdab cbgfea cdgebf dagcef gdc | afcdg gcd gafdb bgcdaef
edabfg gecfdb adcf abf cfgbade gbace bcfed af bface febdac | eadcbf efabgd fa fba
af eaf daebg fgebac dfaeg geadcfb cfad fedgc dcefgb fadcge | afdge ecgfabd fa fa
dcfga agfdbc db gcbefad baefg fdb gefadc dgabf fgcbde bcad | gdcfa adgbf fbd cgfdeb
cbega fgb agfceb dfbcage dbfca efga dbgefc bcagf bedcag fg | dafbc dbcfa fdbcge ebdgac
eac cbaeg ce acbfg daefcg abgfde bgdeca dbgaecf ebagd cbde | abcedfg cae gbdae ec
gadef daebgfc fge beagd fcebad cbfgde gf afced gfdace agfc | bedfcg geadb gdefbc gf
adbfcg cdbge cfeg beagdfc cbegfd bfdaeg eacbd eg fbgcd egd | ge gdcfab cgebd dacbe
ca cbega abcefdg bfcdea bgdcea abc dgac ebdag ecfbg aebfgd | adgebf debagc bgcae bca
bg edcbg gbd dabegfc dcgae aefdgc cbga dbaegf eadgcb dbefc | cbga cgab adcgeb bgca
egbdf becfga gdcae cdgef cf cdgfea faegcbd dacf cgadeb fcg | cdfge gcdabe befagc cgf
fbade ca eca eabcd cefbag ebacdf fdbaeg cdgbe facd dbagfce | fcbgdae dbace debfgca cbade
fbdceg fgade egfcd fbdacg dgcbf cbed gec ec dgbacef ecagfb | aegdf egdaf gecdf feagd
aef bacedf aedcg bfge efabcg fcbagd fe gfbac fegca abdfceg | cgafb agcfdb gbcafe acfge
bgfc defgbc cf deagf gbcdea baedcf ecfgd ebgdc dcagfeb cdf | fgaed fedcab cdefg dfega
afecbgd bgefca eca gbcaf cgfe bgcdfa bceadf ec cabeg dgabe | ce efbcagd aec fceg
gaeb eafcd dbeafg egf dbfag adfbcg bfcaedg aefgd eg decgfb | fegbda bgfecd adgfe gef
cgb baecd begdc cbdefa adbg ebafgc gcfbead cgfde bgdcea gb | cfedg ebdca adbg eabcd
egafc ac fecdg acfgdb aceb befgcad eadgfb fbgcea cga ebfga | daecbgf bcea fegba ac
afde cefbgda gabdfe cegdab feb bdgae dbcgfe fbgca eabfg fe | dgeba dgcbfe bafge gaecbfd
ceabgd ba cfbagde afdce dba ebcdg abcg fdaegb adbce defcgb | fdgbcea bacg dba agcb
gbcfed eag dceabgf eagfdc febgc bfag eagbcf abdec ag cgeba | gae aeg gfdbace gbfa
gb gbfeda cbead gebdcaf gbdf cgabef beg fgaed gdabe cfgeda | gdbf bgfeac bcead cdefag
bcda fabceg dfecg eadgbc dcgae dfabeg afdgebc dga ad cgbea | dbecag efagdb da fcbeag
gfcdae cf fabgd cdfabe gcdfa fecabgd gdeca fegc dacgeb caf | fegc fca cf aefdbc
efcgb decfga cfbga bfdca gcfbde ag eagb gfa bgcaef gcbfdea | ga egba bfecg gfdcbe
cabedf cedf ace bdace acdgb afgbec fgdeba ce fadbe afgedcb | aebfd ebdacf ace dcef
fecdg cgdfae gecbfa de cagdefb bfdgc egda gacfe ecd cdafbe | dcgbf fgaec facdeg gfcdb
gabfdec dabfge abdeg fa egcbda fdbgac adf efbad efcbd fage | gdabe bgade bfdega afd
cbgad fcabdg cbfdg cgfaed fdg bafg cdabge bfcde fg abfcdeg | bfdaecg cgadeb defcb gdfabc
defba fgae daegbf adfgbc bfadg fegacdb bef bedca ef fbgecd | befda bcegdfa ef badgf
fdgbea agfdcbe bgfeca dag da eacd dagcb becadg fgbdc acegb | ecda adbfeg bgedca cead
gafd cdbaef dbgcf gdeacbf fdacb bcdagf abgdce gd efgcb dbg | gd gbd egfbc gadf
de abdegf edgfb efd gcfedab dbgaf afcdbg aedb fegadc begfc | fde edf ecfgb edba
cbgefd da bdagcf cda cegfabd gfdcb dabcf cfgead dgba acbef | bdfcg bgcafde dbcfga bafdcg
aebfcg gfedabc eadgb bf cafgdb bfg fbce fgcea gcdfae gebaf | afgebc fb fb bgf
gc fgecbd egc gaebf gecbda acbed fcaebd cagd bcgae gfabdce | gacd egbacd abefg egfba
dcfab bgdc efagd acg gc aedcfb cdafg begcfad dabgcf cbefga | cdagf gcfda dbfac cdgb
bcdfea dgce cadfg dae agfde cgdeaf adfegcb bafge ed cdbfga | cgedaf bgfae gafdc fbgae
adgcfe dfagecb gfbcda ba cafegb fbdce eagb agefc cafbe cab | bcedf bac ba fbcde
bfeacd fcdebg fg dbaeg egdaf dcgfae gcfa efadc bcegdfa fge | acdef fgaed gf bdeag
dcgefa ceabfg fagbc beagd df fagcdeb dbafg fgabcd dcfb fgd | egafdc gabde beagd dfbc
ef bgdaefc cagdbe fbcda fgbade aef gfce eagbcf gcabe cfbea | fcdab fgce bcfad acebf
efagbc fbdag afdcb fg bgf begda aedfbcg deafgb gedf cdgbea | dfgab acfdb gf gf
edcagb ecfbadg ebdfg ega fbaedc fecdag ga dacef gfac gaedf | dgbfe ga dcafe ag
bedfacg acbfge cbg badfeg begfc fbcde ecag facdgb gbfea gc | fbced gcb eagc agebdfc
fdgca fbgce gfecd gdeb cdfagbe ed ecd fedacb gfabce dgfbec | afcgd deabfc dfagc begcfa
bfeg dge cgadf cgedf gedfbca cebfad dfebc cdabge cdgefb ge | dbcef ged eg ebfacd
ed cebgadf gefdca cfega dcaegb dfce cgaefb dge gbfda dfaeg | aegdf afgce egd deg
aecfgd fgd cedbag fgecd bfagdc feacbdg gceda gf bcdef eagf | dfbce agced gf geadc
gbcfdae fdgea bdgcae edb edgbfc fagdcb ebca be ebgda adgcb | be edb bed gcbaed
gdef df afdgcb fdb ebcgd begadfc gdcbef efbdc baegdc fabec | dgceab acbef fbedc cgbed
dcbgf feg edfacgb efgadc eg fdace afbced fcged eacg aefbdg | cbaedfg ecag aecfdb efdacg
fdbega gcebaf dacgeb eabdc gabcfde fcdea gcbd bd adb agbce | afced efabdcg afced bda
cd dafcbe adegcb edabfgc bdcga adfgb gabecf cdb egcd cbega | daegbfc afbdg cd debacg
bga becfdg gcbfad gcbdf ag baefcg eadbc dcgab fcgadeb afdg | gfbcead edgcbf gdcab bag
aedgfbc fe cgdaef fbec feg agecb bgadf bacefg afebg cgdeba | aebgcfd gfe aegbf efbcga
afcegd gbdec acd afcgbe ad cagfe gdcea fgbedac edaf dgbacf | cad dagec aecgdf da
gfedc eagcfd becagf cafed ace edbaf dgac feadbcg ca bfecdg | gdac gcadef ac eafdc
gab abfdeg bgdac fadbc gcdfba bfdeac bcfadeg bgfc bg gaedc | bg fcbad gb bg
bgeda agdfcb gdaec cd fcabedg cgabed fgcae bdefag dca cbde | adc baegd febdag eafgbd
gedbca dacbe efda bfd efdcba fd bfacd cgabf cbedfga bcefgd | bafcg baecd fecbgd cagfb
egdc fed ed bgfced bfacd ebagcdf dbefc ebgdfa agbefc cfgeb | bfegcd cfebd agdcbef cafdb
bdg bdfga gdfca dfbae gbafcd fbgc eacbdg aegfcd bg dgceafb | gb bgd gfacde gbcf
egdbc badfec caebg cegdab ed bcdfg aedg afdcegb gfaceb bde | agde adge de egda
adef gdcfba adgbecf af acegb fab bfadeg bfega dbgfec efdbg | bcgea fdcegb degfcb fbedag
gbfe bcdgaf edfag gae gfdeba cfdbeag fgdba gebcad ge cdfea | cgfadbe bgef agcbed cgdfab
dfeca cdbg edafbg gcfbead bcafg cfgadb fgd bfgace dg cgadf | gfcda bgfadc cbgefa gfd
fbaecd bf dacfebg dcfge ecbgf gabf cegfab caebg bfc gdbace | abcge cegab egfdabc dcfeg
cbdfag fgbec gdabfe gfdbaec bdgfc ef fbcdge fbe agceb fced | gbfdc fe cegab cgebf
fdebc adfgcbe acebf fdac df dfb bdaegf abecdf abcegf bgedc | gbcefa ecbgd bafec dbecg
fadc fgc gbcdf bcdga edgfb cegbaf fgbdca fc debgca dafcgeb | dfcbag cabdg fbcgd afcd
dgcef dcgbef afc adfce feagbdc ac deafb cdag gcdfea befagc | ca cfegdab efcda fdeca
febgad ag gdeab afdbecg dcgfbe agfbec fbegd gfad aeg dcaeb | ebgdf abedc dgaf dfbge
daegcb agbcd edg bafdgc faecg gadec bfcgde abde ed fecagdb | baegcd edba efgac cefag
adbcf gcaf bgdac gecdfba efdba fdbcga fecgbd cfd cf adgceb | dcbag abdfc fcd fcd
cfedb cfa acefbd afbdcg edcbafg bfeag acebf ac ecdbgf adec | fedcb edcfbg bfaec gecfabd
cef befdcg dafeb cefdag fbdaceg fadec fc agced bdgace acfg | agcde daefc afcg cgaf
ebagd gd gfdbace fagbde febad gfdb dcebfa dcefag edg gcbea | dageb dg gde gd
de gdecfb baefg fedbg ecdf bacdgf bgfdaec dge acdegb cgdfb | dagfcb dcfe ed de
adgfc ad dcea caefgd cgfdb bfaegc afgec egafdb fegbdca fda | da gfedab ad afd
abf cdgfae acfbg gecbadf ecba efabdg cabgfe gbcfd efagc ab | acgbf fegacd acegf abf
cgbaefd cdage dgef geacdf agfdcb dacfg gcaeb cdefba ed ead | abfgdc abdgfc dagcfeb baceg
fgdec cbfa bf gbcfeda dfb fadbge acdeb dbfce cgbdea cdefab | cfegd fdeabc badecfg dgcfe
eba dacgefb deacb gabfde ecgadb cbdef cage gcbad afbdcg ae | gdfeba ae dfbec agce
daebc gcdeabf bgcfea agfeb bdega dgbeaf dgfa gdb gd bfcged | febdcg egfdcb bdace gdfa
gcbaef fdgbea adgfce bgefacd fdbg egdab daecb gbe bg gfdea | fbdg dfbg aefdg bdfg
dbgfce ec faebg fec fagdc cefga fgecda ebfgcad cdae dgbcfa | cadgf dgbeafc cgeafd ec
gabcef edacbfg dba agecb bdce dacgbf dgcaeb eafdg gadbe bd | dfgea db adb cbdgaf
cbfdgea fbdgca bedagf ae dae bfedca dacfb feca bcged adceb | ea ae fcea ead
daebgc db bagecdf bdaec fgdbea dfeac bgfeca cgdb bed gbaec | dfecbag ebacd bed db
agdcef fdaeg fbcdaeg cbgdae afgbd fea cfde eagcfb gdace ef | bgecda afe defc daegf
dcaegbf fdgeba ad dbafe defbc faebg dacfbg daf egda cfaegb | abedf bfdaeg dbfgea cfagbed
geb bgafecd bcae fgabce aefcg be gadfec gfebc bgdfc gbfeda | ebac be fgace dfebagc
cdfegba eafdc afcgb abcfge bfgd cfagbd adg dcafg cebdga gd | adbecfg cfagb dabgce cafbg
geaf fg acbgd fgd dgfcbae dbaef fgbda ebgdfa egfcbd acefbd | gcdba edfbgc ebcadf gf
cbgfe egac ce ecf bgfeca cbdgf fgbdea fbgceda geafb eafdcb | gecbaf adebcf efc ecf
fcd gfbcd dfbge dc gcbefad dcag afebdc gfbac efabgc fadbcg | bgdfc bcfga bfgdc cd
dg dgeb acedbg bdfagc faceg cfagdbe ebadc daceg afbdce cdg | gdaec afgec gcd eacdb
badeg dfegab efgadcb ad fgcdbe egbdf gcdeaf gad dafb bgcea | dgaeb bgdfe agdbe fbaged
deag ea gabedcf decfb dcfea fcdgae fgcda dgcfab cegabf fae | dcagfbe gafcde fedbc fdbacg
eacfg bged cgdafb dg cegad dgcbea bceadf adg gbdfcea baedc | bcfadg gcfae gbed eabdgc
efdba cegafb bcgdfae gdabe cefbd aef af ebgcdf ebdcfa facd | fdabec gfbeca fea adfc
ab fdeba cgefbd fdgae bea fbdce bdafce egfacb ecbdagf dabc | eba gdafe bdaef gfbeca
feadcb dgfabec gdafb cb ebgc fdcgbe cagefd dfgcb fgedc bcd | gfcde gdcfb fgbced decbfa
fabcde cae ecbfga agbed fbacg cegf cgdabef dbagfc ce acbeg | gbdea gcebfa fdeabc baceg
gfebca cageb dbfcaeg ed aecd edg bdegca ebgafd fcbgd gbdec | fbacge dcebg acde edca
bfgedca ec gacfd gcafbe decb cef gdafeb afebdc efadc dfbea | bdec cfeabd cagfd feadc

86
8/solution.hs Normal file
View file

@ -0,0 +1,86 @@
import Data.List
import Data.List.Split
import Debug.Trace
main :: IO ()
main = do
input <- readFile "8/input.txt"
let x = map parse $ lines input
-- Problem 1
print $ f x
-- Problem 2
print $ g x
where
f = length . filter (`elem` [2, 3, 4, 7]) . map length . (>>= snd)
g x = sum $ zipWith (curry decode) (map snd x) (map deduce x)
-- | Parse a line to a tuple of signal values and output values.
-- | The signal values and output values are sorted.
parse :: String -> (Signal, Output)
parse s = let (a, b) = break (== '|') s
signal = map sort $ filter (/= "|") $ words a
output = map sort $ filter (/= "|") $ words b in
(signal, output)
type Signal = [String]
type Output = [String]
type Mapping = [Result]
data Result = Missing String
| Matched Int String
deriving
Show
-- | Extract the segment combination belonging to the given number.
get :: Mapping -> Int -> String
get m i = head [ s | Matched n s <- m, n == i ]
-- | Extract the number belonging to the given segment combination.
seg :: Mapping -> String -> Int
seg m v = head [ n | Matched n s <- m, s == v ]
-- | Deduce what segments correspond to which number. The number
-- | the combination belongs to is its index.
deduce :: (Signal, Output) -> Mapping
deduce (signal, output) = foldl match (map Missing signal) [ 1, 7, 4, 8, 9, 6, 0, 5, 3, 2 ]
-- | Use the given `Mapping` to match an unmatched set of segments
-- | to the given number.
match :: Mapping -> Int -> Mapping
match m n = match' n pred <$> m
where
pred = matcher n m
match' _ _ v @ (Matched _ _) = v
match' n condition (Missing a)
| condition a = Matched n a
| otherwise = Missing a
-- | Map a number to a function that can be used to match that number.
matcher :: Int -> Mapping -> (String -> Bool)
matcher 0 = p
where p _ s = length s == 6
matcher 1 = p
where p _ s = length s == 2
matcher 2 = p
where p _ _ = True
matcher 3 = p
where p m s = s == (s `intersect` get m 9)
matcher 4 = p
where p _ s = length s == 4
matcher 5 = p
where p m s = s == (s `intersect` get m 6)
matcher 6 = p
where p m s = length s == 6 && get m 1 /= (get m 1 `intersect` s)
matcher 7 = p
where p _ s = length s == 3
matcher 8 = p
where p _ s = length s == 7
matcher 9 = p
where p m s = get m 4 == get m 4 `intersect` s
-- | Given a list of output values and a segment mapping,
-- | decode what the output reads.
decode :: (Output, Mapping) -> Int
decode (output, mapping) = read (concatMap (show . seg mapping) output)

5
9/example.txt Normal file
View file

@ -0,0 +1,5 @@
2199943210
3987894921
9856789892
8767896789
9899965678

100
9/input.txt Normal file
View file

@ -0,0 +1,100 @@
5434456789899876598943212349988734679896554212468998765634987654567895454569876745567898765987654678
4421345997789997987894103458976521398765432101457897664529876543988987353478965434456799974398766789
3210237896698999876789214567895320129878543212346987543212989432499976212389876521234589867219878999
8732456934567987655678925679976543234999657323657898952101296521276895423467987420145678954323989987
7545678923979996544367899899997665379898775454767899843212398730145789439999876521266789765439896895
8656989019899985431257912978989876456789896569898998754324579821234679598787987632348999976598765434
9767893198769876542349943969878987567891987678959799867465679932348789987656899973467898899989754325
9899954599356987653567899858767898998910198789345689879876898655459892199767898765989987678978965434
0949765983245898974567986745656789429963239891256998989987998798767891019898999899899876569867896645
1239899874123798765678965432345678998754356999969897896998919999898942129919899989798765432956789896
2456999763235679976789876521012899569896567987898776645789423998989763298901698978679654321987899987
3567998654345789697894997863246789345989679976897655434678999887678954397992497761598767210198989999
4568998767658994598932987654959896559878998965689543212789989756545895986789986550349989321239878934
5679109978767893459891098799898998698766987654599652101299876542136789975498765431256999932349867995
6789299989878932398789129988797789797654598543688943212389987651012898996329765432367899893998656789
7898989899989321239679939878646678976543987654567894345678999954123456789319876783456987679899543454
8987674789996510398567898965434567895432398769878997656789298893256567892101989865597976545788932123
9876543668897932987678987654325478999421239878989989897892196799347878943212399876789876434567893015
6976542456789893498789798843412345678910129989095678998943985978958989954323498989899854323479992134
5988764567898789999895699752103456989321298792134578949959874567899699895434597898998769436589789645
3299985998976679891934987653212367895433397654236689939898765878934598789545986567899898998995679756
4399876789765456790949998654354567976564579864346789795789876989423987678959876456789987999754598967
5989987898874345689898998765667678987678679875467997674898997893219876579898965345699876899643457999
9874399987654234889767249879799899398788799986578998543456789979101985498787976458798765678952348688
9965456798982124678954123998989921299899899897789019432349892356919876987656897569987654569861234567
9878767899971012567893299897678930987943945798899929656478901237895988995434789978999543498990195688
4989879998753223456789987684567899876542134689967898767567892348954599986523678989898921987989989789
3292998987654345567899876543454979987698745798756789888678943456795678987434567898797990196578878990
5101997699865456878999987952123567898987656987645999999799764789989799398646678987686789987458767891
3219876439876767989789854321018798909998789398739898998999875699978989109798789999575679654345656789
4923987421988878994698765432134689212999899219898787887689987898967878919899899998434598743212345699
9894996210199989543569876987656789329896998998997645986567898987657567894902999896565679754103456999
8789765434567896532979987899769895498785987987987432345457899996543456793219899789678998765214667898
7679987545698987649899998959878999597654596576796541012345678987532345789398798698989439886356778987
6567899756789498998787899543999788999869989435987782134456989997431234678987654587892129987487899876
1456789987891349987656987682101677896998764324598943245667892986520345679976743476789098987598989988
2367891098942998876435699899323456795109875455799876368998901975421234567895432125678987798679769899
3458932989959897652123799998754789894298989569899965467999219876532345689984321014789976689789543767
6567899876798789943254889879875678989997898698999876567894334998765456796975432423899865579899632156
7678999865987656798765679765996899569876679987987987678999549769976789895496676534998784465978943234
8789489954698747899898789894597943456965569876756798989998998756987899932398987545797653234567894965
9896567893497656789989899989698952349893457954348899999867897645698998651239998676789543123456789896
4989678912399788997678969878999876599789569943235978987656798434569679962345999987898654234567898797
3479989101989899234579953967899997987679979754101567896546795320197599843469899998939764346678999689
2567893219876920123498842545798798996589898765243489998657896431989987654598788999323976487989898567
1457954998985431245987721234989659987456799876756678998798986549879999867998687896534987578998766456
0345699877896545679876510129878943298589890987987899989899598698768899979896556789649898789789654233
1258789565987656798765431298967892129678992999398987667965498797658789998787445679998769897698763101
3879895434598767899876532987656789098789979893219898548994349988546698987674324567898756989549874222
4989999323569899987989749876545699989898767689456798656789239975234567896532014687996545678934965434
5699888912479959876599898765434598878989954597997959897891098754123458976432123456789436789029896945
6789767893569542987456999876523987654567893456789542998943987653012369897643234797897647892198789896
7996556789698959898968998765219876543478932345698951019995698432123456797654365698998758943987668689
8954345678987998759979897654327989657899321234567892129889997643264567899895776789019869959876547567
9543287567896789543499789876456798798999872345678963498769898965459678999976798997998999898954323458
8762123456795678932987678976578979899698765456789954989656789878678989689987899756987889767895444767
6571012387894239321998489987678967989459889578997899876545995989789996577998987645986563456976655678
8432123456943145939876569998989345678967998789756798765634894399898895456879995429876432356897779899
7543234579432019899998678989492235678979679897645987654329795210956789327467896539994321237998989931
8654345678973298799659989976320136789989536965534598965418689632345893214357987698789435398949995432
9765566899954989578945698765431246789997645984323459876324596543456789301236798797698945459539876543
9878987899869876457936789987543657894987659876775767988765797786569895212545679987567896579321997654
9989398998998765346897894697654769923498767987899878999896789897678954323598789498678987678932398895
9899459987679976456789923498767878939999989998910989999987894998789765499989892349989698989993979976
8798967943568989769893212459878989998791299879322398989998943769999876987878991234596439498789765987
7667899652456899878976493967989399876689399965443987878999542356789989776456789949697321334599654598
6545678943597956989987989898996598985545989987559876769897669467894597654345699898989210123498963239
9436789894989439997899876789987987694234567897698765458789798978932987543248998767678933234987892134
8997898789878998876799985689998975543123458998789987345678997989321997654367999856577894549876789045
6889997679767897545989654567899984321012567899898753234789986899439898765456897845456789698765692156
5679986545656989439876543678920995443227679998999864565699875778999789996567986434345698799754589267
4798765434745778924987762399439876654535789587998765699789564567987678987698954323236789988643678979
3987654321434567995699843489597987765678996476899876789895423459876569299899875210127999876512889989
2398776410123456789987654578976598978789875345789987899964312346975489101999986321239899974323690198
1459898531234587898798767678965439989999954234598798979975423769876594312398765432349798765434569297
2699876542346678997649878789432123499999876345987659567897569878987694324569898543498659896565678956
9989987843656789989432989896543034578989985456798741456898689989299789455689987654987545987876789245
8978998654567897678921299987652145989979876787899432346789799892109896596798998965696534698987992123
7767899765678976567932349998543267898765998998989753456899896789512987989987899878987623499698993434
6456789878789995478945678997654379987654989999978999567899945695423499879876789989998834988549889645
2348995999998989599656789998765459998769877898765688979998956789534598766545679199876549876534678956
1256893212987876989767899869876567899898756987654567999986899899976987655326989298987698765423459867
2367999323986765878998987656987878998999967898323457899875798999897898543201399987598789879210678978
3478998939765654569999896545698989667989899999212345899964567898799939954912568898449899984321289989
4989997898754343456789765434349893459879798987463456789643456789688929899893456789234989965434567896
5678976798773212345899984321256789599965667896554569896532567894567998789789598894345679876565778965
6899995987654301236789876532367899987654356987789678997321013789679876545699679965956789987876789954
8987989998765312447897987688456789998543234598898789398432123678989999433488989989897999998998899895
9766767989876493558965499786567896987652105679999891298743454569999878521367893198769899899899966789
7654555679997989967894329897898945698543212589899999987655677678998765310456789976556789765732455894
6553334899989879898965469989999334987654323456789678998766788889429896321367899896345678954621234932
5432124578978956789876598976989219999768454579894599659877899995434975433698998765234579543210129891
6543023569864345699997986745878998789879767699923589547998998989565986844589987654357897654521656790
9632123479983234567898965434567899667989888789013478936669987879976987865679999866468999865632347891
8743244569874347689999874323456965456795999993123569425459986567899898979789899998567899876774458942
8654456798765456790198765201597896568954987654256989312398765458968799989897789219778945988765667899
9987587899976569891239877332387899878943298965345993201239954349355689997956678909899434199876789978
1098788998988789932545976545456956989652129876556789713498765693234598766434567899974321019998893459
3129999987699896543489988758969139898761034987679899654569876789345679654323489998765432198779902345
4534567898799987654578999767878956789432156798789998775678987895468798765434591249976545698654323456

70
9/solution.hs Normal file
View file

@ -0,0 +1,70 @@
import Data.List
import Data.List.Split
import Data.Maybe
main :: IO ()
main = do
input <- readFile "9/input.txt"
let x = parse $ lines input
-- Problem 1
print $ f x
-- Problem 2
print $ g x
where
f = risk
g = solve
parse :: [String] -> [[Int]]
parse = map (map read . chunksOf 1)
risk :: [[Int]] -> Int
risk a = sum $ map succ $ mapMaybe index (areas a)
where
index = ($ a) . uncurry idx
solve :: [[Int]] -> Int
solve x = product $ take 3 $ reverse $ sort $ map (basin x) $ areas x
-- | Find risk areas (low points).
areas :: [[Int]] -> [(Int,Int)]
areas a = [ (x,y) | y <- [ 0 .. (pred $ length a) ],
x <- [ 0 .. (length (a !! y)) ],
Just n <- [idx x y a],
all ((> n) . snd) (adj x y a) ]
-- | Get the size of a basin centered on the given point.
basin :: [[Int]] -> (Int, Int) -> Int
basin m (x,y) = length a
where
a = case idx x y m of
Just v -> let t = ((x,y),v) in t : search t m
Nothing -> []
-- | Search for neighbors of the given node where the value of the neighbor
-- | is not 9 and is greater than the value of the given node. This search
-- | is then repeated for each result it finds until no nodes satisfying the
-- | constraint can be found in the immediate neighborhood of the cluster.
search :: ((Int, Int), Int) -> [[Int]] -> [((Int, Int), Int)]
search t @ ((x,y),v) b = nub (next ++ (next >>= (`search` b)))
where
neighbors = adj x y b
next = nub $ filter (\(_,n) -> n /= 9 && n > v) neighbors
-- | Find elements that are directly adjacent to a point in a matrix.
adj :: Int -> Int -> [[a]] -> [((Int, Int), a)]
adj x y m = [ ((a, b), v) | a <- [pred x .. succ x],
b <- [pred y .. succ y],
x /= a || y /= b,
y == b || x == a,
Just v <- [idx a b m] ]
-- | Index into a matrix safely. If out of bounds on either axis,
-- | returns `Nothing`.
idx :: Int -> Int -> [[a]] -> Maybe a
idx x y m = get y m >>= get x
-- | Index into a list safely. Returns `Nothing` if out of bounds.
get :: Int -> [a] -> Maybe a
get i l | i < 0 = Nothing
| i >= length l = Nothing
| otherwise = Just (l !! i)