From 6ad529cd31df76bf7e6002d5d6520af6d8e506c6 Mon Sep 17 00:00:00 2001 From: Donald Ball Date: Sat, 6 Dec 2025 11:29:00 -0500 Subject: [PATCH] day3 --- day3/input0.txt | 4 + day3/input1.txt | 200 ++++++++++++++++++++++++++++++++++++++++++++++++ day3/main.go | 147 +++++++++++++++++++++++++++++++++++ 3 files changed, 351 insertions(+) create mode 100644 day3/input0.txt create mode 100644 day3/input1.txt create mode 100644 day3/main.go diff --git a/day3/input0.txt b/day3/input0.txt new file mode 100644 index 0000000..6b1df6a --- /dev/null +++ b/day3/input0.txt @@ -0,0 +1,4 @@ +987654321111111 +811111111111119 +234234234234278 +818181911112111 \ No newline at end of file diff --git a/day3/input1.txt b/day3/input1.txt new file mode 100644 index 0000000..a4db573 --- /dev/null +++ b/day3/input1.txt @@ -0,0 +1,200 @@ +2252522232121122125212322341424262435212421332333533223124122242222222112222222222423222112211212132 +4243411423211141423114142211242211313243142414412343314441331444324234332241124232434314434334456789 +2345424216235354235454854465356531424386646663353345325863344653443225833345272432733245254123233555 +2222254221221322323251221422122222222913213312413322232222233312242241212222211222221231522123222321 +3325223333143325613631334413332223343313325235244225334333436234347343343233333423464343343122752313 +3242443323221134431342122242212346227132332421443253322321322334232124233222231124244313234213132526 +1234121211443342212221221322221522122242328222121312231315222223322221123221422123222223322232222212 +4453323344334434344323333343543424435433334433954334352443441343344243444243322342433443534344434223 +5474276335464423565437273475752354666561567964574443674757242677132854532476246235614444754448776257 +3323452233234434344353441341345333242333543354253334334523443334235333534343442254233353233412333535 +2532122222226222222221224422323222224122727212251421322211752421212222232222232235124342252222223363 +1254324323843512624123233512222283421332422152481373421252325148224492222223232152322122222542222232 +1222223234222222924222322222322322312232213322212312223112322232221242122322221222222222222222216231 +6975549447563635765855586445845455558858445455455654554959743586354559733567677694859557557755965555 +2355899345525328166273721762337769447325354323737373364764746612525724723535548526544676177856542227 +4341212622222431424212144254124351322542272252214232432212322222121122422221223222236222222324112221 +4212223232322132333333232332222222231121652322233232363131333331231236323322224233223252213435123222 +5648577426568837859575584663754875685664686555964674845885368385691923865857663936884363954747734753 +5523536465955333656658133557664777945532467536335626552766567427573556316436443556654536567464366561 +1826464511756411543323441323373432722536324265424653146463156264733223371141222252213214325333364262 +4364414354564473352423315876164463327536469132433336845616266735283552653184443365126553544355656347 +2854678943826969694277816944874848873975596766535832719596865482421668636975627587698929495786637695 +2222223223215223223322132552142131221233412422211312324422234428342244422223212224523124412322327334 +5441223235425662243525654422465344234242424556541243421364342633354444244252221135422426342495432642 +4232122322212322542233222221221212222211521221222232122222222122422335351222222124123223231223222214 +3432775726725436376664456643542637425375244243425128374655626277694745452347243855686496752521626735 +2242543322251233253223323433322713132222353223212222224122322242522233232222122212223312122322233312 +3454263256248445748432439532747467624844553273573335862762518226533349368465356544636463852447352635 +3623466442522132524453252313522746737561344422254433424443432234232254439215169345126222242332524562 +2443474234245332332347345214444774643433354222342431645342551222527537345631434444422224132142144743 +3746536522327444323165929274547469823446284434743541365484273473422852824254245774332464523844444831 +3335263432495232514272346536243634333233234344624432423521136439333363335543726123233332363637635933 +3221225222212231223141223223224121333232121232211222421222222225221413333231222222222221122222222131 +1222235216235445222142234223554422224533332241542244422221146221431212252311522232142554222535222223 +3232522211434322314322333283413443322243752233233523327321221222231313232333222234143143223243333242 +3237343122762236131322333633337443353313353536322323225277653323322621534333455142263155613223362263 +1433342423438642634335333224353411444342343645333333332635142443322242434326433542334354144344324235 +1112322333222313335222252233224222222222213122222333222222223133222232221233122433332123141222345312 +3344423723323447734746383644737444336434733434322487332442251513973244335428322734244632444364425345 +4745225249233353582262575548313352221344832444572672465853592365484725636653934142835714418134625743 +8466765685973979963574745876677476797866765676576636567468688598668568647486785529685763542668778669 +3212212132222221224222324242322523217112222222422221122223222372222223122322322122142331232221522223 +5668456654767558837567865555674664641758873966635455359874568935557655688515558693588775447655556596 +5234731722235517843345322423264675163524751562554242425445443324626642434385444122552594242374392256 +2222223143222422323523142312132213234242112122333322224222221122221423342222342323342122214123212242 +2342533665534434343646363321345748353473353793768434424458444243363386457376746239387348323347751732 +3311242323131434225215553263262127363542254531552222283234356113255312122252563222523533436322641722 +1959743436627367547698757689745296752754249362876773664677758776466859779768544462783975322853948278 +4437734445234436355443762333432345341363433333684453433354644435443635344755444352434344663337475267 +4371235464353317643333733472447633263546733755343524334324254342523537744133473695432282383263866457 +2232233221222322322233622133222342111322223332225223224134122122323132213415233522232333326233633242 +3252113232154245523221553521624632554226292226322133655234222144243351326327322122563143533514232314 +2322122352262241237213523123252232225123232332321624323222532422224231224271323283227224534231332228 +3664465886644756625234356562253466574544263535566724336666666346763448428172674838655493666545546666 +1222222611422722232312122221122222221212213522222212221121332211221242222225211421221222222322222222 +5386323633862222251286133262232228238283232222224925223122235232221232214626434222442622235522223682 +6435643647656656735477346565665666646565564848655645246636666666566566434657234666856496842666667555 +5121445466446462533514563251513525273225225246653624622222557655635222346625224354214623625435342256 +8628583845447667446664638445465546847877552767244556766574668476342476756454436645477266363394476447 +3411252542231113232425145444241554533144143545424451334415353342513345222445431112343312114344146789 +1545412531325222232341325243345615245215222212415131337254413524245422452344412332222254322233324235 +6334211633452222226626262122256522331223222122453662325623262423732412223638252132226222222223322212 +3512337233297211233633333321273293434532297643661114222333553338235859436422342252926383223342316263 +8442343366422248246287747753613373647224382245532312637224852872367358361429739338787777561966766761 +2363333354532425332344213246142622142232234343412481244324313133434332334212432223223642244333433243 +5531222212224326122122222324532312132512323232722422222121222221232232315522424322252326524212222353 +3222248754225353245155547243242523245415566644325854246412572552453455742115424632253553532733653553 +3141851522674612841746865862124642216746487136287718443278565225856236448582461756387155234356328819 +3132242323632237324425342445232522422242435443231246542752413434333327433223642242433324542224437334 +2321223321123221113322222223223132222122222225223277323321333333222233333332233132222312322312121332 +2242349132551221233212625422522512234225221217442422641526222327222221222514124148222442212222222222 +2434334424324423435244442423233424246332334434332443483324411463344333443253444711236144331243562325 +3122214122322222443211213362421236232215222221133342554325212532444222552441364324231423443222424242 +2254313233342232224244132333214213363332422233223343232413343346332334132323232332331222323233223332 +8234712633872422175458368372454646755375578244443454255842488473826462288574864377234517386362548376 +2231122322212132212222221311222222152332122333321342121231222122221222223142222221316232323122212221 +3422125232927343353233323361342441322242242332323223351332224324382534432378234272946333232522233341 +2443543233333533573535523271374445525441543334434433546433333374353541323333433531653235233535559533 +2627221272254221229723255324332447525525762422173262672742623252622322442324542426226344572352223424 +2474225232215222661236432252266422662164643214576225224242235442523263224211454545412621574222362325 +1212222222222223322123222222122221222522233213212221222222222213122222322222242222223231222222222232 +3546242746233374138556521236663431363653314266156335616346333453642436527574226536463535245534656586 +4737579738883783836522645562639448786437227536826626623645464624375718858395875378463423616584356366 +3234224324334724333722233332221242433224242383341223223231543421333252346242147433133143312933427243 +5555554853324354836452543757463375341532533358338252336524531534944363244245844625672324364133711244 +1342275724123538233335643433352332222222645583683335347925348553373244433225622133525322331646733765 +2222221212131112222321231112121121112112211322121222223322231322122132224222231122231353232112232222 +4463622672336947655461687255647363483763335233634366964333246334353336374354553146643232423623363913 +6431325335513252333533138313445432513261532134235325232352322423234232533352254521422332313342244235 +2115772625542375244752436365766633546214172445163346627247121757733246625727117652542166362317121789 +7338243233223233514313323223232313432223344343331232223323222332334123333233332221321232323332363624 +2263382232772272466743474334634357253277526272483833833664246472524128343523488842837447483263726433 +5223124211224441234126152244332326332233223525251113316442265316322222222443225684537252324243541334 +3727144455434265445253348444563724357745446344934144224432428196654635264627534456526423243442574335 +2322223322323222313213212213333222222141132233223232321232232121213532353222323222332123222332221232 +3372647783464355462543268684626364277637646254935697433645873312922221433234763725474725522373836522 +2212222122141225222323225252223123221222213221222221222222224112122422221232222122222222112222212112 +5431335842533273322223332234333424455523132544243523343463324551333753233342332433383153533473333322 +6358115145724567453554488577236365533824747671533822657425657836639542624125685646644625356452153552 +3432323364463433333223343432333233334333334343334233232333343332322333333313333333333233333333333231 +4395365346467273336347357537349324544962326771324429676875267363676463322667665175135676141375367432 +4343325444454334244145345494442474324332433333543135435646338644353443543844432462434233461534243233 +4344332222244123223222123242212233222222353323321142562222232352432223121333324327242132214342222232 +2434442221212224262211784291242966253562415625442323244681132221626234582252242342743562252233422222 +7826555383235539395355736463844546379536565944535769667322352594587667686336657234744675364565444635 +8586877768867899556767773872766577887884977474678757868484568647677779774778577844485978878677768897 +3333326332133374332733333344243334233343333324323233433338433232333233134323333133233333333323333333 +1542321131231212442223512226442612222323222623142524262425111322224422335422622422325435244253223222 +2224212381222222124532332432344321114331242232222353422323222422212242237324352312222221331313332312 +3314333233241433735333333355332573764353433323333125343463436323323232453235323443333324333322473533 +2453161323622648231327226323422121532662417225662247422222522246252227129623223554233214221444732253 +3238424425421282452153222337622424243432222431423222824454421412285344492222752246393842431223222213 +6692347237744443239697634335434591957955636476833963538764352394536749457327866645443454426758534454 +3313222234127222132633122221831333312222811342223222432312424126222232222223222242422222223262223322 +1222323232142223222622223222622132235324342522222412212232226324332232243254221232223222134424212232 +2333322221245323223221143221232333212222222333122232212123131212222222212322422222213232313331422222 +4464443414432427426142324434743474243442453245144523432243711334533243733444558433235223438438322242 +1143434221233331314222112423323131414143232332331223373323224343233244222422224322212222371114123232 +4553532582231343652135434333433434222425736324333833553233255536535351331238525743335532232355626332 +3233725323335428332222233332212222322232312343422261221332132222443323323312382322622172222333233522 +8363332141224212532222252322325226323222132366222271224733722245223326332293326332233517323646622723 +3432533352334132333133132323441365153223133333322343745332342373549241533234323333234245426322523233 +2122536546542233225221232532135544222234422226123142444213222534353222222232423272225122231151422221 +2241411223322222322134322222422224142224243244211223122412724221243444333244222122112121231324228422 +3242175225351221356613222137222322221512221136311223353322122162222233333322223712132222331121321214 +3227222271772222142229222266127222625122353222226222222222666842242329231222226222252232992222422222 +3122244135766512522435277523421273153252434263263316524166322361257552732232224222222522352342315734 +1433444448132563443442345332364524574541412535434541233824553433412634337337323425744424424253274244 +3133222262232433332324356332233332114383122636233232632333627233323133312316233462354125394323222333 +3433333234333333343333433535113333333323333333733332533334332333333322423322333322314133332331721224 +5514414644443464634444443646444344326374444242335354434444424344444242145234144343534334444544433342 +2343453434145464535423444442234321333354214533245446453545354843454434544245244336434345344354634824 +3322232121432222322332222213361225467422242233226233333223322236613322422216126621324321451422432135 +2211232222422116223622411511232223523112242122231222223222312252342122721457212322223223326831521111 +4949222454241447611224531124453243632421242439512531231344212452277422623352422352422523333722437263 +3262512425212355412151124226223326243556222265222522114145223211641215123226262526643622144262224125 +3142222242221222121222142222231221232112212222221122422132222211122262132122222222211221232321211233 +1834532223331343143232233223423533363434232433514423463632524216223622266325315636333264334534533419 +2222722224123422222222232235333222262212222121212242222222522225212222522261223226142622222221364122 +4212222264642916228432412254476642665322412354271412654252434373442333226152271424132641213462452843 +4545413651453431333334324434544433333242344333341332332333244433464343532336434433443421344335344434 +2322222321322223322322222332222222212223312221222232233222122212242213222124224222321122222223132322 +3565232837324332333233313223232232834273133217383247638324667723232321733132612333374335833332232333 +3322422139323222431145932928455542231234462156124226864122452328585222234334211524328444242222235971 +2423423321334232322225232131222231422134162522123241322341122223212223233353221222133324532223423223 +2356463444667554466644658446433145185263634355774666546536465443475665655365644456654346246736473666 +4945751528648854597949845588447153564455893555554454557453337851568754525939244566444535575678556445 +6723443343332532363932264434544334374235232767332634532372333632773355373733334343363775355435534647 +2334131451832232252422362236251484512213252452436522362152123243453312423411134241242212315584222242 +3235321222122213151252552111133222221222224121212323233141312422112121251212112122222222343322222242 +4675147687445465485357785986564484452648689484794554973485758588934386477574472833187267555564588475 +4324212234221622122414123122222161216262222212222376221223422132322213443252622221212214211333443222 +2124142422512223334352153322222331523253262522322222223243282323322242227135222222223112213322352153 +6422244212161224222222522221532222252222223222225522156661652524214642326224225242222212122221232246 +7254322321672272821222251222222222222773221222232237223222212312262224222632222182227657323622232121 +8637266348328647468251268728247225664737624427233446775546872662552248837842257645745438177159688749 +6223554454444122254122242634232422412224164234224242222342322222221233416512221224422842321523223423 +4225211221132212243123222121233212222222212221622232313142121122332222322222552322222222191212222222 +2323462221722111248172123531222233352126382521122473227522232311212234225238122612223418222222233223 +2237223422212242222226232322252212643222221222221322842433222223233221232625319352222123233552323242 +5575573866776756235774524723677646197774238427577125743636536773319268546663435397425429875433766675 +6377325396348965537739533385765735452653847356663757556736567532757657758633555767656735637537355472 +1234242322222335243323133423331311422432121132142122232234242222232132322221224424113224242224111322 +5523313622352424232343232233542223364231351332312231323333323322533362333323383213333336374513232236 +4652444244463544433364433442421223632623436533353142323444333474335445643534523235443443433153344333 +7412436422778894959772766762745323569424345656896652676687647524793672648346644477257667376564563542 +6535423547244152755263233532232431457424634353344376313346334433434344634446465444613543555444233332 +4456664257544554553454365946556684556573545644643535545464452433454556583592553985736645565557563745 +6512262252624512732332325925745242423324215342477222227215225633473251152511324221322522122235221246 +5442264334253632323443264373383332232243373343443333232333383166133533343224262353353332633244352333 +5475534858434854567757745655241357647772315357635523584557577447775566555376763456364379756464767656 +3232214351465321364662433646255743573433725462842436438353612326342425632637362267415322334624666423 +3464373523637235716243315322477425153433545333554331433334263763747443443283438383335663536545352584 +3322255222322421213332231223336212323412423323342323123222633232333234321323212123311223232623231622 +2222342424524632252523142271612112224231627722343322244622222225226115123244323222152225622112215254 +2221242142242523432334123212342253212442232222212422244324312212222231223422442423422222222332132324 +4252222322322323232213232243523222222222323222712223333213325322221512252521335523222221452233122252 +5292228233145322122233221264322244422322663245333811352623259212322223114234524336222252373234426343 +1533382682272393278318714633884422331463823345514676353832365366343724337734372438678354355673753338 +6333243536333322833864382656624874354672454646424742368592613866462546343624242324363135256465746654 +2172639721312211222121222324222123223112142333214222422322222233333222221222222322521222281122232252 +4638857823366363454267562443674643272245676682532676632713725537447443744564352673355834824443823476 +3561435131555526454514643235221634436544245654415426344166313143346116366252655421113253156216234789 +2212642522222222122432212324512426622211318222122772229823112621222313222325223224234212434231232824 +2214464434354227262635512364212151223432622231432245435222273825213554222262335352576264373722123221 +2232225316422521573112222242225322872312232223261422251265241321122212232222332222323473722222172332 +3314435534335533248335422223373322332385344332434632264236573333227573333334241232333473565332724436 +2213322124226225282244122321222422333322231242232422214331332224424134442212122222142242118223412141 +4344254413543453154545523445435433454544444341341812552555333633243345314525854213435452356329546553 +3225425531622224222122362422413352122224624663233553344244732442882421223223232142226445431453333531 +5243632342133444532623224231444445381445225623248162433244582655238243223433324321361351355528244435 +8322752522223511222162214222227222232221622263325121222221727242262732222125125521264232212222764143 +4522212252224126593231226422321225422413352323222322212322232324234452521216212223223424222522126222 +5591235344322323633413633223612343322343543132322215243542131522465512233427362655532315345262182153 +2656483212362222282366552121133263324442422561633521251322452313548422123693152232757117522221233712 +2242453322442226545322312222144212244224252224244251422222234422223212443123252122221222225142222242 +2621335234442325265225531223352222466663562422221423354521422131435373155524246254221221412223266225 +5242222361522332422322322472123532123433323313222342244243223322223411333335432342232313323421221232 +8893564555879495445828753892279594873596593565543456944794468835648924855534755338356398649645934746 +6326254656835294345562595633473644538653475339934927723274363437353233677374453343363338875334375625 \ No newline at end of file diff --git a/day3/main.go b/day3/main.go new file mode 100644 index 0000000..46ede7d --- /dev/null +++ b/day3/main.go @@ -0,0 +1,147 @@ +package main + +import ( + "fmt" + "os" + "strings" +) + +func readLines(filename string) []string { + raw, err := os.ReadFile(filename) + if err != nil { + panic(err) + } + return strings.Split(string(raw), "\n") +} + +type bank struct { + batteries []uint8 +} + +func parseLine(line string) (bank, error) { + b := bank{ + batteries: make([]uint8, len(line)), + } + for i, c := range line { + var battery uint8 + switch c { + case '1': + battery = 1 + case '2': + battery = 2 + case '3': + battery = 3 + case '4': + battery = 4 + case '5': + battery = 5 + case '6': + battery = 6 + case '7': + battery = 7 + case '8': + battery = 8 + case '9': + battery = 9 + default: + return b, fmt.Errorf("Invalid battery") + } + b.batteries[i] = battery + } + return b, nil +} + +func (b bank) findLargestJoltage() uint8 { + if len(b.batteries) < 2 { + return 0 + } + var first, second uint8 + for _, battery := range b.batteries { + if first == 0 { + first = battery + continue + } + if second == 0 { + second = battery + continue + } + if second > first { + first = second + second = battery + continue + } + if battery > second { + second = battery + continue + } + } + return first*10 + second +} + +// Functionally, append the candidate to the batteries, then +// remove the first battery preceding an increase in values. +func addBatteryToMax(batteries []uint8, candidate uint8) { + n := uint8(len(batteries)) + if n == 0 { + return + } + last := batteries[0] + for i := uint8(1); i < n; i++ { + current := batteries[i] + if current > last { + // i-1 = current + // i:n-2 <- i+1:n-1 + // n-1 = candidate + batteries[i-1] = current + for j := i; j <= n-2; j++ { + batteries[j] = batteries[j+1] + } + batteries[n-1] = candidate + return + } + last = current + } + if candidate > batteries[n-1] { + batteries[n-1] = candidate + } +} + +func (b bank) findMaximumJoltage(n uint8) uint64 { + if len(b.batteries) < int(n) || n == 0 { + return 0 + } + max := make([]uint8, n) + + for i := range n { + max[i] = b.batteries[i] + } + for i := n; i < uint8(len(b.batteries)); i++ { + addBatteryToMax(max, b.batteries[i]) + } + + joltage := uint64(0) + for _, battery := range max { + joltage = joltage*10 + uint64(battery) + } + return joltage +} + +func main() { + filename := "input0.txt" + if len(os.Args) > 1 { + filename = os.Args[1] + } + lines := readLines(filename) + var total uint + var max uint64 + for _, line := range lines { + bank, err := parseLine(line) + if err != nil { + panic(err) + } + total += uint(bank.findLargestJoltage()) + max += bank.findMaximumJoltage(12) + } + fmt.Printf("%v\n", total) + fmt.Printf("%v\n", max) +}