diff --git a/2021/day08/Cargo.lock b/2021/day08/Cargo.lock new file mode 100644 index 0000000..1d4626c --- /dev/null +++ b/2021/day08/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day08" +version = "0.1.0" diff --git a/2021/day08/Cargo.toml b/2021/day08/Cargo.toml new file mode 100644 index 0000000..7156aba --- /dev/null +++ b/2021/day08/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day08" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/day08/README.md b/2021/day08/README.md new file mode 100644 index 0000000..c0ff89e --- /dev/null +++ b/2021/day08/README.md @@ -0,0 +1,124 @@ +https://adventofcode.com/2021/day/8 + +## \--- 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](https://en.wikipedia.org/wiki/Seven-segment_display) 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`: + +[code] + + 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_ + +[/code] + +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: + +[code] + + acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | + cdfeb fcadb cdfeb cdbaf +[/code] + +(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: + +[code] + + 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 + +[/code] + +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?_ + diff --git a/2021/day08/input/example0 b/2021/day08/input/example0 new file mode 100644 index 0000000..d479e6c --- /dev/null +++ b/2021/day08/input/example0 @@ -0,0 +1,17 @@ + 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_ diff --git a/2021/day08/input/example1 b/2021/day08/input/example1 new file mode 100644 index 0000000..b383a2e --- /dev/null +++ b/2021/day08/input/example1 @@ -0,0 +1,2 @@ +acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | +cdfeb fcadb cdfeb cdbaf diff --git a/2021/day08/input/example2 b/2021/day08/input/example2 new file mode 100644 index 0000000..4bed913 --- /dev/null +++ b/2021/day08/input/example2 @@ -0,0 +1,20 @@ +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 diff --git a/2021/day08/input/input b/2021/day08/input/input new file mode 100644 index 0000000..67e40ef --- /dev/null +++ b/2021/day08/input/input @@ -0,0 +1,200 @@ +dcga cadgbfe gecba cbfde eda cdbea gbadfe fegcba bedgca da | bgefdac bdace ad agcd +fe ecf fdbagec dcgfab defbca efbcga daceg cfdea bfed fdbca | aefdc fbde abdefc dcgae +fbg cgafe bf bfdc ebdcag fgcdba gbdecaf bcfag badgc gdefab | bfg bf bf bdgeca +bgedf gecabd cd cadbgf agcbe aedcgbf ecgbd ecda agfecb gcd | bdacfge cd cade dcae +cedabf gcfab cad abegcd fcbedag aedbf gbedaf dc ecdf bdfca | cad dc dc bedacfg +ed gfdcbae acbdfg ecgd cdfeag fbdage dfcae fbaec eda gdacf | dae fadgbc ecgd aedcgf +dgb gb dfaeb beadg cdaeg bgdfae bafg cdgfeb aebfcd gcaebdf | aefcbd bgaf afgb dgb +fdabc ef cfaed daebgc fgebda fde dceag gfce cbgfdea eacfgd | adceg edbcagf edf fe +dagebf bdc bc afecd daceb cgbe dabceg edbag dfcagb cefgbad | dbc dabge abdgce cegb +dfgeb ebgfc dcabef gcab bc gaecbf cdfega cgfae bce efabdcg | cgfae cb gcdebaf efgca +afc gcdeab gdbfac bceafd becad ebaf fa faecd gfadebc cedfg | cgdfe afc acf af +dcgba gfabdc edgbfa gfdab bdc acfegdb bc adgec bgfc eadbcf | fdaegbc gfbc bdc cb +adg gbfac acdfbge fedca cdgfa ecafbg aegbfd gd cbdg gdfabc | bdfgac fcabedg agd dfbgcea +fdcgba bfc cfadb aebfd acgb cafgd fgdceb gcdaef cagbedf cb | bcag dfeba agbc cbag +gedaf afbcdeg ecgadb cbgafe dc efgcd dce cgefb cgdbef fbdc | bcgef efgabc dc degfcb +abdgcf abefc fd gfbacde gcedb gedf cfd edgacb fegbcd febdc | cefdabg bacef fcd fegd +faegbc dfcabg afgbed daebc eg gfec afcbg acedgbf ecgba egb | eg eg bgadfce baegc +edfba bcgafd edbg bgadf ed adefcg gfebda ecagdbf efabc edf | daegbf ebfad ceafb bgde +fcebad gcfebd agcdbfe daefgc gdabf cdg becg cfbde fgbcd gc | cg gafdbce cbdgf gfdab +bfceadg dfe geadb cgfd edfgb gdcfeb cabgef fd cfgbe aefdcb | cgdf df agbde edgcbf +age becfad cfbeg eagdfb dfecgab acfeg efdac dgac cgadfe ga | gfeca aecgfd eagcf fadcbeg +gabed efabgd egafcd afgecbd adefb bafcd gbedac febg fe fea | gfeb fe badge adfeb +edcgf adgf afgcbe ecfbgd dcegaf af dacbe efacd afc cafdgbe | fa edbcgf dcgfe adecb +dgabfc dbagc cbdgea ge decagf geba gcbfdae dge bdgec ecfdb | abcgd dcabg dgbafc eg +gadfe fgbcde afgdbc ecfb cf edgcb defcg dbagce fcbgdea dfc | dbgec adgbcf dcefg fc +gfeda abf bgfd ebdfgca gaefb debafg fbeacd fb gcdafe aebgc | gdfb fdbcaeg gdfb bf +ea eab ceabg deac daebcg gedafb bdagcfe efdcbg cgbfa bgced | gfaebdc acde caed dbafge +eg cafde gdfe aedcgf fagbcde baefcg adcgb eag cfdeab acdge | bfdcaeg eg ega ge +ed dcgbe fbcadg gde bcagd gdbace dbea eacfdg feacbdg bfgec | ged gbadfec cdeafgb acbfged +gcbdfe bead da ebfdc adebcf ecfdbga dcfbag cad decaf eafcg | egdcbf dac bcgfde cbgedfa +cgafedb cabfdg deagf gacdeb eb cbdaf bdafe aeb bcef afcebd | becf dagef befc dbfae +ad gdebcf dabcgf afbce dbga ecdafg adc bgcfd cadbf decagbf | da fdcab cda dac +cebfd ab cebadg fadgec agdb eab caged bcdea fegbac dbcfega | bae ebdfc ebgcad bdefcga +adegcfb edfba abdgef afcbd dbagc edfc efbdca fc fac abgcef | bdeaf gebfadc faecgb dabfe +bgfecda dbceaf gabedf gdf fg cgeda bgef cfdgab fdage aebfd | badfe egbf efgb gfecbda +deafgb bcdge df dacf gfacb cfagbe bagdcf gcbdf ecabgdf dfg | fbdgca dgf dfg gfadecb +dbcafe gebcfad fagcdb gecfb cgdeb adeg cbeagd cdaeb cdg gd | edfacb gecbf dcg dcbafe +facdeg cbefgd bed cdebfa egfb gfecd ecdbgfa be dgbac ebcgd | ebfg cebdaf cabegfd gfecd +bacfdg cdbaegf caed fcbge cgebad gbafde cgbda egcab ae aeb | gaceb edfabg cdfbag cfbeg +adfgc fgecad gdabf acbg gdafcb fcdabeg gdb gb dgecbf ebfad | abcdgfe gcba fdabe gb +dfbc df adecg def dfeca cafbge eafdbg bdcgafe cbefda bfcea | adefc df efcda aecgbfd +aeg gefcabd agdcb dfgcba egbdfa bfeca edcg ge bacge cegdba | gae cdbga gea eg +cdegb bfgc bgd ecdag dgbfae edcfb bg dafbce edfcbag dcfgeb | fcgb efgcdb bcdeg gb +gdef dbafceg cdbeag ade gfcae fdabc de fedac efcbga ecgdaf | aed dea gface agebdc +gdeac ab bga baef eacgb gbcafe bcgef bedcfg dgfbca gacedbf | gab ebcgfd fgdeabc ab +efa ebagd debagf gefb fe ebdagc gfdea egfcbda dgacf dbfcae | afgdc gfbe efa bdegca +daefgbc afecgb fd cfbdg fdg abcfg cgebd abgefd fcadbg dcfa | dgf df afcd dfg +agf dgfcea ecfg bdfgaec fg dfaecb abgcfd dfgea baged acdfe | fg efcg fag dafeg +aedcfg gfcae cdebg gab ab abegc bfae aegcfdb bfaecg dbfcga | dbgfac dbgce ceagfdb ba +bd afegcb bedcfa adb dgecabf fdeb bfeca acdbf fcadg bcagde | ebcgfda db ecdabf daebgc +bdf fb dbefcga edfgc fbga edabg fabecd edfbag gefbd acbdeg | cedgf edbacgf fbag gdbea +bfc cfeabd gfaeb cgbfe fcdge efcagd bdcg bgadfce bc dcfgbe | cdgbfe gcbd cb cfb +gfbde fdgec efagcd ebcg ebd eb gdfba ecbfgd deabgcf becdfa | gcdfbea gecb febcgd gbce +acf cbgfad gabecd bcfd bdagfce aebgf fagcb cadgef dagbc cf | dbcf afgcedb edcfag cfbd +fbcag ec gacfeb efgc ecb gdfaebc aebdf gbecad dacfbg cafeb | ce cgfab cgdfba cbe +abdcgfe gfadce fcgbe fgadbc gcb gb debg gdecf bceaf bcdgfe | gb dgecfb gaefcd bcg +fdbca adbfe dabfgc ebcdaf efagb dcefga de becd dae bafcedg | cedabgf de de ed +ge bcgfa eacdgbf aegc efg cagfbd fgcbe dbfaeg aecgbf bcedf | ecga ecga gfe debfacg +geda gfaeb agcdebf agcfb cgfdbe ebcadf gfe fbdeag efdba ge | dgea egad eg aegd +bgfecd cdegb ecgdba bagce eag dcfegab dfceag ae cbagf bdae | fdbecg abcfg gbcfa ae +adfbc fdbaeg bce bcaegd gbdcfea adceb ec dcegbf aceg daegb | ce dgbaefc bdcae fgcdbae +faedb bef fdcbae cfbd ecgadfb bf gabefc fegad bcdgae edbca | bf cebdfag ebdfca fb +badfg acdfgb dbgae fegbdc agfe afcedgb aebcd eg edg egfadb | eafg eg gdcbaf cdabfg +gacdf cegfd acdegf dbgfec ad fbgac dgfaecb dgea bafced afd | agbdefc agde ad da +cfbdg fcadgb fbg gcab fbcde gb degbfa dcfag edgcaf cdbgfae | fdbcaeg bagc bdegaf cfdbg +fgeab abegc afdecb cdgbef fg gfda beadf gdefab bdaefgc gef | feg fg bgfcde dfga +fgcab becdfg dfec dcabeg daebgf gbfcd fd dgebc ebcfdga dfg | eadfbg edfc ecdgb fecd +gfcd fbaeg df begdf fdb gcedb adbgec fdcbge cbfdea acfgbed | gbcaed cbged degfcba dfb +adgfb eagf bfade dgbaef bcdgf dga bcfdae ag gecadb edbgfca | ga abgefd ga geaf +cdg bfcdeg dagbc dc eacgb ceafdbg abgfd gefabd cfdbag cadf | bdacg gcd acdf dcfa +caefd dg efbgca gfaebdc bdeacg acegd debg fdcbag cbeag adg | fdcea gd degb dag +gfd cfbeag dabfe dgce dbfge fbgce cfagdb bcgefd bgecadf gd | gd ecgd dfbea cfedagb +cgabe dbefga egdfbac bdcfe acgd ebcdg gbaecd gdb dg ecgafb | dagc badfegc dgac gdb +ebfc cf cbdaf bdacef acdbg dbeaf dbecagf gadfce abgedf cfa | caf dcfeba debfca ecgdfa +gc cadeb cbfedg egabf ebacg badgce gfbadec ecg defbac gcad | cbgdae cagd ecagbdf cgedafb +eadgb cgdeafb df aefdg ebcafd cdgbea dagfeb acfeg dfa bdfg | agcef bfagedc gacfe fd +cbafeg gedcf fdgecb fc cfg gebdf bgfeda bcdf caged bcefdga | cbdf fecdg bgedfa bcaefg +bafcg ba fadbcg dacb fab bafdge becgf fdcga gafdbec efcgda | afb gfabcd bgfca ba +dfbgae egbc cfadbe dbeagc ec ace ecgafbd edgba afcgd adegc | dgefba dfcga aecbdf ebgc +caeg fdbcg decbfa dbefag gbadcfe gef gefacd cfegd ge aefdc | edgacf fdegc cfged egf +fad fedbg dagbc dafbcg gebadc afcb dgafce dafgb fa fdcebga | bgafd fabc fcbgaed edagbc +febdc fgde fgecbd ceg aefcgb ge bcadg dbegc faebcd gdfbace | dcgafeb bcfdge dcfeab cge +edgfac bfagcd ageb bedaf ea fgabd gaebdfc fbecd aef gefabd | ae gcfedba ae fae +adgbfe fcbad fbgdec be efadg adfbe edgacf ebf debcagf egba | geab aefdb cdafb eb +ebgfa afbcd cedagbf aebdfc fbgad gbdfac gd defcgb acgd gdf | gd dgf dfg gd +facdeb egab ebdgac ecdab cadge eg bdagfce gcfbde gde acdgf | efabcd edgcfb dge bgea +cdbage bgdefac gacf dagec feadgc ecfda bdcef fa afe bfegad | fa bgcdea eaf cfgade +ebac dfegb dcb bdcfea cbfgda ecadf gecdbaf cfedb daecfg bc | gdefb gfcadbe facdbg aegcdbf +cfbead cbdagef egdc bdc bfegc dcbgf cd gabefc ebfdgc bdafg | fdcbg bcgedaf bcd efbgc +cfebg bgca fbgdae gefcd cefba dbeafc gbe bg afegbc gbafdec | bg bg ecbfa abcg +acgfed cbeaf dgbe gabecdf gbfdae fdeab adfcgb abd bd efgda | defab bdcgaf dgbe dbfgea +abfdc cdabef dbgfac fbcg gedac bg ebgdfa gab gbdca fagbedc | gb cbgf fdcbea abg +ecdafg abcg dgb gb bdefgc bagcde degac adfeb egdab cgbdefa | baged eadbf bg gfecda +cbge eb egfab efdacg bfagec agdbf cedabf gbadecf abe cefag | afbcegd gebc abe abe +fbgaec eg bgdfea gcbfda bdcefga fbdce caeg abgfc gef ecgbf | cafbg acbegfd eg cfabg +caf bafgd bfgec baec ac dgceaf fbagce badfceg ebdcfg cgfab | bagfd bagdf cdbagfe cfa +egdac edbcgfa acbefd bfgea agfebd abceg febgac bc ebc bfgc | cbe bgcf cgaeb cegba +fc acbdef fdbae fgeabdc dbcef gefdba bcf efacgb gcdeb cfad | efdab eafbd cafdbeg fc +aegfcdb gfceab cegadb gfedb gbaec fa bfa fcga dcbfea efgba | baf gcadbef bfa dbgfe +gfacbd gcbafed edcbfa bd acdfg abd daecfg agbfe bafgd bdgc | agcdbf bgdc cdgb bda +fegdb afd bfgdae aebfd gaecfd gdab da baecf dbgcfe dbgacfe | dfbega abgd dfecbag gfdeb +cf dabce bedcaf fca beafcdg gdecaf cfbea afgeb decgab dcbf | eacfdb acbef cfa bdcf +aegcfb gadfec bgfce bfdae fac gacb ca abcgefd edbcfg febca | cagefb fca ac fegbdc +de dbfcega dbecga deg dcbe afcbgd bcgda efcadg gdaeb ebagf | gaebcd gde bcde bcde +dbgaf gae cdgbfa gafbced acgfde abef cgdeb gdefba ae dgeab | bcdge cfbaegd ebadg feba +bde dgea dcgbf fbdge de dafgbe efbag bfeadc fecabg fdbagce | de dbe feabdgc gdae +agdec bdgec gecafbd bcedfg ad fgbacd dbae degbac caegf adg | fecga adg gcdebfa cefgadb +bceadg ag cafgbde fedbg gab dbcfag ceabd agbed gaec befcda | ga ag gace agdbcf +aecdbg agbef abc becfagd agfcb ca cfbdg fadc dgcabf bcdgef | ca fbdegc dcaf gcfab +cedg agfedcb fecagb baedf ebagdc ec bec dagcfb eabcd dcagb | bce gdec cdge bagdc +fgdca cg cdgbef gdbfa cbag fdeac adfbcg cdg ebcgdaf eabdgf | cdg gbafd gc cabdegf +gdbc bacfge dfbeca gc gcf febcd gfade cfedbag fdgec bdefcg | cgbd edgfa cbdg afged +feb cefag fdabc ecgb gdeabf cfgdae facbe eb becafg dgbcfea | egfdac acfdeg dgecfba fcdab +faedgc aecdb afcb defgbac bae eadbcf fabged fedca degbc ab | ba fadce gadecbf ba +edfg egfcdb eg dfecb geb cbgaf dcgaeb acbedf cfbge fadbcge | ge befgdca fcebd cfgeb +fcagdb ebdcgfa fdaebc gafce gebcad fab fb cabfg bcgda gfdb | fba caefbd fb gfcab +cfegbad gcdabf cdgebf fe bgeac gdbeaf bgefa efda gfe agfdb | gebac ef agebdfc edaf +ecbdg aec febdca bgecaf eafd agdfcb cadgfbe edacb ea bafdc | efda cagefb defa aec +efagdb cfgbda gfdbcae edcbfa cadgb dg dag dgfc ebcag bcadf | dg cagdb gdabfc dag +dgcfeb dfebgca fbga afecd gaecb cfb bcafe eacbdg fb cfgeab | bf aecbg gbfa dgaceb +de dbcgfa fgdace acbdg abced gaebcd gebd fbcae afbgdce ced | edc gdbe bcdae fbagdec +gfdbce bgda dfcab abfgce cdfgbea dbf decaf gdcbaf bcfga db | bfedgc gcdbaf bd agdb +befacd bcdeg gfad begfa edfgba dbf debfg cbgafe fd fcebadg | fbecga dfegbca fd bfgaed +acgeb adebfcg febadg beafc aegcbd aef gcfe acgbfe acfbd ef | dbcefag afe bcega eaf +fcaegd bg dbagfc gbce gbedfc edbcfag bfged fcegd bdg fbade | dcbagef afecdbg ebcdafg gb +fbdcga da fdabcge fad dbag gbdecf gcafd gbcfd cdbafe aecfg | ad agdb dcfebg gadbfc +db bdg gadfc gdfcb dbecfg efdb gbeafc ebgfc gdcfaeb bacedg | gbd fgbdc dfgabce gadfc +bfeadg eafbcg fbage ed edfgac fed agcdebf dabe dgcbf fdebg | fcgdaeb fdebcga abfecg fgceba +cgfd eagdc afg afegbd faebc cdfega abegcd gdebfac fg gafce | afgebd gfa bedagf gfdc +efagd fgcbeda bfgac cegd cd edgafc fedgba bdecaf cda cfgda | dc cd aecbfd dfaebcg +edabf eacdb gcdfba fdbaeg ca gcbfdea cdbeg cad dfaceb ecaf | ca debfa cfedab baedc +ecafbg bd afdbce deb gedcbfa bfad cegad cgfdbe febac dbace | bed bde bd ecadg +bafgd agfbdec daegbf dgecb cbfgad adcbfe afeg dbfge ebf fe | dabfgc fbegd fe edcbfa +bdegf bd cedfg gecfda fbd fbaeg cefbdag acdfgb dbec dbfcge | bd bd cbaefdg fegab +dgfcae agd afbedg eagb daebf fgedbac afbedc dafgb fgbcd ag | fbeda aecbfd gbfcd fbadeg +ecgdfa cbgde bdafegc fg ebcfad fbace fbag egcbaf efg bfgce | gbefca ecfagb fagb cegdb +afgedcb fabce gae ecbag fedgba efcabg bdafce ge bcgad cefg | age gea egfc ega +fadgb fdgeabc bcefdg cdfeba fcdgb gbedc egcf cf baedcg dfc | dgcbf gafebcd cdf bfdaceg +ag gebdca dgbfc gefdcab fgcab eagfbc ecfab eacbfd faeg bga | bga ga gab bcfdgae +bdgcea gd gecd fcgadb gad egcba gcbaef eafdb dbaeg cfebgda | dbacgf dceg gdfecab gd +bgdcfe bda adbec edfacbg ad dgca bcefa becdg dacegb badgef | da adb egbafdc adb +ebafd fbgcae begfadc fcgbed feabc cgfa bdegac cabge fc ecf | cfe fgebac gedcafb cf +gcf badecgf fg bcefgd fbgac aegbc afgd acbdf bcdeaf gdfabc | gcf abcfdg fg cbfad +dbgace afcgdbe fdce baefd gbcaf abcdf dc bdc dfceba efagbd | dc dc acgfb cd +bdafegc cb becg becadg abc edbca dfabgc beadf ecdafg edgca | dbfcage ebdaf bca bdeac +bfdacg degf ef abcde fgdac cgafde aef cfead abcgfe edgafbc | efa fcbgae dfeg bgacfd +dcafbge aedbc abdcge gebac dabg gb cbg daecbf cgfae cgdebf | cbg agdb aefcbd agceb +cfgaed ec fcdgbe egdba gdfac becadfg dacge feac dec bagfcd | ce ecd egfadc gdeca +gbdcf ba ebacfg cagbd fadb gdeca bgfeadc bfdcga cbdegf bga | ba dfab ab ecbdgf +gdcabf bcfde cfdea dca eafdbcg ecagf ad gcefda bacfeg aegd | dca acd afcgebd efacg +ebafc egfb ecb be gcbfea cfegad fbcda dbfgaec gceaf ceabdg | abcdf gbef ceb ceb +decbf ecdbga adbce bac gfacbe ab aecgd badg fdbagce eafdgc | ecabgf ba dcaegb cfebag +fcbdga be gfbca cbeafd bgfea bagcef aeb bcge defgcba faged | aeb bea ebgc be +cgb abgcdfe afcb bc aedbfg gfcbd gdecf fdbag fagbcd dgeabc | efabgcd bdgfc debcgaf ebcfgda +cdafeg bgfedc edabc bdgea ac defcbga abfc dac abecfd bdecf | dbeag afbc abfc ca +egd efgbac cadgefb edafg gd dfbgea gfbd eabgf dagbce dfcae | fcaed cgbfea dfbg bagefc +abc fgaecb fedba cebgdfa gacdf aecfdg abgfcd bdgc fbcad cb | fecadg cbgd dfceag fcgead +bdec cgb abefcg bgdcf bc fgacbde gdeafc dbegfc fgbda gcedf | afgdb gecfadb cbegaf cb +dfeg gcadef fcega cbade fbcaeg defac fd afd afdegcb abfcdg | fad gafce badec afdegcb +gdbea ec becag ebc bgefcd ecad ceabdfg gbafc aegdbc defgba | bce gdecfb ceda ec +efcbag dcagfe cde ecgfd dgefb efgdacb gbdcae cfgea dafc cd | cdfa fdac agfcebd fcdgea +bgacde fgdc bfdcge afgcdeb acbfe gf gef ecdgb dabgef cebfg | cfdg bfcea aegdcb gef +gfcea agfd dfe cdgfea fedgc egcbaf df fdacbge cebfad cgbde | edcfab dfgcae dfga df +ebga egacdb fdagbc dabcg aec dcgfae bdaec gfbdcae ae cefbd | gaeb dcaeb eabg acfgebd +aecfdb agcdb facgb bged fecbadg adbgec bd cdgae eacgdf bcd | dcbeafg cfedabg egdb degb +aedb cfaebg adegfb gbafd da debcagf facged dgcbf afd faegb | ad bdae bfeag aedb +beagfc fcagd fbcgd egdacb efgacd fead acefdgb dgaec acf af | fagdc dfae fagecb dcgfb +bacd gcaeb agd begdf bgdea gdbaec fcageb cegdfa afebdgc ad | egdab edgcfa ad bgcafed +degacfb dfgab ae cgbde ebgad eda cebdaf gafe fabcgd aegbdf | gafe aebgd ae baegd +fdaebcg cegadf ecag gdcfb fedca dabfeg ge aefbcd fge dfcge | edfca fge egf agec +dgebaf cafeb ga cbfeda fgbac ecagfb bgdcf gfa cgae facgdbe | gecafb cfdgb agf gfa +fdageb bfcd gdeca eagfcb cbfaed bd ebd beacf gbcafed dbcae | bfeac ecdag fbdc fdcb +ebfd cfe cbaed bfcag dfagbce ef dbaefc ecafb eacgbd fadceg | abcfed cfbga fec efc +gfe gfbac ef fdce edcgab aegfdc bgcdaef dagec fgcea afdebg | ebdgac gacfe efg feg +efdgac afd cabdeg defb df cgfab badec dfcbgea fabdc fbadec | badce efbd deacb daf +fg cfdg fga ebafgc cdebag fbgdcae aebfd faegd adgefc daceg | gf fga defba gf +fecabd cfadbg cdebgf gfdcb bedcg eb gdeca begf ceb dfaecgb | egbf cbefdg gcfeabd bgfaecd +gf fbegd bcfg gcbde dcefga aecgbd ebcdfg bdegfac dgf bedaf | bedgfc fg dfg eadbfcg +edfcga efbcad eba dabceg ba dfbeg agfcdbe dface eabfd bcfa | dgeacb ebdcagf ba facb +cbgead beagf dbcge fdcebag fecbg dbfgca gfc efdc dfbecg cf | fcg decgab efdc cf +dfbgc caf abecfgd dcea edafgb ca gfdae acbgef acfgd cafdeg | aegfcdb adec agedcf ca +fe edbgac fagcdbe egdbc aedfcg edcfb dfe cdbfge dcbfa ebfg | edbcg gadfec bdacf fde +bag dgefb eafg eabdg bfdcag cebdfg ga dgfabce agbedf ecbda | gfea bag fdgeab ga +dcegf bcfega bcadfeg gfdeb fbg cfgaed becdgf fb dbfc agbde | dagecfb bdfc fcgeda bceagfd +ecgdba cefdba bacfgd bcdeagf gd degbc gbcfe eadg ebadc gdc | cebgda efcbg edag dgecb +dbeafc cgbd cfb cfegd cb edfacg bcgfe gabef adgcebf edcgbf | abcfdge fbc bc efgcb +gcefda fgba fa eaf beadc fgbade gdebfc dfgbe fgdcbae fabed | defbg dcefagb af aebdc +fdaec gbfadc bedgfca egfdc gdcfae gd cebdfa ecbgf dgf geda | gdf cfadbg acebfd fcaegd +gdbefa ecdfg dgeabc gfabce dbaeg cbg bc cedgb cabd cadgefb | bc cfedg edfabg geabd +gba dafgcbe edabcg fdcabe gcfbe abdegf aebgf fdabe ag afgd | befcda fecgadb cgebad dfga +dafgeb cgde cfadegb agfec gcafb cafed cagefd ge dcaebf eag | fgace cbefad eafcg ge +dfabegc cabedf cbe fced dbeacg ebfda bdfeag ec bgcaf fcabe | ecb fecd cgaedb ebdfa +ecf fedabc afcdg fdbcag efgdcab cbega fdge fe feagcd fegac | gafce cfe cdgabf gacefd +fabeg gdf gcfabed gbaefc dg dgabef degb gfbad egdcaf acdfb | defgca dgf caefbdg begfa +eabfgd edfab ebcfag egdafbc afd edfg fageb df fgdbca acebd | gbfea dbfgea aebfgdc dcgfba +cg dfecb cegdb dgbceaf dfcg bgfdce fbaceg dabeg egc bdfcea | edgab cg ecg gec +db dceag abecgf bdc bfegc beacdf gbdf bcedg bfacgde efbcdg | gbced bdacfge fcgeab fgbd +gfecd gdeacb cdfaebg bgf dfebg fabd fb gdbae gefabc bdeagf | bagde bfg egcfd bf +fc dbagef bfedcag fcda ebcgd fgdea dgefac efgabc defcg fce | afegcd facd eagfbdc fce +eacgb gfdaecb eafb egfdbc acgfb fb dcebag fbg fdcag abcfeg | beaf egabdc gbedcf gbf diff --git a/2021/day08/src/main.rs b/2021/day08/src/main.rs new file mode 100644 index 0000000..bf8d560 --- /dev/null +++ b/2021/day08/src/main.rs @@ -0,0 +1,232 @@ +use std::collections::HashMap; +use std::collections::HashSet; +use std::io::stdin; +use std::io::BufRead; + +const SEGMENTS: [char; 7] = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; + +fn main() { + println!("Hello, world!"); + + let arg = std::env::args().nth(1).unwrap_or("part2".to_string()); + match arg.as_str() { + "part1" => part1(), + _ => part2(), + }; +} + +fn part1() { + println!("Part1"); + + let mut lines = Vec::new(); + + let mut cur_line = String::new(); + for line in stdin().lock().lines() { + let line = line.unwrap().replace("_", ""); + cur_line += &line; + let last_char = line.chars().rev().nth(0).unwrap(); + let is_wrapping = last_char == '|'; + if !is_wrapping { + lines.push(cur_line); + cur_line = String::new() + } else { + cur_line.push(' '); + } + } + + dbg!(&lines); + + let mut count = 0; + let interesting_lengths = HashSet::from([2, 3, 4, 7]); + for line in lines { + let second_part = line.split('|').nth(1).unwrap(); + let count_interesting = second_part + .split_whitespace() + .filter(|s| interesting_lengths.contains(&s.len())) + .count(); + count += count_interesting; + dbg!(second_part); + dbg!(count_interesting); + } + + dbg!(count); +} + +fn part2() { + println!("Part2"); + let mut lines = Vec::new(); + + let mut cur_line = String::new(); + for line in stdin().lock().lines() { + let line = line.unwrap().replace("_", ""); + cur_line += &line; + let last_char = line.chars().rev().nth(0).unwrap(); + let is_wrapping = last_char == '|'; + if !is_wrapping { + lines.push(cur_line); + cur_line = String::new() + } else { + cur_line.push(' '); + } + } + + dbg!(&lines); + + let sum: i32 = lines.iter().map(|l| identify_numbers(l)).sum(); + dbg!(sum); +} + +fn identify_numbers(line: &str) -> i32 { + let mut number_map = HashMap::new(); + number_map.insert("abcefg".to_string(), 0); + number_map.insert("cf".to_string(), 1); + number_map.insert("acdeg".to_string(), 2); + number_map.insert("acdfg".to_string(), 3); + number_map.insert("bcdf".to_string(), 4); + number_map.insert("abdfg".to_string(), 5); + number_map.insert("abdefg".to_string(), 6); + number_map.insert("acf".to_string(), 7); + number_map.insert("abcdefg".to_string(), 8); + number_map.insert("abcdfg".to_string(), 9); + + let mut possibilities = HashMap::new(); + + for segment in SEGMENTS { + possibilities.insert(segment, HashSet::from(SEGMENTS)); + } + + let words: Vec = line + .split(&[' ', '|'][..]) + .filter(|s| !s.is_empty()) + .map(|s| s.to_string()) + .collect(); + + let mut word_cycle = words.iter().cycle(); + + let mut last_hint = 0; + let mut unclear: usize = possibilities.values().map(|set| set.len()).sum(); + while unclear > SEGMENTS.len() { + let word = &word_cycle.next().unwrap(); + dbg!(word); + if word.len() == 2 { + let should_be = HashSet::from(['c', 'f']); + let should_not_be = &HashSet::from(SEGMENTS) - &should_be; + let word_chars: HashSet = word.chars().collect(); + let word_inverse = &HashSet::from(SEGMENTS) - &word_chars; + for a in word_chars { + for b in &should_not_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + for a in word_inverse { + for b in &should_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + } + if word.len() == 3 { + let should_be = HashSet::from(['a', 'c', 'f']); + let should_not_be = &HashSet::from(SEGMENTS) - &should_be; + let word_chars: HashSet = word.chars().collect(); + let word_inverse = &HashSet::from(SEGMENTS) - &word_chars; + for a in word_chars { + for b in &should_not_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + for a in word_inverse { + for b in &should_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + } + if word.len() == 4 { + let should_be = HashSet::from(['b', 'c', 'd', 'f']); + let should_not_be = &HashSet::from(SEGMENTS) - &should_be; + let word_chars: HashSet = word.chars().collect(); + let word_inverse = &HashSet::from(SEGMENTS) - &word_chars; + for a in word_chars { + for b in &should_not_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + for a in word_inverse { + for b in &should_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + } + if word.len() == 5 { + let should_be = HashSet::from(['b', 'c', 'e', 'f']); + let should_not_be = &HashSet::from(SEGMENTS) - &should_be; + let word_chars: HashSet = word.chars().collect(); + let word_inverse = &HashSet::from(SEGMENTS) - &word_chars; + for a in word_inverse { + for b in &should_not_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + } + if word.len() == 6 { + let should_be = HashSet::from(['c', 'd', 'e']); + let should_not_be = &HashSet::from(SEGMENTS) - &should_be; + let word_chars: HashSet = word.chars().collect(); + let word_inverse = &HashSet::from(SEGMENTS) - &word_chars; + for a in word_inverse { + for b in &should_not_be { + possibilities.get_mut(&a).unwrap().remove(&b); + } + } + } + + last_hint += 1; + let new_unclear = possibilities.values().map(|set| set.len()).sum(); + if new_unclear < unclear { + last_hint = 0 + } + + let clear: HashSet = possibilities + .values() + .filter(|set| set.len() == 1) + .fold(HashSet::::new(), |acc, ele| { + acc.union(ele).copied().collect() + }); + for val in possibilities.values_mut() { + if val.len() > 1 { + for c in &clear { + val.remove(&c); + } + } + } + + unclear = new_unclear; + + println!("{:?}", possibilities); + if last_hint > words.len() { + panic!("Loop detected. Cannot deduce segments"); + } + } + println!("{:?}", possibilities); + + let signal_map: HashMap = possibilities + .iter() + .map(|(key, set)| (*key, *set.iter().next().unwrap())) + .collect(); + + println!("signal map: {:?}", signal_map); + + let mut num = 0; + let mut multiplier = 1; + for word in words.iter().rev().take(4) { + let mut signal: Vec = word.chars().map(|c| *signal_map.get(&c).unwrap()).collect(); + signal.sort(); + let signal: String = signal.iter().collect(); + dbg!(&signal); + let digit = number_map.get(&signal).unwrap(); + num += digit * multiplier; + multiplier *= 10; + dbg!(digit); + } + dbg!(num); + return num; +}