day03
This commit is contained in:
14
Cargo.lock
generated
14
Cargo.lock
generated
@@ -8,6 +8,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"day01",
|
||||
"day02",
|
||||
"day03",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -17,3 +18,16 @@ version = "0.1.0"
|
||||
[[package]]
|
||||
name = "day02"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day03"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"iter-first-max",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iter-first-max"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "42ace18d32276b4ce7d8261c06b376398a4ea82e4715a5b37e46fddbff41a617"
|
||||
|
||||
@@ -6,4 +6,4 @@ edition = "2024"
|
||||
[dependencies]
|
||||
day01 = {path = "src/day01"}
|
||||
day02 = {path = "src/day02"}
|
||||
|
||||
day03 = {path = "src/day03"}
|
||||
|
||||
7
src/day03/Cargo.lock
generated
Normal file
7
src/day03/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "day03"
|
||||
version = "0.1.0"
|
||||
7
src/day03/Cargo.toml
Normal file
7
src/day03/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day03"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
iter-first-max = "0.1.2"
|
||||
200
src/day03/input03.txt
Normal file
200
src/day03/input03.txt
Normal file
@@ -0,0 +1,200 @@
|
||||
4641144244413454444424423342544444342452422334233433444444444433441244134444334334433442445541322435
|
||||
5342626167342261132346432442524535654423854254336833422424525424435362223322423132824617223244222225
|
||||
3253334325533334343444454444442443343414632433443453434442224354345345354343441443342335732424432245
|
||||
3125446612343355332433423331342424233221222225351166255322343613238264323364224521414453421341322224
|
||||
2121433222266221212226223214126322122225222122223232323222232233312272221123232322722362212224321232
|
||||
3121322293422231222244623232221222322121212124223222342233242161322222432232312232333313222241321233
|
||||
4343754227224325253462446556234242222265733234423352446126333232224344222242521432311226462243124235
|
||||
4233945333453333467353542374532527553254345354954353465352433265644455345335344463341137496652164734
|
||||
5453756862345874845337376263545357351615334758525567326257773648575775767585536364536457521766556466
|
||||
2222222212232112223311223212222332222242322223221222322113223212222232211222222231322222222222212232
|
||||
4537238344323323433433332147415344474344425443428332334224283953345235535334544444343254334544255333
|
||||
2143543142143143543244334242444244354323333433444343644323444214243834442444343543343424434254224333
|
||||
2123422324333221224222222322222212322323431212212412323224713225222221321222412132643142333221212222
|
||||
4373537335231321222338473333545357342341653434553573323656422465333333465536523532222237343573233563
|
||||
6123352252421943254535246241522221345233472743222433674123453212332532244265274234273422332224263312
|
||||
2535233336222312532242332325224732133222333223243413323239331232214333332351552313324282322325332362
|
||||
3236333333311222323153263343432221225242232323212223323323233322331133332343312224132225422332323332
|
||||
2212222322142221222224122332221223123122122322122222242224412532621212122222221212312223114263222222
|
||||
2323151222322222233332132223321222222252322222222123312222122212232223124421133223224212342122313212
|
||||
2825122213232222222222222123122222223123223521225122323163322323232132422213311212222243321422222223
|
||||
2445743737443564743743534553443454433955333356653437463535544454347526174237772454455234424374525245
|
||||
4242441564243344454434342444351544332335424834464524331444422343463532444442343344333343143234525233
|
||||
3366353344253533234354545453335544433213366333586364433633333632333456642555234465343336336433652636
|
||||
1122324322322222223232222221112112212223113662422232221222312223222232132241262232321222262222121423
|
||||
1112221214421141412222222421213221431223421242244324222232252311132323432222213212222222214249248251
|
||||
2221122223322222222332211222115322111222122422242121223322222232222222222221212145233123222232321322
|
||||
4323221222232233232123222222222243125522232122322623153221235222381233122224322242213814242212231242
|
||||
5122222222222125224123222214522222221212212222222214326226221233225212221222542112222652222232221422
|
||||
2532223349524332222462322324333353433132344233332214213441433223633433231228233253442323231635354542
|
||||
4224434153715233541233452122414221122442223411522251125334261222111131124243114432222322221322512253
|
||||
2443543334534345334434264442543344333444336564343422345442452934543544555554534345545354323244245333
|
||||
2363333526325434537123443333333353236423354432835385614333623637382263533333525364554339333435323334
|
||||
2439645733422423655745832433374383282342333232123812373236212443513241234422753353338952223333362553
|
||||
2223522222126221322233322122122232222232123222222313322212332311222211122233222122222222222233222321
|
||||
2432662444464224354444354342243433342434454445322441445424444543444344454244433354232354324437336535
|
||||
2422512512422222222122645212211422222221211221222222212321221222122222221212211222212321324222222323
|
||||
2462312222312329247147222222223321241147212251341542222335435222233262245251342312672234224222222232
|
||||
2123222362232332322112112272313223513221222523221123222212333273371122242232313223121333322471263223
|
||||
5122223526243212232232212224313253532252332332323537321341232172413333222722322532233222122233163734
|
||||
2355476445226532225533545756354354585446246555792493655554468446644665655642444275768444446787554565
|
||||
7373449743336443522643362348744297866766442348366662335742564834664546532533496534438434643923554325
|
||||
4355523434433513333524443645535353554334234342544445642353343435436355345444352543425453545523544344
|
||||
3342344434324334323423333336233433454433433332433143436451544123334534234333333426344833233433233332
|
||||
3564334544241312545445333623535263624124662354834262553443435453221534347442771465246232454554115533
|
||||
2211222222222222222121742222222142222121212222112222222422351128222222292222321213262221322122142222
|
||||
3222314323131431333226222131222233123322422232225353443233222222434242133222322225222214226322222123
|
||||
1222222226832143222145422222222122222322232222224322223211124424612222322227122222422422322123223223
|
||||
2124325455624362243313233213454442152222312153133251413331122514663232535534122269442321242362454472
|
||||
2162222312322222524422492322234366242322343244432422274224242224633122222222233224222222232222223431
|
||||
3322435534358344423545443434544344434243443424444434462123424444383243284445344322331345424434332343
|
||||
2122228321121522238221232222152232127322224132222221262222112212122222215223723126222222221222223125
|
||||
4213225225672442372625624535222555466336731555433664244633543565452245466232251354655233252455423542
|
||||
3342121526554224232225222223232512225552422442222435124212862212582222112222744824226253522272252249
|
||||
4411443574742543934434413433443557552445243453424324236846542983345544444355364344552443525225324335
|
||||
5293421327333637256382343721348937383482346333643333744233541123328534433266456335423433373233543335
|
||||
3272642252282329772421221553225368223218231743222471242546212223339612421221139241445526244535222822
|
||||
4213322644541255537649246247534943258424423474657241244243144335334946464462352986432257425323524443
|
||||
2528423242272255222323434762334222322441214453234215622133122274463952155334633224346311532444534112
|
||||
1232223243422212122113422212125222222212341312241132222132122222212222222234224232413422312232222133
|
||||
2442266322845246444683524642643633622616637465936243464634323346454458425645366324355853586768313486
|
||||
3313322232335142323313222343234324325332322321232342223214326945352415214313342442313162134544143523
|
||||
6265531262221424122222122224212562243225222626222212321122132125521227222412222511221122122652214252
|
||||
4362346514214223642415448444254524317322252323224244255254221223152244215251122222322522222625522412
|
||||
4424316241216414215356512133566264653553156563455656316311365445646266445512626661141125531434146789
|
||||
2882858546334499442384231783584995332246312589824731334368492741673426328355383233572933284351393533
|
||||
2211222213222226423642223222243321322412632223123232224114223229222232232313336252353534222232526533
|
||||
2232222124422124241232221551222252222222232212242512422321432222512228211226212325462546523822234222
|
||||
4423343343333513152332222235523732133615232432241522324235243622237384172615235232921177522522334424
|
||||
4433333244546235543453343355373424234444465134345443554411233433444346732342434243435545343455633552
|
||||
4353355235643466564243644544566546556344434654333444443654664444564354572554444463444542565466547456
|
||||
2222222227322212222123612222223123221221222522122223222214522123233221222222221221222222222221212122
|
||||
7222472459425143232164222221214424412242849444232273413453325342742362242121223522433242322333224222
|
||||
5332442223335541332353114141462423532313132435124212223355225422235515134222553652244534232324735562
|
||||
1422555384252412242463254554444436324455372714424434452454495543544559964453445356536554544779133455
|
||||
3444293454448444843454543344434346445444433834343426544344364543454544444443444464544444331544344455
|
||||
2325463233333333316244452243536636336443412221333222354122243658523132338423122443156712222433431343
|
||||
4222214222252112227223222322342282242321214342411121352532242112242254322122112422221221421232222412
|
||||
3556834522355553454438655387254556364344354442965955459465655459755552962934935244644675585723953454
|
||||
3393333364479397686613534436359341789353233643336156315943351152935642362338342552924332335213574485
|
||||
3636373533342433563234333333345233362343341332343363364312723243336321335534644673343335323533632643
|
||||
3133323313323242232223222324823122131434232221323222122133322221243253232134122323331313333323323233
|
||||
5447554522425252342233347255542655433422523444652453234554314125642222445233326337845443354354456442
|
||||
3831453237543434321333433444335336432323783433333333434431453336324444334321434433434432433233333443
|
||||
6736554186426644253377375534745356563565716462657553565517768567535546767744566652767553256743354764
|
||||
1221321222122312211221223322222222112224122323211222222123222112223222222232323223221312222222223232
|
||||
3444833334244643361444433414544454242444173276433323633444443323374464733544349653641314363263534321
|
||||
4452372323353432333322343333523223332323232232323232543431433354333432334323516522533133532273323434
|
||||
8558697778663439873617598567663778276867763424687666484632554868767636544866684755549344165667673565
|
||||
4363632321153422343243222542135234322522365566255322526233215165663114362232532553236224629422415332
|
||||
3364354223224736522526666423153666385335645623423755278236365266432365445363864623424762322721465243
|
||||
2334341215237332333333335254738263452153322233333233424333242422352423524215374322333417343323433321
|
||||
2232422333382312443223221112312123122423126233433234322123524267252222522232242382262332523322373452
|
||||
3133334422343132223332432333323223223332223322723343523256443342433333223343333343333353232433531443
|
||||
5121324523832277111232258213731673422843242212823251332464757246537422243322224721122442427283613522
|
||||
6311333342633582533333333283243323333277333331353333632413338354573524353532372332533332422533323232
|
||||
2837527223537266322783424363764747147226145457525763437625253385226732671272128646272716726637143763
|
||||
3333232332322323233333353333233333233333333233232233333541633331333533322333633352333223233333213333
|
||||
5545238824365423231236545142566244466647343624623334474358652326649445442312222921649765133253525693
|
||||
2332421352221241224122444412262232123122431424321232142328245422222522442311323161217223411412544438
|
||||
5524636253656234267335431337343446446465264545543485347352324674445439732643661455451546435624228544
|
||||
5344335233433232343223323333334372234532512325233332233323323323333332223323422322213433244231221422
|
||||
8253534624242186724231322134324225642853263321127274167744333242332323322445442223622233443261222222
|
||||
2222225211222222722222222222221712211212222123422232222142222221222112223221221212222163222131122212
|
||||
3234452243432414252332524344434422442442222443234432432243351445543255333211343242413324552323442313
|
||||
4363353475487842942643262378643649353334476655248673934325788173238437836383832566854233833357431159
|
||||
3333363333333443315323333332332333333323424222133333333533333363334334243333423333333533233333333313
|
||||
1222122422322122226122411222122224212612321233524223222112422221232212322242214222228222222422121222
|
||||
2122322232241328316222312223222122321222223223223222393223222222216312322242322223722212232222232232
|
||||
3236445364463433335383366344322232233445423532434224333533453453335363838335332326226343233246234333
|
||||
5743322653372334763237424542237545262332657577343314227364722526242634373358122427425236314246576726
|
||||
2232252425152534124242244426622622214634622245412624613231623326365522225226224422364213241231442526
|
||||
2333225132223533641323243132333124333223734534322144343124232322433354222451413332244232323433312324
|
||||
4212422222212322325451322332122522215234112323422155241224542214382253522212441224322222423241432313
|
||||
8224263242213362233443152453322222222231334221223141224412232212262241412423233111422253141222244432
|
||||
2444142423533635562333274453344732422243132234261922284244632224252241527213222225333726222423443253
|
||||
2332313431223322333343223322333333222332231332113233333221321232223322322326622333232232224322222232
|
||||
2325221242223443334153422225224122442244241234542242342324322322423342223353221521234322412451522133
|
||||
2242622233222242242235231324324245232424432225233222232245123424311123132223213231324341252235212222
|
||||
3125222234343434422344433733413234333322333652214335321335333213333334372232243313333433322332636131
|
||||
4724242132173428332345773234312628223312224375222333212222324323113122324233322121322522331112543232
|
||||
4513343222231233222334335332332421243344242223334544312353333133342233335534323233432322323145323323
|
||||
9323323563272443336734722433366543333343335332725533373322322433263323333343313235327353644723338253
|
||||
2323452462258322354212432321532222522521222224563122223242512355342322233212245243345422245566224114
|
||||
9233233623333593333223243381332353413733554365733623833523334392334723643335353362836333633733833123
|
||||
3244122353454233333323424322322327442421253552312113324443334322534323312322251253251422222365322443
|
||||
3334333334633333333643433433333543233233322433335223237532432343337232223333333332323813433333332323
|
||||
3553455657646654697475355552533548365855538558345464566577863595499296456852785458333475523233555845
|
||||
2822221522122241222233232222222122221222222223221541222111123235222221612222241212221236221122512145
|
||||
5242353914324342322464445345855321334534332674533534443633334696533236555213534953333522322335332343
|
||||
2365325434246335363235331228463732643222643364635333232367334533333745233316438347233455333343239238
|
||||
6437232453221233322331326231432223323332333432232332323333332332322244433313233262133343233312332334
|
||||
6221212242232233212222224232112212211262411632512231222271224222123331233222223222225232222212122312
|
||||
2232233234222112222222222221212223311212221111523222924212221212222121212221422222213222121223224233
|
||||
6655755543524646845626957366662755334544946516552485374645342947357589841324245535558462624843224337
|
||||
1212323322221222122232221212224231322222322211212212212222222222221212122221222122215221322221222222
|
||||
1422244312522222532213211222221124122222222222326862522522354121212232215221224324231223223322252223
|
||||
5553313335545573235555536556554534725225534573474345335354349473754454433755334332373443333634568143
|
||||
2724443163242552233263712414252222442253722655227357362325261236323211855153313162144673135232221622
|
||||
2211222222222222211222313222223422222243321121223222212122232222211212222231222222212222122211222222
|
||||
1524445222545824253552234224162222243323144243446526711242411346265421535424234333625223652652211422
|
||||
2512574375232132252577321125315232833232439436233643533239352233222235422223423215227226433222412315
|
||||
3521343454242334454235244136422332441454233424143232243233243233132434333243234433434313424435333342
|
||||
4241443734444363444324344344353244443344474333441545553433343464423254424444353444444454444154914462
|
||||
2422522313236422152134532223213123423432312222311224322211232333251321324242771722243272222332426224
|
||||
6535225123525455442224414223622334413212232363222328664453332333623552815253343152244224225343542354
|
||||
4154352542334352546154344441364555524652734456334152353441435553334444424171655524455381372432442144
|
||||
5463433333324423435425554243353442235422445432652535344345423446234232312241153345343521444232262553
|
||||
5874536553556462456925656542446535455539843548256553366535534545563752564543356246536544653457654373
|
||||
5515243429442364454455253434543544243124141443434442434233654432344245444473444363334472342234223435
|
||||
2552663486323751657384665359573246422645445554673552555548565576465445979625576264426427636554454533
|
||||
4624643622223234234542222542753452333512524623657153225633225526622322724262214454527749826333242434
|
||||
4244555433255443442332234245442326322464243232355352252142225435325434535472352323266324533325525232
|
||||
2924236434273323225252393221121323222649432242434723242332323344333222444433442222822242276321533322
|
||||
2432154525334152255354534335633322327325453363333628225315345454322243432223233254324335324483132265
|
||||
1232332222232322235533312531123453252121311252212121222232455323452525834225223222325323214562322112
|
||||
3212344292433232112233312242232322223236316622513342442323245331437322133232222322343124332271521222
|
||||
2212312232333322432232212322223323233332312353332242333211222322323352213132231233233333531222231241
|
||||
7566938564535544427676353796586244266646551126495968641558263448243654664896951296268326223553633456
|
||||
4643152253655523374454334445324434444544445245773425544333525634554233349429454443444344429554443454
|
||||
2234223224452322214322523247361115226233434642132455521459463223442126144429421222221112422221245221
|
||||
2342221144322432443322432223124444513232342224223234523323434353422323432432432242344343331413233432
|
||||
5144325127224334344523322213233322353225252163225442352332424232341255321424333514443242242435241632
|
||||
1222232132123223232222223321322212133112133211212222312223421222222222222242212236223222312231333312
|
||||
6222122224213224621222222222422722222722114221232321222251271222623252622162222232222122122212226222
|
||||
2343124121222241462122445342232424234516342123242421332527536371414735222641114325222544222572237143
|
||||
3132223432133532113222224532424281332228231222433234392214324732233212322321222234323213131533522371
|
||||
2222482168223322122232221222224512232722222713231342225134115266142222213224352445241212252123324222
|
||||
4653343672652345652446445444463655356425653644452344475263656322635542545325436642535434567747425165
|
||||
1512131353255454452253313245145125321212225554355335543551152211343321241324214251443153122523336789
|
||||
4542233444344444325243544533543533536221553243311424142353342322345344145243245352241454523323344332
|
||||
4233222523323332323421234722133524422254123422324332353432632232233433222433445444322521257323122224
|
||||
2243222522422243331424132223244221252122452232212244154422221446542322322327242422222221562125545672
|
||||
5552533323128334733354733251353353496342333335324232227343547463277316173333343743334641538235133336
|
||||
5241445544532472451322543452326434244421274243442553354323234433243423364412423344343243143733454533
|
||||
2653264644452325356755223123482453453434332254311241524433328456237244335444122444236254353465444244
|
||||
4324542342324238115263422332323232424224424121424241282144223242222232222244243212242222143314453443
|
||||
1111211521221622222222222212224222325252264241222214422252622232612323223212123421244231222232212122
|
||||
2152442222221213114153512224748422227642237241532243233642457173321224215244774621415223223327312243
|
||||
8338575527445534554453353555434184556155535444551544335726435555544544654555555577363534351435557544
|
||||
3233624233394342443442473453433432224423447246334442319132143222624224369424225342143437122435312544
|
||||
3232323322362233447532222343322322333112123122223223352222212321231223232223333322313332323123332243
|
||||
1214483763244715345767214456231422126272334247275584353585561686273137148613173382768256346831175529
|
||||
4331113332444344343123231314442132411211231223114332221342321441242242311321313224441221413142356789
|
||||
7874687656557157565344687794645787673747327685656787578856467697558465245457654675636758788555585845
|
||||
6121641222246282475232225223223221462237626227264272372132222622221556233321223225332221425122232622
|
||||
3563133423222233223333254324332321222713233232333233348323238323233313332552234225233222233323233323
|
||||
3641124522716243444254122252725312942339454212551129693226417322514222472543243224224253323336239153
|
||||
5453553334526442335464656553566356345522534463556654344553643524666264565332456542213554595468352624
|
||||
2324422233223252254624112233432222222222232423441224244221222442433222234232212532413223323222325213
|
||||
5144231222332223222223221222251321423123122322126426232326322132644134323312312211323521732232323142
|
||||
4542242222223222245242341322245223243222245212212242441223122341272542232122513434222242223411323142
|
||||
3424626122767313615767155612327526555664333122213711236537637743351166314144615142642535612364143289
|
||||
5533233333252463243535424444343336344534463465323552535323234432365123334253343536532544333434263433
|
||||
1233234323233322223333332332222232332332233227658351322323323323324222132321331331334232332321322322
|
||||
1322222231242212221132122321212122223211432213212221212222322222322242134122142233222222222212112113
|
||||
1423227252272621526413321222372262716572245627222222722214322422296247222255222727152334227522435572
|
||||
4327443522448244242543423343442232454443444634341446443444444346542343543833444445134545447763234442
|
||||
4323333553233332233243222332353252323223222213341412222313322255533333233323432311253333421353333332
|
||||
7433384734436364342237476254418424144144346647924444342446548344343224336344333444345733475294534354
|
||||
3222512323622425223322562322134722122131422232112272412232462322223422252215126343354353937233529334
|
||||
2
src/day03/src/lib.rs
Normal file
2
src/day03/src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod part03a;
|
||||
pub mod part03b;
|
||||
23
src/day03/src/part03a.rs
Normal file
23
src/day03/src/part03a.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use iter_first_max::IterFirstMaxExt;
|
||||
|
||||
pub fn solve() -> u64 {
|
||||
let mut solution: u64 = 0;
|
||||
|
||||
let mut file: File = File::open("src/day03/input03.txt").unwrap();
|
||||
let mut file_content: String = String::new();
|
||||
file.read_to_string(&mut file_content).expect("Can't read file");
|
||||
|
||||
file_content.lines().for_each(|battery_bank| {
|
||||
let batteries = battery_bank.chars().map(|c| c as u8 - 0x30).collect::<Vec<u8>>();
|
||||
let (i, biggest_battery) = batteries.iter().enumerate().first_max_by_key(|(_, v)| *v).unwrap();
|
||||
solution += if i < battery_bank.len()-1 {
|
||||
10 * biggest_battery + batteries[i+1..].iter().max().unwrap()
|
||||
} else {
|
||||
10 * batteries[..i].iter().max().unwrap() + biggest_battery
|
||||
} as u64;
|
||||
});
|
||||
|
||||
solution
|
||||
}
|
||||
52
src/day03/src/part03b.rs
Normal file
52
src/day03/src/part03b.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use iter_first_max::IterFirstMaxExt;
|
||||
|
||||
fn get_n_max(vec: &Vec<u8>, n: usize) -> Vec<u8> {
|
||||
let mut n = n;
|
||||
let mut max: Vec<u8> = Vec::new();
|
||||
if n == 0 {
|
||||
return max;
|
||||
}
|
||||
|
||||
if let Some((i, biggest)) = vec.iter().enumerate().first_max_by_key(|(_, v)| *v) {
|
||||
n -= 1;
|
||||
max.push(*biggest);
|
||||
if i > vec.len() - 1 - n {
|
||||
n -= vec.len() - i - 1;
|
||||
max.append(&mut vec[i+1..].to_vec());
|
||||
}
|
||||
else {
|
||||
let mut found = get_n_max(&vec[i+1..].to_vec(), n);
|
||||
n -= found.len();
|
||||
max.append(&mut found);
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
return max;
|
||||
}
|
||||
|
||||
let rhs = max;
|
||||
max = get_n_max(&vec[..i].to_vec(), n);
|
||||
max.append(&mut rhs.to_vec());
|
||||
};
|
||||
|
||||
max
|
||||
}
|
||||
|
||||
pub fn solve() -> u64 {
|
||||
let mut solution: u64 = 0;
|
||||
|
||||
let mut file: File = File::open("src/day03/input03.txt").unwrap();
|
||||
let mut file_content: String = String::new();
|
||||
file.read_to_string(&mut file_content).expect("Can't read file");
|
||||
|
||||
file_content.lines().for_each(|battery_bank| {
|
||||
let batteries = battery_bank.chars().map(|c| c as u8 - 0x30).collect::<Vec<u8>>();
|
||||
get_n_max(&batteries, 12).iter().enumerate().for_each(|(i, battery)| {
|
||||
solution += *battery as u64 * 10u64.pow(11 - i as u32);
|
||||
});
|
||||
});
|
||||
|
||||
solution
|
||||
}
|
||||
29
src/day03/story03a.txt
Normal file
29
src/day03/story03a.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
--- 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:
|
||||
|
||||
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?
|
||||
25
src/day03/story03b.txt
Normal file
25
src/day03/story03b.txt
Normal file
@@ -0,0 +1,25 @@
|
||||
--- 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?
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::time::Instant;
|
||||
use day01::{part01a, part01b};
|
||||
use day02::{part02a, part02b};
|
||||
use day03::{part03a, part03b};
|
||||
|
||||
fn main() {
|
||||
let exercises: Vec<(&str, fn () -> u64)> = vec![
|
||||
@@ -8,6 +9,8 @@ fn main() {
|
||||
("day01:B", part01b::solve),
|
||||
("day02:A", part02a::solve),
|
||||
("day02:B", part02b::solve),
|
||||
("day03:A", part03a::solve),
|
||||
("day03:B", part03b::solve),
|
||||
];
|
||||
|
||||
let start: Instant = Instant::now();
|
||||
|
||||
Reference in New Issue
Block a user