From 802af0fd8bfa7c126a5fbe5c21f411a3df4712aa Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Wed, 3 Dec 2025 19:00:43 -0800 Subject: [PATCH] Finished solution for day 3! --- 03/README.md | 57 +++++++++ 03/input/battery_banks.txt | 200 ++++++++++++++++++++++++++++++++ 03/input/test_battery_banks.txt | 4 + 03/main.py | 70 +++++++++++ 4 files changed, 331 insertions(+) create mode 100644 03/README.md create mode 100644 03/input/battery_banks.txt create mode 100644 03/input/test_battery_banks.txt create mode 100755 03/main.py diff --git a/03/README.md b/03/README.md new file mode 100644 index 0000000..6de8f7a --- /dev/null +++ b/03/README.md @@ -0,0 +1,57 @@ +# --- Day 3: Lobby --- + +You descend a short staircase, enter the surprisingly vast lobby, and are quickly cleared by the security checkpoint. When you get to the main elevators, however, you discover that each one has a red light above it: they're all offline. + +"Sorry about that," an Elf apologizes as she tinkers with a nearby control panel. "Some kind of electrical surge seems to have fried them. I'll try to get them online soon." + +You explain your need to get further underground. "Well, you could at least take the escalator down to the printing department, not that you'd get much further than that without the elevators working. That is, you could if the escalator weren't also offline." + +"But, don't worry! It's not fried; it just needs power. Maybe you can get it running while I keep working on the elevators." + +There are batteries nearby that can supply emergency power to the escalator for just such an occasion. The batteries are each labeled with their joltage rating, a value from 1 to 9. You make a note of their joltage ratings (your puzzle input). For example: + +```txt +987654321111111 +811111111111119 +234234234234278 +818181911112111 +``` + +The batteries are arranged into banks; each line of digits in your input corresponds to a single bank of batteries. Within each bank, you need to turn on exactly two batteries; the joltage that the bank produces is equal to the number formed by the digits on the batteries you've turned on. For example, if you have a bank like 12345 and you turn on batteries 2 and 4, the bank would produce 24 jolts. (You cannot rearrange batteries.) + +You'll need to find the largest possible joltage each bank can produce. In the above example: + +- In 987654321111111, you can make the largest joltage possible, 98, by turning on the first two batteries. +- In 811111111111119, you can make the largest joltage possible by turning on the batteries labeled 8 and 9, producing 89 jolts. +- In 234234234234278, you can make 78 by turning on the last two batteries (marked 7 and 8). +- In 818181911112111, the largest joltage you can produce is 92. + +The total output joltage is the sum of the maximum joltage from each bank, so in this example, the total output joltage is 98 + 89 + 78 + 92 = 357. + +There are many batteries in front of you. Find the maximum joltage possible from each bank; what is the total output joltage? + +## --- Part Two --- + +The escalator doesn't move. The Elf explains that it probably needs more joltage to overcome the static friction of the system and hits the big red "joltage limit safety override" button. You lose count of the number of times she needs to confirm "yes, I'm sure" and decorate the lobby a bit while you wait. + +Now, you need to make the largest joltage by turning on exactly twelve batteries within each bank. + +The joltage output for the bank is still the number formed by the digits of the batteries you've turned on; the only difference is that now there will be 12 digits in each bank's joltage output instead of two. + +Consider again the example from before: + +987654321111111 +811111111111119 +234234234234278 +818181911112111 + +Now, the joltages are much larger: + +- In 987654321111111, the largest joltage can be found by turning on everything except some 1s at the end to produce 987654321111. +- In the digit sequence 811111111111119, the largest joltage can be found by turning on everything except some 1s, producing 811111111119. +- In 234234234234278, the largest joltage can be found by turning on everything except a 2 battery, a 3 battery, and another 2 battery near the start to produce 434234234278. +- In 818181911112111, the joltage 888911112111 is produced by turning on everything except some 1s near the front. + +The total output joltage is now much larger: 987654321111 + 811111111119 + 434234234278 + 888911112111 = 3121910778619. + +What is the new total output joltage? diff --git a/03/input/battery_banks.txt b/03/input/battery_banks.txt new file mode 100644 index 0000000..847f7a2 --- /dev/null +++ b/03/input/battery_banks.txt @@ -0,0 +1,200 @@ +3643533343334553356323612233523343334333322332347332342233335334343353422313333362232334317333244336 +4245332336454541352336544565547555345334553554644451464613583535342534444543844244442365543555543655 +3132422212822542231313232712224522122222222152525222422222622128232511532642212284422225132272232224 +2215241223232422342222421223212221212233241122225132212232252214222122222213312726326222224222221213 +2454323541241242312123252342413262223122524227324755642223226335224222525242532522131341237182232532 +3452244323454232413326324642422453522232223433232323224224223222142142523224364424534315321214213113 +4343123613344543353433433413523534412213342463244332332433223323532334434334424343133342245432324433 +4333335353333543233622334153353353343343133331333233545335325223332433232332333183331334324233453333 +5221656657657454555445353546666565628552365426252565134463666546264553645654555513555651233975613625 +2116141332321232223222212522222122227236224235212322225337313252432122333412212222413225223222222212 +4553674352261634265243545422164775734346745365643325643442326325462452223443436243553466664583644525 +5694474334336972226344635423238362745765545252444595123722563342242521627572532422244462513822682728 +2222212222433211266212232123422322242212222222422222212423314222222122222222242212122224222222322221 +3774232456333925533524324337452565753553425336345556337593745432667567443364243522433535712335357597 +2842282362252223222353913523312222642212231243826332661492424625321242225623222422223527133322329223 +5753554657954494747845264953599653865369346773856644395744384677435448565955886434655447964835544764 +5325515363525323557227552346371327416252464642135223132612315522614636435612624444345445562422355244 +4454442442674343865334643464333365124547338533343445444624452534473384444442348593534175343362543587 +2222212311232222224131422222241222332622311285222422222152222222422112322222121122224314412224252122 +2322344222211333233333333322342124354133132133132431443243332232233433323363334213243423432232534133 +9875947797472655647644547457977786453545827444427128777538928224886776457893558844655675698755474468 +2522222232822122223222222213112322522422222222211242146225322222222242235224462322423222612235322112 +5534554448355354544455443345446543424454334743256355553652534554444352254453253524355545344343545353 +3231222232231223122223241322112222222223322131222232221123122223322422132221132112332212222121232213 +3423336544555663725355326335543514556354369566752575594544543543645323653777332445554674551543476322 +2232522332221524231255323322223324422233123432322153133222223522232342221222225321222223122251232433 +6232742271431442224333633355335368235311354221624235333332233553531231234412536754353355143533424624 +6226212212212256332122222314623222224223671621373222212232627322222332233522262232121123242223322223 +4235354352365455524345444552353524214455231522324254553354635543533224245545562324212452422253355531 +6544354364934464344436445424634314444474645444844362435646254654434744434444438346326454444336645435 +5353543344425244345215461733334454444444333443553333552324444452243332433453233354343153333572444454 +3412422355262233212233222352151332124513222234355222221252362523153533353342222243233316363135325722 +1777672767684287255766672434264546367665526663637671554427647612355545678765653359747667635972654716 +3135333542556443425336417136872325253542322923235415253134553252242252123242555364622525533227621376 +5337431863346397345445456474333564499353126455343234125338364635771575533434543263489689184338244443 +2232223122124212232325324262422236221551562212217422223223361221223143142211122226345265465226236231 +5556763473474857227375653536735357747775363375573646447476677652575363554765545545575567447747877698 +4435555445472433333443423544433511432252523233467422343534346164443115153332534322512424132326355533 +3534343635643244624421222243423313231336441364331224414323425854335334333424634542533833332433224213 +4422324342222337523432563245323855213453122323356342475224341242532823328541454323232213222223252242 +5443152332223233242442113323322315433122344223222242424422224132622415453224212222224144122922523243 +3513233432524323434332234245342343423323223343233334313433353234324322343333331313243433223433413342 +5427235522741635224235222122223252224232287222221422523523222325622312225623222225234622222322222532 +7232523322822733314364315324244221329533231421342223233152332231431322231362333455273636324339192421 +2223224322222462243431222111222322323222222325224222232132332513152332221122321623311221122222222221 +2222312232222322252222222122222121221222111253422122213212231152122223223122322212212221121332213221 +1333256265236423344323334545432546315638313575535323315233544333236343452322433544333333523543857333 +3646344664454555465441444446456633444331446656646464535646448655533545345462545459164444344464565442 +3343332532332233232333332445323632214261422132323323123621322346333343142153224334323222222321232133 +3333231373337253343327523213231724415375222154331234523334365225334323222343332326334234243322533132 +3844632335233445231212832172332234222122321521226726212332424522347342222441383223412124432622275421 +2324233433234422121331213121131122242213322334224232212222233223222421233334222122222132121221211122 +7355664266665657666775647586436277596677866367569647752766665667956867467877647797796756738668857567 +4344523332424223243213222423323233223331434223322331323342322323232334342322112324423223322332353334 +9685786757498866799685757688758487758556757476669576776694586786869537766675696758677598699677558466 +2243142444425434543434552343324545432224353555637522344154416433245244613446642455326254255535632352 +5232333213123372223222522223222232221323233251328225221252541221232222322132321224123222132232222262 +4364735464653323384822824622463636234374656352854335147622254467564531624337223567536448442452573644 +2232233232322322235233332312512533242463233435621543233223323223512362224234411224435321233423343213 +5881735621434428562273225347467726224333847858238174812323399123168264252736666254214242627323723564 +2222221221212342122122222512222172622113221226112524224222455732222223252211222632122212222113232523 +3513233653334323323233353245223333533263213233324423332123432254411222322323222332325382315333343329 +4474544643454554864458572351344473767437244253554453533445544654654573443543744443445334445537448333 +5223213223327262242222228252222522223222122222222222132522122252112334221222227232223522252212621722 +2282429272253155555624461243422555545222564454235515164592235268315436733511872241233311356245225931 +7557577884786766778497983869679888887875577777775668998747676779579754784777799667968377587889776776 +5262464426277647186433725265636242523235823822133762524651224124654832215222652637455733324252343127 +2252314552232254272243524253352222521247225453122413522253335321145222242332422342141522222225226325 +1422122823214431212343212522323332423442152442273241452242142434223214272172232222442422321252112419 +1525323455454225533552354116462254523235355623651554125627324422345664443536213332452632241534523754 +2423212325528122412212132422124523215252223324322222322222225221213222222432312222552232222523321134 +3235455452451433525754523223635555443355455342445445252443133424423553345436555224244323532354546524 +2225426321323144212312232123231222313222513321262722422122123112221112623222315321232222712322243211 +3323744313651336442542244325641323333544364253353245326387245424435444212125245345145243148322253333 +4224221525222322621278411312322251331345522122222322227323251333512312262222523234526223342232212224 +2866388648221635468642657666674252428635753785542663244656546668522694932745465146232222537356684845 +2132112322421321222212223212222122122222221112122141232221222122222223222413232212312322222132222222 +2228222261226262233311234313222222181222332222214322432542211122232242222223336222333121177221223122 +4474497489835964745735843354532568683777547375733573467656536334742553433324277563694487364736557357 +3432333353332333333333123213543333332313332442423433322243334322235273332313244137244333353421332334 +3223146141433235433623339333183734222331373327332853235273243336933337737962135623233739323626643532 +2173572337835432382353443319231543415354874433353946265686357781342135522553354764583275433333653595 +1453347561277434235478374222466337462424221429222444332422227333465511452712453644652443242244243256 +2252314332244352422228473237452225452523122442251225244333142471455653542462622222624212234121527423 +2215322233232222322422222522222134253322532222222122242322222211332221222223522223422222255222222222 +4579264435717882322422463184492122622644477433478374644693444426189222224748844688241442239323525889 +2133131454343333333323334333333233233511242335433123433332311432252333222423132233331512334134232334 +6147155816162151778162378548751588455436588875825147476634212743184886242318384628657772363465633469 +2432513227322211222221443113422132142252231231222332324232222222522232622224231212321242221225322831 +4344468323243222633441387247433448444464443422354561484442344234233435444124242234673444423444462644 +2245221232211212252912532123434224222122254122422222225245221122412224222714555212222212332222525162 +6937597777769682797565886474786756676876875978765699549696768546869767785965977733758729685777877967 +7213122222221232142226434121142242222525223225411113313421242232543222122434222342522923123244232523 +4525876664685726366641962568493654988254652446466686778565659251666624716636455647566766247465558644 +2323411522233242455422213222322242622252242224223342432321122133474242333333213213313324232332322411 +4237252966662184343257663775467446355274488722694335748786477666176666456445566547517928755544385583 +2565313565474553645443333233342834534323364536625324435343463335144446333553431354333351533635632482 +2538615442335544757756436673557335533346965657315393434363735453323435275566496455834354655454596453 +9554473553395423522332743425528542222223237443535348853435433234337324163535553139847537155752525328 +2261243342431244234754356434224114232724334633643256124235262432457254323174354336424364444124734343 +6665363657825645245534342368452745676985486456986655657354552657863654667354467453465855446654355586 +2174242636167553351523656762552146324756646372511472211611445251772723646563537665455416357676675489 +4244323533431424223168332322331422414342255326454324332235441323843324436232243443624434223283334344 +4343421454232434146465624331522616442435235312325423166233561232413615121662145363266353163444315789 +9282727222322142584324424444517317255535742436453527234351223421242955152145424635232311242334326384 +3212125524123222233222122232133112222223222222121712232216221223122222232223222223222222223223331222 +5343558454444555335544445435454154445455464434545332455463435445645553553552454544144454942464443524 +5555345235533344344655255434445538536733243524323734333565333673653262454149232436524453544575734545 +2121212221222222432322124225242222333321222224321224222322222221222122222422222222221232222232122222 +7345834223349333434648335134333335456243331445842433143543346474336343333253135253256435333439223433 +5333555414323642334151212225322252422142521233221112572314252222233232222224333233242612222422723512 +4555644554425545556646523535576435562345544443446355335585765275544555535567145675586557495557555472 +3233331432331343233333333233343332341363214311123222337344334442333333323543343234232333434933333233 +3322376333212373232333212323344333333232323333333336232321332233362333322221325353333383342233331323 +1423222226242154221324222122624122221223223923222222242212342222922222322222326322212121133429323222 +2121224222213342222221211242221123254221226422225322421213512225822132232225222212215212214222122222 +5736535374343325533212577441433234453335333363384222334433322324523363153252373344226753333323313235 +5575232615542451243534244862342154211523134242635762464326224352515452212662343335852132122521456244 +7578456547647344554357444555514754564764444137286632763553653777452954494339554346487856432768745347 +3233241532223242113642436254345215345526532822133355231341555152263434722539554192356242221141343523 +1422422222221142222324172221222221322123324222122311122322423221244222132322222223213221222223122131 +2577646666156266665684345458946433655626536675346465666664666566765676566556544763435665576346746784 +2132443123232342122232222222122242331225322423122423324244313243125425152124322133323145354332242221 +3212222211212242222343121222322352226222113363172221253222227224232221111222222122122252222422223222 +3225122412226223212323422224133113324232422213122322212312222322341223242224232222433242222224242232 +3417353333616537621425643343453266633724675676513332525323133333253362331346223583393228262522433226 +2324435333433234433342335283342433232333322433533353443322242312333323333344323438133455238233323333 +5833534733433743734743342456315543535553334345634535342355543545254455344245344644326343233334922385 +2522232222136531623122121221624332231416224233222324321232422235523212232324212222372225231222314231 +3433342314333243436433333344433234633433443331433336652343622442334333324433342233233334331445333362 +3332442242212222122222323212242311233212422322233241244422224423223233212422212224323122222222232432 +2222122217221222264222232234227353222622223242222228222321621352222241242233323122364342243432221222 +3545524535744155431345522353352423582213652343522223523423435553634411335223135545342251245336234333 +6232256627222433273623332647412245548122352676934722339316276573222322733336313632416151423358227152 +6748737433323674735434557548774442436234551357445646674367465543537353468855567343355526535463876537 +2112311123343312124244222211222213254232222223321133321244342424222221522433244532423222244234421332 +6343454644535423563463336312434532433234275435434422343263484325533334434563521442444524266552254345 +4464543735432325527373322322245333112222241284232335162345236442212122332122532244315412532262525372 +2422111224122332124344124424214224112412312144443424124441443211314234424214323443422243143224156789 +4348332622165244265243979343752428221867869437326673722549262336224882632443713192456443482423282389 +5142463625323658674625452653365547545345333425774465555676425454556573623162573264467556623459346476 +3232622574221321231312233222223221323162213262232722431213231312112223523221225233522316231323232363 +2322533221364222334134232234324334323228222233132213322222271122232232223232272122424226254242132351 +5775557385855746385639673686474366636564865654164685753785937683668656555646364544966795476832482384 +4343233212322642342252223214263243242532232522213324322732223222529611532322221324242117223334322223 +3236314421243232342231234117233211321393331233411232232133232123323923223235223242233322236223224241 +3543336443644634353426453641582248246533343723285542554333615537356345433554326554433565623243386133 +4533423242324337442444534244343546121434444453454345522656754342543534435444643444554543243525434434 +3643654425644448445335855754334514446575436522561653556555343575663753333747354574366434326426334633 +3443244221326226323233244212443226311324533424323224124363224444232514233234363333322132124343225312 +3223322343363334234383343344433323144444335333353453244353323394383445233443237433343434446334634333 +3113632272232323444363566316832563573565463233233634734339423443326234436213391643535455743544433424 +4224222262214222321121122323424284414422432212222321432723212124324322422143232114213226252232721224 +2323262222321221222242212424221122221222242342222222224321422222252212223224222242223222222321242221 +4322834683464333323445343224346434684733456532326624514432645658335765454158433384733264344652575474 +3222222242212222212222223232233222212122222212422221224231232222222212222222131222212223222222232242 +6224225222325222332232333624532222332335626222321136423233413141624222232132122232332623522313162312 +2334361331622222223222234232333313111233323144432512232343333321232242223421332312332341234532124344 +3225434443444334242222136333235432437235412444323461672443642323663442321441153453522264232446324132 +2243314315411421515212531413424133541115431212135425423431545554313125143525242321334322515241316789 +2537623656587467377751777387435682368446474434126277457976746277376232575362352347775775334674523353 +8764222334232465252225621549236546253421792327752232257256545466514729345436422293374255222442335262 +4453132422541232252534223451322435238533232312242824312635555675247472222232573323733214452223433412 +2224342222323135254342223334432232223332244233433222222244222152564122426344132242322354422344334222 +2736622546664653345565546734466756355665635465126655464485657364556666545555355655545694365447347798 +2222115222332223431121553343221225112422412223122322422352134122241322322132232423232152222212324212 +1229333112311323232332332222323232232332532321232232234332413312234323323222233332243233343333453344 +7533864633432334834422245353344473112432446261334362243534462243343742313227555384474458423423335432 +7337594434447643536444421644523646355624843444444254514824337524734468533323444439453363332343553333 +3335323623333433333332332536343433232823323332352335344433822235379626433323333331337317234376532433 +6697865656558666557855446563645668465598866855766653557659565877594565666658765687649478755966866756 +2972213723232333835332283332133545316223332325323333872433363333232433333634733243323335335233432325 +4335433323233333123433334332434354322233123332434323534344353233334333443333344333332424417332433544 +4233322222223632333232223221232334444331424333243212322721324242343243134242232232242223443346322524 +5832331146877323342542227373841232774576331428736413464343382731335836423333223342441221424432333134 +2322223132131223223233382252172413612422162224122532213324332533224259232323124523231212232322144212 +4534472414244334544238454744746358744344593432446443444421444454722854853333652243334333532224443224 +5112222222222225234212221221222212336122222122122222211322222132312422322223312222222221222221222221 +6533334364333392352662395337363534334434354336554314334457442355335224732365637666331532346634336434 +5934647853553585633376657484463947344567345345455355485448646445444877844466854485558473978785455627 +4335832532322797325727271722352222263323472322423414332642713222234444468322122323454312323243213282 +2223333342724343333233322523432222333233222243632334233222322132343222434339324343321323332341223233 +2345422223132244162562512444423442322423412224222222342331422132233224243443221222222322232443423224 +5133382343353463341663242343653533233323131335346663354834372533333332333233926553462213343572423133 +2322342223233521123225262222224216232222222235322322531121423252346174222322322312222432221414322211 +3253343344355482545438423535434244344625424441345444324653644455442343332444453235234235434443343452 +4346347872663633346453834882647887238753543734632464434444479745549263384448713833486837668624234553 +5226522554342333532241745245322552133543455314253243363452322211154314268211526313234363233542444536 +3522843565111922443144833822225372311353263225529125622227545523214812252122992654262557344522526127 +4354667557554752665134555635766747497575437455432564637547776765242676654776355466547755333857397335 +6533412733323543355423473226534542383133222336233441451732233334323563324332433222233363233358643533 +8354584547979575554784657655458775266978754955348636463625947354332769877664966544568889533655685448 +5532748224222153222234312452322233243373446672233233141213332344343422364122342533323888147253333436 +4643814452834434754665543444434534437444534445474425483445447545444334344474444244452844446343347444 +8454726851123747735433743113385758323352583263647722725858484335475233533265739526245571368825735335 +3333213211223333222433223213331322262223214322331125222331432223152322233324232235132212331321613233 +5454493125422345444552335632255424471425534425222143323544275455445565445214424541234698452325351354 +2258227824814793238421822294782262328722223842298473672283733413552231334841641267534753339324269765 +5552554554453333624133425223413283441422435545275151253553454554544454223545452532454354154446143532 +2325853123224336424153434532433322345421325276142232223244323332122222311243153442334324245452324243 diff --git a/03/input/test_battery_banks.txt b/03/input/test_battery_banks.txt new file mode 100644 index 0000000..6b1df6a --- /dev/null +++ b/03/input/test_battery_banks.txt @@ -0,0 +1,4 @@ +987654321111111 +811111111111119 +234234234234278 +818181911112111 \ No newline at end of file diff --git a/03/main.py b/03/main.py new file mode 100755 index 0000000..9d3b962 --- /dev/null +++ b/03/main.py @@ -0,0 +1,70 @@ +#!/bin/python + +from logging import debug, DEBUG, basicConfig +from sys import argv + + +def parse_input(input_filepath: str) -> list[str]: + battery_banks: list[str] = [] + + with open(file=input_filepath, mode="r") as input_file: + input_data: list[str] = input_file.readlines() + + debug(f"\n\nRAW INPUT: {input_data}\n\n") + + for line in input_data: + battery_banks.append(line.strip()) + + return battery_banks + + +def get_highest_joltage(battery_bank: str, digits: int) -> int: + battery_list: list[tuple[str, int]] = [("0", -1)] * digits + previous = 0 + + for d in range(digits): + for i in range( + battery_list[previous][1] + 1, len(battery_bank) - (digits - d - 1) + ): + if int(battery_bank[i]) > int(battery_list[d][0]): + battery_list[d] = (battery_bank[i], i) + previous = d + + return int("".join([x[0] for x in battery_list])) + + +def get_highest_joltages(battery_banks: list[str], digits: int) -> list[int]: + joltages: list[int] = [] + + for battery_bank in battery_banks: + joltages.append(get_highest_joltage(battery_bank, digits)) + + return joltages + + +def main() -> None: + input_filepath = "input/battery_banks.txt" + input_battery_banks = parse_input(input_filepath) + + debug(f"\n\nPARSED BATTERY BANKS: {input_battery_banks}\n\n") + + digits = 2 + joltages = get_highest_joltages(input_battery_banks, digits) + + debug(f"\n\nJOLTAGES IDENTIFIED: {joltages}\n\n") + print(f"Sum of joltages from {digits} batteries: {sum(joltages)}") + + digits = 12 + joltages = get_highest_joltages(input_battery_banks, digits) + + debug(f"\n\nJOLTAGES IDENTIFIED: {joltages}\n\n") + print(f"Sum of joltages from {digits} batteries: {sum(joltages)}") + + return + + +if __name__ == "__main__": + if "-d" in argv or "--debug" in argv: + basicConfig(filename="debug.log", level=DEBUG) + main() + exit(0)