Welcome to another installment in my Advent of Code 2020 series, where I present my solutions to this year’s Advent of Code challenges!
In this installment, I share my Python solution to Day 6 of Advent of Code, titled Custom Customs.
Spoiler alert!
Please be warned: If you want to try solving the challenge on your own and without any help, stop reading now! The remainder of this post will be all about my solution to both parts of the Day 6 challenge.
The Day 6 challenge, part one
The challenge
Here’s the text from part one of the challenge:
As your flight approaches the regional airport where you’ll switch to a much larger plane, customs declaration forms are distributed to the passengers.
The form asks a series of 26 yes-or-no questions marked
a
throughz
. All you need to do is identify the questions for which anyone in your group answers “yes”. Since your group is just you, this doesn’t take very long.However, the person sitting next to you seems to be experiencing a language barrier and asks if you can help. For each of the people in their group, you write down the questions for which they answer “yes”, one per line. For example:
abcx abcy abcz
In this group, there are
6
questions to which anyone answered “yes”:a
,b
,c
,x
,y
, andz
. (Duplicate answers to the same question don’t count extra; each question counts at most once.)Another group asks for your help, then another, and eventually you’ve collected answers from every group on the plane (your puzzle input). Each group’s answers are separated by a blank line, and within each group, each person’s answers are on a single line. For example:
abc a b c ab ac a a a a b
This list represents answers from five groups:
- The first group contains one person who answered “yes” to
3
questions:a
,b
, andc
.- The second group contains three people; combined, they answered “yes” to
3
questions:a
,b
, andc
.- The third group contains two people; combined, they answered “yes” to
3
questions:a
,b
, andc
.- The fourth group contains four people; combined, they answered “yes” to only
1
question,a
.- The last group contains one person who answered “yes” to only
1
question,b
.In this example, the sum of these counts is
3 + 3 + 3 + 1 + 1
=11
.For each group, count the number of questions to which anyone answered “yes”. What is the sum of those counts?
Importing the data
Every Advent of Code participant gets their own set of data. I copied my data and went through my usual process of bringing it into a Jupyter Notebook running a Python kernel.
This involves pasting it into a triple-quoted string and assigning it to the variable raw_input
, and then splitting it using two newline characters in a row as a delimiter, producing a list named split_input
:
raw_input = """wdcmlzfnugqtvjbsahi easrkmocxbpjgi xrpnegqlcsyodhjfutzakmiwvb mgilapxjtrndbheyqzckfouwsv scynhfozmlvbqkarwj qhvjkmbyxcfonlazdw vjhzfnapwclkqiomyb bpourq ujpmoqs obqup v v v v v hwjlda thkdjo mlhdjw edyfjvh djh xvjadplfcwmkeriug adgtscyjipewvulr hmkipduz mdzkupi vtyizwcdm zdvtcyiwm mfzcywvtid iydclntzvmw ps pm pm p pm nglatdiw twialgd igsadtwl iadgtwl igawltd jqoxkavs askoxvqj aqjvxskho javqkxso kaxjoqsv ltkjfoxes aifogkepx ufcekybmzx tigxeahnkf xzfqbsnhmrviju rmfvsbihpujq hstfumvgrobijq bga ye sg gs sg gs sg knipbjmtrqoawe rtaebqkmopjiwn ewbuqcaijonmrpk trnmpeqkobjiwa puifme ufb fvp rntzlyxwokfcg nufjzpyawbqsdgi ogcmpsdqaxvnzwetklh qmdxnfrjobatzwgcyse rdxtqseawzobygcmfjn dbot dwetlp odtr utbqx bxuqt xtbqu quxbit rvthuiqbljzaofgwy ldftrsjvwoiayzuekcg dtxmalyuifznvprkcwgoj twc w pwg rmzaoylipcwkvuqesbhn kpuvhqlwyrabmszecnoi cwaylknbrhvzmiupseqo qlwiyczkevsnarhbpumo cgr grch cgsr rgce eso ose soe swimuzq zqwsmliu iqzurwsmh iswqzum wzhumsqi amolqcs sqoayj sjazqxyo aqhxsot ozsajqkx qbkyhzfspewgrtlv pyeqtcbskf jxbsonqtaykpef tqskfmyboeup bdjtfqypokse yrtwqacluozbg vwtfgzmcqobraj opctryzqawbg v v v nv dclgejbqrwtkvsxfa ovgkhjyiqwbmefc vpkx jmydw fxa xa gwtocmzslfqk olkgwzcftsmq ogmsfzlwqckt uwxdl wdux oxuwdt dxwu ab smej t iwhlqdermv menrvqd jemdqvr n n a n jqcvetapkbsyu twndyfrixzhvmsogu xsz zxs zxs szx sxz fsatbzpvqjknlrm pbnljkratfzvsmq qtrasmjnlkfvbpz bsjavlmpqktrnzf qajlbkzvmtsnfrp utlchvariswzqjmx almujswrcthzvfqix p j p n cgsxwk uwkcxj xpwethnkcyb gywc wcgy ctyugw gwcy wgyc afmtuw muatfw ujwmnirvkdygtqcabh takwqdjgzcuivrhnom cawvrnxfjmghdtkiuq srgt ma urnymxabthi rtxnailuqvoyph duytknrwiehaxz rhaumscngtyix gawbhtkqruyjel rlcqbuhawkmyfgjte jhbyqgkraetwlou gxbtrnadlejwhyquks ujfkleagybrtwhq dctkngohsxaljfrbvmey mlbvxaksifyneodgjctrh lmkfobtyxngsjvrdahec srkyveohxfmcjdlbtnag lbguestydkjxfopnvmhcar pjrhgzdwcmxq cgwhmqpjdrxz meipjv mivpje jevmip peimjv vmpeji zkcavufyr fyzuarckv fvnacqgksry yvcrfak crfvyka pqynikfxd dynxqlifpk laozucfdek kpeauocdlz uoldfsekzac dmekaclyhnuzo hwubponvt vwytnphobu vpnbgtwuoh wpouhvbtn wcxlspod fmedwbtjops pwnoscid ufenjp ngfxpe njbpfc vqnthfazdp pmfjng qrphaykit qkysrta jbwdtqrxzav wjqfz jqfaz meuxikpsdw kjimpsdwxu dmxpwikujs fjbsnmcgxz bpfimjnzduh urtkjpo cthod voclmdguaw oczdlntgmeir zipf fipz nbwcexytjzripskmo kwpzrmtxiosejcb tsrcoebzpiwjmxk jafcwtsukrodynhvqge pubrlchazvetosyigqjdk qminshlycutrdwx vigzpqbmunacrjftokl hweisfpycjdzouvmbnkq nqjxscbvmuwhzfakdo qtndfjcglrkoyux jqtrlgfkducxnomy tqnrjxckgfaupdoly pbe b n n n n n nsc ns nse qzkxif vkfqizx kfzqix zixkfq ifzxkq nvshwafe vbewacnh nvwmeah ziafwxcnmebko cdfrxmk fkmpxc y y y y y yauxbwtcvgoq ywcvotagxbqiu gqwyvbacouxtn uobqrcgwtvayx qcgwoytbxuav r nr a r xjrkwdnbyuscfgqtphiov brshqnjcoukxitvywfdpg vrxlptbcyfdwhgkouqijns fqcvwudyiotjblzpxshgnmkrae ouhrqwtgnjeyxiamfzpdscbkl lnch nhclmg ldhzcny uzfeomrwi ydgkhjbx qcylp hagpbecorwyvfdz rstplobhmfnxvykdewc hfruowdvyjgbpzce rgdiznuxa dragzniue jzysdgpuvkowqin lugrndiz nbdcfugzi eqponlctfbwgk kyjniulbegpdchtwoaq onczetbplkwqg thevzsrc zcewrsv vczueosky wmzlcsve sxeqojwkarhyutcdnzp rjnuwzxpmeysdoctlaq pzqxeratsdnkuovcwjy qexjbo cqunbaprhwyjfe ilvegkbszdt efbmqj wm wm hm wm m texosjaiycpqn mntxspgikeldwycjarq nysqtaceipjux phiqznsyxjcvoabet neqytxcuipsajvf z nozy ydfcsvgbikjphamr wskfdahgpvmrcjby kpsirchfmjavbdgy pslxjrugtzydaokemvbcfqh t pyt fu fu d yj fnl qxkevrhpna kmvyorduqe sfnmyevqwka fvmyieawqks kvfhmctdqjaslywe ywfkvqeanms kufwyqmaesvh ulvtwepimynb ehmpnylubtvwi wplevtunmyib lpbwnvtuyeim unvpbgtjiaylmcwes zfxkceynsu xdfzysnuie efzgxunsy nzydesxfu jneurlkfzxbq xekznblfurqj rbzljfkqnxeu zueknrbxjfylq xzferlbjqnku tr f egtr jmu s aeq qae eqa aeq bsgjrilayoezwndvtq giadlcryjwobhznqesvft guxfyzespj zusejpfgxy zxygjuefps xvmlgpzjdrubqa jfmbdvzeyrlup ruzchykmbpwdvloj vhmuebldrjzpo bhvcmrypulzjiwds cfsjzamyhtoxq xoysjqfzmhtac qocxatmfshjzy vdhsifmynta nsmfhdtya tsnfhamdy dafyqtsnmh rfybkqmh qoumrd l y l y w vlmerz mlvrze zvrmle zelrvm mzlrve rnb gdcb pbtergc tecfjgr ogzsajwvdiplrtxbhfcnky btjzlswhikyofgcpvrnda tbwinagrhfyscvlejkdop dakhrbipqglmovjyncswtf bhyljrdpsktgfiancwov qolmnyvhtwe uwkecxshldmoy zwblsockyumh miywfgjhaopr u u q zdxynqmev bdexmzyiqcn bwtqagf tnmwfaqbj p p p p p yjgicnxerbsu xpzfqirsjh exswmarikbj mjxgrsi tvzkbxugmcnshproiw czxtirownbmpksuvgh cnofirbgmhzuspvtxwk gmctsrohkxwzupvbni vsgbzcmktxihonwupr dyaoim aiom ozimual oxaim biyotma saliju evzhxcobk lj txhebw etwhbx uhwxtbe eyaixfrdhoqksc eakiqsrocxyhd vkr kvr kvr surjoh ubh ubmahe uh vhkcawrno vowahrnkc oknarvwhc cnkhvwoar wvhkanrco ntpfemsvialxgrwqzc kcwegrtispxnyd phstgwixebnucr xpwjtenscoigr sfanq xgtzfrn ulfinpkv kdwnyjuzctiqpgf ngtcrjipqfyh ypfvcqntirgjh urmtsh svihtu utmph juthydwafcqx uthve sk ks sk zxbvcrgyqfjtoeiudwpkhsm igkswrpohqudcfjxzmvytb cpatkzfuoyqgvrsmibhxjdw kvgumiabnoh bvaiomgnukhc mkhnvusbagio hamiobunkgv umsgovkahbin yrceuq rmdbpa nkvxjslwg lkt fl vil whn hn hn gvebcszypdlkqm zcyhdlpqgsvke cdzbylqkpsuveg pzsyxlgdaiofcqwvek gmcpvyqzeskld ryzfspdtbwxiomhkjnlu tqihvployrmksbunzfwc dualjxigyrzb durysxjlgzaib srgxulazbjyid lrgaubjydzxi azritbxdpljyg cfahxspjelitdnobuvqywk vwmkoxulnypfiatgjbhrcsq f f f f f grbmahpise mbireyxhalgs bcrkvuwqt trubckq rbuqtkc trkbcuq rubtqkc zxpbejmvhrutwlkq gtvrzphoqcbnmju pjazbvqurgmysht fhsptiumrvjqbz kpszjieflb blsijkefzp zksjiefpbgl ilbzrksajpfe sglzbepfjik nxwohsipd zitjrlvfgsoym bqsiopuc aoisd wj j fzpglynjeu jk ikj abzftcqvgsrnexpmho pxvlybndrtqimzhosacgef nocqfzerjaubxthgmwspv psauzryot vmdhbwe lfc gsri sqya lkjudnqterhyscgx eckqhlxybrngudstj wotecvlyrpgqmijxkhsd htpc caph hpustjc nyhcedmgp ezaljkfpiyvroud hbiwxfstmldkecnvpgqo kiqvzyjrthmblu ahszlduemxcij wirunkh hwqdknyirul kxwuhirn hkirwnu ierwunkh vcgno snmicvo ncomjisv coynuv lconfdvpz tgqkhowsyfbzpcur zcqsugtwhrpfy fgydqpcjswrlzeuth thgpcfyzqeursw rwpci cipwr ptrlaqebo pebtlaqor qboxepatr epbartoq fypd dpyf ypfd fdpy c c c d c gxysqiamkpc mpiacyqxkgs mvqbskuzefy zcsqebma ztbnlhe snx xsn nkxs snx joyblxztkswmvnec gdcrhykmfseqapibzu oqzmprikt fhlgxucda ygrjkfazu kjiagrzyfu btg o v oc bnqyz ebdfls vjhocqkamygbplu dbwsyefuxzntijrc kmlb kqsbl vhjsdxrfpokyznibwgtmu syvkmzfjgpdwixrnbutoh uzbjgvhdkpynmtfoxsrlwi jpkxwvyrgftzioubhsmnd ykjmzvsqnhxwgbrutoipdf hwby yvbmhl echgiyabjpf krbouyh hrynotb tobipzwfxy pfxwytizob hsfwzpbotiyax znrgilpcfse seinczlpgrf nircgepszfl rcegzsiflpn lzgrcpensfi tupvbxcgfnmh fpqntvibgxhmc npbtvxghfm tvxfhmbnipg rnvxmhfjgpbt inysghdka wbc noktsjqvmzxheplrcfgiuwda dlogmasfzwhupjrkcntixqev zhwfbs bzcwh igqkpctmafr jietalnfksqp ewlanjvgdbpm lpjangwvem vpngjwmeal glvwnpejam enlgwjmvpa niocufzwrk kirmuzfocvw juqervaxni cudjqavnix iuvjndcaxq xvniqajugd vxunjiaq lmayozkep koazylep aeykzlop ejgotzhrxbsnqimu lrspkgobcaywmjfvhd uazxfrony uoaxynzrf yunaorxfz lowmgiuxpnakzsq qwaomukglnx ubgkoyqanwlvmx lowaqgyxunkhm alfoj floj yglkurfahidmoxtvz muykfdrzivlothga devylsarcupgoihkmftz rmtgkalydnhvozubfi ahrzqodkgxyuitvmfl gmdoshlaixnc xhmicdgosanl hiqgvbulakdmoxcpn hoiamdsngcxtl egnprq ash oabx abxfo vomitazdbflqhkue lvemipubdafohztkq dkabtiylqgzuovehmf euanfbq skufxtami nefauc fadpbjxvzeghqkriy xpaqfzjvybdhrkieg jeqghzxrdyvakpfbwi ygvakzbdpqrxjfeiht dhvmgjoqzknbwyl hvnlobmydwzqkjg vbzljhdqgnmwoyk s k sk d svzubcigpeqdthfmxnj djogszbcnvefqthiuxmp vgkpjwymduqfxcztoasne swgapuymcvqnjodtkfzxe stgv yvbts iokqv h h h ujxw uz uz uz cdh lhg mkh quroafxhipsev d d dg lbrsefwhxagdmpqjiocuvyn uobfgnhvsqwzlkjyraicdpm uwnvthio yudxpkhrngb selctamwozqvf ueiypcnoa nidaehvt akencir ozcru uoflar pwoxuyvm nrgaujbldiwxvq sdvuirgaynbj synqkzdlpebticorvwxmaug ekyatuvbpohmxnfdgczirwql ztvwupairdeqmckgbnylxo alngerwtckpvmioxbduzyq dqygzrkuowejsm zyikwtjsver yzjrsvawceik lqduw iudw wdrue zxnrflac lx lvx otlwkx wxyl q q q q q aigmyqxscvhde csegmkqdnvyahix gvqixcdheysam dsmyaexgivqhc shiqmxgdeycva nbmovulhpai yfcobuiaetpdvmln mnopuvlbia wviopnmluabk uvamohipklnb cibznrjtsgoafdlkx cethszlarinx xcsztairehnlp szhnatlxicr mpselnxatyzric qxdzauyvbc qxzvabdcuy zyxabucqsdv uzxvatmprofyjcliwgbdsqnek kynesjvmoifucbpqwxlzrdtga mepouwaj cutjoxvpa apojcvftd opwdaj njhzgqrikyapslo efqisyl elfqsiy sfiqley if wvzghe xn dnxu afuolgsxdckypbenqjhmr euqojgbfzphxmnrysda xsbwrdmnfqyphaegjuo xsfuhpgnbjmwqoaeryd xjyizvdsbkfmhenwop mueiojtyqnag bjlvkzugwqspcihmxr mlugrxwjqsvibczhkp vikuscpzwqhmgjxrbl pxzbksqmjlcgihurvw payfmlnhgtzsrxke yeknlfshmrgztx p p p p gnmcwdtquesz ztdscnkg zdancosjgt cdntzsg zcjgbpstrnd ugxawjehznr gujxqzrwhnea rewagunjzhx krvmqtuiwsezbjcdhployaxngf xkwsubhjlfvparcgomyenidzqt jrglbkzcwtuayno iwydqmzt tdwfeyz jnvchaxzotqd jdaohpexnt yifrdlua asridu rchuily brliyc icyl nclfjxiytvs hiycrl fj fj jf jf hncwvidj ncijh qjhrin zsxiheynj jvihaqnc zkraj zkjrl akjzr zsvjrk culpozwn copkslvruwt fhwlecupo cuibdzoagynvrkw dobjkzuxvrcyinag ipsduzkvybnoacrmeqg gjwfeblzasoirpvhcqtxu dcfqurpiaxslgjhbyzwmeno agi sa vuhaksj zatyex xponks ojnkpsx cqeiyuforpx bxfnzrs xhfnvdr rfx tqowdj dw diwfu dfewgi wd dyvjwabnuzgx bjdyglxvzonw dxbwnyvagjz zyiwdcgrbjexvn xjngvztwbdy ob ob bo ob cp cp cg cnwlshdkyboxmpvtz ndswvmyzjeuprbgchl ayzkbswhlnfptcvdqm cmfyketonuixwdgpav xeyngtvuwfcdik xdufiwcnekvtgy zuvyixgbsdhwnjkelcf xwdfvucyinekgr vwznkidlhxuaosfr rhakozvwgfisdunl korahvzwdsyilnuf fhniwvurokxszald rlhewm whpr wraqfh szpvechrdnybfjmog vgptfyjmbdcxzshe zhsfqepvtygnbjmdc yzhjalgcusdpfbmveik xapwbucizkl qgs yoehs jrsge vjgdtm vxyuftcbpl uycftxlbpv vtxflpbycu pbcxfvtlyu xylavcftupb yhbadxqs hqdxsay zphxtaqsd asdoeqxhfnj tlmrid osmrjwlvge hfprkgzljcs slghpczfjrk flcjzshpgrk hzfcjsgklpr rgphlfjkzcs jizfvmyocxq kogmzyjiqv ujnqwpaszvhymro gxomvzjcqy ie ie piec ei tvaxzm txamv puqcatmbe bcuqto ubsiqhzgc vkqogu oqfhigvudm voqubg zlfapsjxtm umalzjpfosxt jplsftaxzm tafzxlpqsbjm zicyojr ilzcyr hzcrtleyvipf zicypr yicezur ehigkrowmvjylnxb tihkwoxbjelnmrvy nkjbmxwhoelrviy vnojlkwxrmhbeiy lvkhxnowmjeyrib bpzgcrtoisywvned acdwbrytesguv b qb ba bh b hntmvbilk tnmbzvl vbfnmt yvnomuxbtw ib ib lib jefqbsd iyhzrvcngl zaeqfuhynlbxipwvgotkd kagozfcnbirmpuesywdv wfulhibnvtapkyrq ibleqfykavprunmxwth raulpqfwbhvintyk vnwrklhufytbapiq bknqpvrytfuhlwai wtgqajfmk mwbfcjukqga fmghkjwaq gfawqmjk fjwmkgaq lxk t w w mf mnzbcw rad ejpxykvhu zslo fatkelbspwjyhoqrdui qsngucrzamvx ldwhapn wnma awn wnau naw fnrgykvulp vyrlupngk kynprgualvq oz oz oz zo dehqmlkfyxpvujagbtcwio huapickwemyblxodftjvgq pagblyvnjotwekuhcfxiqm qjythibopgxaeckmwvluf huclkitbnrad uhdoictnrklba tlcbauhdinrk pamdyrxtj pktnmarybxjd tr drt rkt tor rt gjrclwxyuk isqmdbf eoivmdfpt qvg i ui j l u lu osd zmdknlf nzlfmdk dmznfkl dzlnkfm ozdievpxm mzoxdiepv idpzexovm ovdemzxpi cbyrndpwk kozyrncdp ujcksndxphyf dnbpkyc e e e w tpndq igpajc gihvnzfacujpoelxqkr mqfjnrodzkuvxaipel okwanixqlpjufevr rxipfjtasnekyulqvob jmgeixvhaoknq qloihgekvjxman ajoghikvmpxnqe ingovjkaehqmx szualfd zsulfaw osajgkmyfvri hwmisgbaqf bvzm izwrn zsfd mz kixhabnoqpzsg gpnxaikqobzsh argcboiqznkxsph oaqgicsndhkbzxp jzxhpstoinqbkga fkisv fvisdrk vkmisf ksifqv ifspvk ovwqbshjirpg iohbgfjqcrvt efsqtnoywmkzrv gwsotczmkdyqlrve zmverktswfynqo rwtuihvjqofszxaykn riqntxydpcakjbgo mzkndrhabvj gywofampnlxbhsi hbtenma rcamubhn bmndhkja afsnkue pfcvirzydqbetlk zftwoivcr rtvzwix kzjwtrgi hitwzor harkxeinjtgpy tprbwighzeksymcv qecrsugzdho uwdxocqyftnbzhl dcrzqhuso sdchqozu cu u u u whlfczpqa yfzhpqc zpfobxkhqc cfzhqp fcozqhp erbnkjqouhg qsthnrjiwxvzkaf yncqejdukrh klqubphrnj ikvyfmlpacez mzeiplkacvfy lyfzipvcmeqak qo h neuzqjk ixqhknj pvbrygdacs xf ef f f t t j z ybx a ewbmvucapsxkig ksiprxbvagwfme agdmiwexvsbpk egisawxkpmbv dykmbtv dtjkmyv mvkdtyc ydkmtv zw o i oi r hjybnqczotxgeiaup cpygejxuzhonitqab jnauyiohtcepqbzgx uaqtnyicgezoxjpbh nptbcijxzuoeayqgh owynpg wpyn pnwy ywpn fqoishlxzarmkc dstzjmxrvfckuoi xsmozrfcik zmgsrcwiofxpku igrkebsfoxcmdz moas xdcfmoa xltpwzrf erbhflxwzntp tnsrbxlawpzf rtzwuoxipfl yesonitqag etigoaqmynshc tpneigasoqy pnmvfgwyierqukct tcnuigmqpeyrwvk qza zaqr zqa qza qza dyphutng ghntydup thpudywng tpudyhgn gofac homgpwae govmnaw azsgo apgmyuo bsvqmnidyogzuewtk qitsokmnewdbuy iqskwxyonmuetdb kbmsundoywiqte uoqj jvs yjazt j r o nv oy r u ru avofjnwdehrpyklqugzimtx gfjlwkrahzqvneodmutpyxi mgpjlfuxoyvzwqtdhaeknir ryfqedjgpnikawmtxhvuloz gidrokyntxavfwpmqelhzujc k mzp jfpow jfop pfjo qokte eno woef qnart utaqr ajqivchfpem upvqhcjmafe pvewjfhmcqa vmjpecaqhf cahpfemjvq ayw kp rixwjogsfhuyvebnp vexwuhjyogrfisnbp psyfwhrxeogjubnvi yjvswhnrogixbufpe eylbtgqvronjfzphw brhqvoptnjeygwflz rwhlpzbgjvoneyqtf hcvlnbegyrfwtpozjq ynwefqphlvrotbjzg xyu xy bj j j j kocgiwr ibksgowc ijkcwog wiegjcaok rlt ltr lrt rlt lrt xjoc jcoxz cxjo r r amrtqgfw udr sderl vonauts atxsyvco psroawlevbjdgk smvzhqoya ubwotjecsqhadvkf ufqkjtesadhvcbow pbqsuatrzk rsquzpobk pqsrbzkdu zqcusrkpvb spbkqrzu vqwcinpb aiejfduvpht zowvpinrxqml zufk kwe k mk wko lydtzkhwajf xrphuebsogm nsucmhgqpx mwhlnfotz shvfwmanl xfywodzc fyoxchw howycfx owyhfxc xwocyf hri rv lzr r nrfc mkd yvjkoguaxip nwkz bcktdsmqf sznkw flhbygto ftbylhoa otbyflh yhbtfol rnphowmiy hpwrmoyis iunworypmh diyrwhnmpo qlhfdcg plju rl nalk lep xwcropjbzkdegnufsqiht rcidlowgxnsktqpbheyzfju ctfxzgvprnhbqeodkwjisu wgzu fkbuxrwg gwjyuhzn nhjyzuwg rwbm wmkrs itzekynp dkepiunyt vmwlbgkcipnseqr xlkbgo kgz kag i x htq sot f kuqperwixsgvbctyjfdn qjcdstpxvyuwifnbre atqumpwrefdxvoijcznys rfxdcuispnewjtkyqv imdwuntbahxvj daniwhemzxtjb nhmajglidtxvwb tbjinhxwymad csaqnhdtfrmjobwkix esjkcgpf gmfszpk pfgksj i i i i alnj zucmhenrtoq bkn nifp nvi mjkagdfsilctvpwhuq wufhtjlgpaivcqdsmk qutsghdvkmcwfaijlp vutlpwschfkamqjdgi isjwtmqkvfgalupcdh ynsgxmfoelkd yxmvgkd vgmykxd gmyhkdx ygxmkdz vldyroebks osverdjlyb eszyrudvblo rvdqomlcbtygse oredlbvsy o o o o jvmo jbkpwnc dtwrpiqbsf vlayehgxmozu khc crkylh ewticaxphkd fhncmk khcf ofvtnbpayduik ufbiatxvpdoyn oibfupdrtvyna favdotpyixuqnb ywatimcefbgvpsoundz ismcgzfqbxdlv zlwdsivqcbgmf szcbqidfgvlm fgjx xjf fxpjm xjf etuipyaxvmjszkf aeyjvuwzspkim pajvmizekyus nmbyxzehsagcdfjvt hzbaxdfyjevntgc dvthajbznegxfcy ftwqrgzohp hexzqdjsrfwmu eubmrcntqyxpsjozvilgadhkfw zjfrdtwiuyoaepcnmkslqvgxbh keiurfhgmzovydqapcxjswntbl umkocxlwagfbzepjyirtvdsnhq kngzxjrshovmdbtclwfiqepyua lyomqhdu mylowqh chlwr rhwcl crlhw wcplrfh tamkofzcpq tomgsjqca bj vfwbi gb gbz njaecfkliuwm cnvkfwualmij iaunwmfelckj hjifcqxumnkalw iht orlie afsxupiz nocfkzrdstjqwu ehisrtpqnwgaymbuc qxgt gtoxcj txgr hoxsgkdiqtfz gtfisxozhvdk gofksdtxihz izghftsdokx i x a i i knqftuisvegdjw ymogrc yacpbrog gxlp sa k k w b speodbqvngkjrl nfslobmegdpkjvqr rpgbneasdkljqvo dnvklcgjiorqpubse y y y y y pahzx axpz axzp ya a ay ta haposzk lromwgtifpz xobjpszq pujonz ndopyqvz wqfl kwuqelft lfbvwq cqgefyksvhwpalxdbrimoznu spvyfeucqnigwxbkzrmadl sxlwrdbyvgzpqmnuacfejti ou uo uo ou uo oridzvksnfuep vsrpfexizdoq nxo qgx xybutqfszdhl ybsdlqfxutz zlxuydqbtsf tl tbla rtel jtgxlohyc w vrgofi kcjt xkqnrf pxqnr xrqn qrxnp qxrn pabnotufrs bornptsda sptobanru mjzonarsplybgt uranspoqbtx gncfptk qyodies tiln lt lt lt ecbxuofajgshqk crehbqksaoifjux klibxfuhqejocaws csbxhojuatefkq gjaqcexhkofbus egjflivrcywdtu gyiedbfovxt qsviadhygtef gihexypfdvnt niotdwkarb jbimonfkl j z yfob bymsrul byqu dsycgzhtioxrpwvlen edskiwvnhozrcypglt ygcjblprwkiehz esfjghqbliczwpuk ieghcjlpzkwmb mcephzsxawni imazphsnexw mxezwnhysipa wsheabqmnipxzd pcmwhysneazxi yovtlfcuiwxmskzjqeghr gbcvlityzjxqmfoswkue mqlgfwkvysjuzntieoxc hivam amfuvi aemv mxahvg lze le lze lepd jwpm wjmc gcmjw mjdaw jwm s sxuq su ns uqs csqmipzdnhxkvfgt hxdfqvptigsmcn snhpfmtgqivcxd scihxqwpmdntfvg nrsmoa rsnma sramn nasmr msnar yihjeplactzrdfb ztfekaljidry qwhty gweh zksdnuipmhal oxtwrhcbjq qhcvb ybemxa jseoixkvc ugdxepl tofhpsgaul lqushgofp lfphogsu goplhsuf lhsugpof gdwzk xqw wmiagjvocxrzkhdtfu xhwscvljdqryibpgofma rgacswftbpxydum rsmtxdypgofuacwb rwqkufstdjxmnalgbpyc thzabcdwprgyxmsfu caywrpsduftgxbvm dujpzvsb jusdvpabr odvjbsup budpvjs kpujbsvd gfpncwmjrdlx rwmcfnbdplgj djw jdw wjd diwvjn jwdb h k z k v al lpa la chinjymkwe vnwgcemkp kvcemyxnj enkcm krlceuqnfom glewafm ngqawfvul fia rup j rmp ju dgvtfbxow wvtjgbdxf gxtbwvd tbndgxzqvw kuycgeanqwh unkhyceqgaw aykcweqguhn ceqhwkunyag vcighoudqlamkrt dnyhemguilvqtao hxzoubfvgiqajtlwm ovqepnmgladtihu oatnlremuvsigqh nd wdn xnea ribpvsfynoctmqh n rpflnskacoq laqojrp liprwqao aojlprq oarqipl jvk zwmvj jv vj vydxlwk vxywd xdyzurw lqghripoewbjdvy qbpcdjlwhovrey pledvrqbwhsojanf hjqdbptlmwzeogvr rqjwohdipuvble h h ot t t fyxkdhnwalez weakhydnlxzf xhqawsrol guxblwar lvkjrydiwxmptea wfaxnrgl famd xmd md fmd damj istm smbj mwbs msw ms raszvfpwdmeich awfmvlizcperdh pjviuhacmzfwdryqe t t t t t b b uemjsklzft teukljfmzs ztfjulsemk jkfesztmlu jlumszeftk toczuxmkslrhpvijeg mopultekcxzv xukmoedpltczva kpolveuzxmtc pktdovemcxzul agvfm jvogqfiau vdaogf c gqd sgfpy sgfbpl jretxvqmfghp tureqgfvjm ijtyqezrgvnfml mergqtjfv ubzsrptcgxwdoe tebrzfpwuxgds wbdrxtfpgsuez gvnuhzpdebwaxtrs gdqmje gqtcpdmfe rxqyiehnogmsbdv fqwzlinbormheg mjorlniezb ia ai ia ai xghswfuabdlqicnyekmt bgetcxwalnkdiysfuh sigwankrfuhtcxedlby nlwxfucbhgtdseikya khawdecfngultbyixs cwzgvbuqlsyetoiajnpm asjpbudkhoinqgtzryfwevxc zbpfxictqy udpyqtxizcgef zptxicfhqy hpqcfbzixyt zxhqpyftic oirypkjhxfcwdqeagu hwoqyupdrjigbmvxsa kxdhpangqieuroyjw fegnzr rzfgne erfnzg znfreg pvuafthmr dvpmwcyg""" split_input = raw_input.split("\n\n")
Each item in the split_input
list represents the collected answers for a group. If a group has more than one person in it, a newline character separates each person’s answers.
In the sample of split_input
shown below:
- The first line shows the answers for the first group, which is made up of two people.
- The fifth line shows the answers for the fifth group, which is made up of five people. All of them answered yes to only one question: Question v.
['wdcmlzfnugqtvjbsahi\neasrkmocxbpjgi', 'xrpnegqlcsyodhjfutzakmiwvb\nmgilapxjtrndbheyqzckfouwsv', 'scynhfozmlvbqkarwj\nqhvjkmbyxcfonlazdw\nvjhzfnapwclkqiomyb', 'bpourq\nujpmoqs\nobqup', 'v\nv\nv\nv\nv', ... ]
Finally, I split each item in split_items
, using the newline character as the separator:
groups = [line.splitlines() for line in split_input]
The result was a list that I named groups
, where:
- Each item in
groups
represents a group of passengers - Each group is a list, where each item in the list represents the answers for one passenger in that group.
Here’s a sample of groups
:
[['wdcmlzfnugqtvjbsahi', 'easrkmocxbpjgi'], ['xrpnegqlcsyodhjfutzakmiwvb', 'mgilapxjtrndbheyqzckfouwsv'], ['scynhfozmlvbqkarwj', 'qhvjkmbyxcfonlazdw', 'vjhzfnapwclkqiomyb'], ['bpourq', 'ujpmoqs', 'obqup'], ['v', 'v', 'v', 'v', 'v'], ... ]
With the input data massaged into something that could easily be processed in Python, it was time to get to work.
Strategy
The goal was to get the total of all the “yes” answers for all the groups, keeping in mind that if any person in the group answers “yes” to a given question, the group is considered to have answered “yes” to that question.
Consider a group of three people. If:
- the first person in the group answered “yes” to questions a, b, and c,
- the second person in the group answered “yes” to questions d, e, and f,
- and the third person in the group answered “yes” to questions g, h, and i…
…the the group is considered to have answered yes to questions a though i.
To put it mathematically, a group’s answers was the union of the answers of everyone in the group.
With that in mind, I wrote this function:
def count_union_group_answers(groups): count = 0 for group in groups: if len(group) == 1: # If there’s only one person in the group, # the group’s “yes” answers are that person’s “yes” answers. count += len(group[0]) else: # If there’s more than one person in the group, # the group’s “yes” answers are the union # of everyone’s yes answers. union_of_answers = set(group[0]).union(*group[1:]) count += len(union_of_answers) return count
This function takes advantage of the fact that while Python’s union()
is a set
method, it can take one or more non-set
arguments, as long as it can convert the arguments into a set.
For example, this code:
set(['a', 'b', 'c']).union("def", "ghi")
results in this set:
{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'}
I rearranged the set so that its items would appear in alphabetical order so that it would be easier to read. This is fine, because with sets, there is no order.
Now that I had count_union_group_answers()
, I could apply it to groups
…
count_union_group_answers(groups)
…and get the answer for part one: 6504.
The Day 6 challenge, part two
The challenge
Here’s the text of part two:
As you finish the last group’s customs declaration, you notice that you misread one word in the instructions:
You don’t need to identify the questions to which anyone answered “yes”; you need to identify the questions to which everyone answered “yes”!
Using the same example as above:
abc a b c ab ac a a a a b
This list represents answers from five groups:
- In the first group, everyone (all 1 person) answered “yes” to
3
questions:a
,b
, andc
.- In the second group, there is no question to which everyone answered “yes”.
- In the third group, everyone answered yes to only
1
question,a
. Since some people did not answer “yes” tob
orc
, they don’t count.- In the fourth group, everyone answered yes to only
1
question,a
.- In the fifth group, everyone (all 1 person) answered “yes” to
1
question,b
.In this example, the sum of these counts is
3 + 0 + 1 + 1 + 1
=6
.For each group, count the number of questions to which everyone answered “yes”. What is the sum of those counts?
Strategy
This time, the goal was to get the total of all the “yes” answers for all the groups, keeping in mind that the group is only considered to have answered “yes” to a given question if every person in the group answered “yes” to that question.
Consider a group of three people. If:
- the first person in the group answered “yes” to questions a, b, and c,
- the second person in the group answered “yes” to questions a, e, and f,
- and the third person in the group answered “yes” to questions a, h, and i…
…the the group is considered to have answered yes to question a, and nothing else.
To put it mathematically, a group’s answers was the intersection of the answers of everyone in the group.
All I had to do was tweak the count_union_group_answers()
function from part one to find the intersection of group members’ answers…
def count_intersection_group_answers(groups): count = 0 for group in groups: if len(group) == 1: # If there’s only one person in the group, # all the answers count. count += len(group[0]) else: # If there’s more than one person in the group, # only answers common to all people count. intersection_of_answers = set(group[0]).intersection(*group[1:]) count += len(intersection_of_answers) return count
…and then apply count_intersection_group_answers()
to groups
…
count_intersection_group_answers(groups)
This gave me the answer for part two: 3351.
Recommended reading
- GeeksForGeeks: Sets in Python
- Real Python: Sets in Python
- The official Python documentation: Sets
One reply on “My solution to Advent of Code 2020’s Day 6 challenge, in Python”
Will post here too — did mine in bash:
rm xx*; cat advent6-input.txt | tr “\n” “{” | sed ‘s/{{/}/g’ | sed ‘s/./&\n/g’ | grep -v ‘{‘ | csplit -s -n 4 –suppress-matched – ‘/}/’ ‘{*}’; for i in xx*; do cat $i | tr -s “\n” | sort -u | wc -l ; done | (sed ‘s/$/+/’ | tr -d “\n”; echo 0) | bc
or better formatted with explanation and comments:
general strategy is to get every group into a file with one line per letter as it occurs in the group. this is the meat of the input reformatting code with the tr’s sed’s and grep’s, mostly converting newlines to detect start/end of group (double newline only), then splitting them into seperate files per group with csplit(1).
simple then to just sort the group uniquely and count the unique items per group, add them all up.
rm xx*;
cat advent6-input.txt |
tr “\n” “{” | #newlines to {
sed ‘s/{{/}/g’ | #double newline is end of group, to }
sed ‘s/./&\n/g’ | # newline after every character
grep -v ‘{‘ | # del original input single newlines
csplit -s -n 4 –suppress-matched – ‘/}/’ ‘{*} # split on } into sep files
for i in xx*; do # for every xx0* file created by csplit
cat $i | # every file to stdout
tr -s “\n” | # compress newlines
sort -u | # sort to unique items only
wc -l ; # count # of lines which is # of uniques
done | # output all counts to add them all up
(sed ‘s/$/+/’ | # put + on EOL for bc to add
tr -d “\n”; # delete newlines for bc input
echo 0) | bc # put a 0 on end (so ends in +0) and add!