diff --git a/input/09-0.in b/input/09-0.in new file mode 100644 index 0000000..f96c390 --- /dev/null +++ b/input/09-0.in @@ -0,0 +1 @@ +2333133121414131402 diff --git a/input/09-1.in b/input/09-1.in new file mode 100644 index 0000000..b02938e --- /dev/null +++ b/input/09-1.in @@ -0,0 +1 @@ +7487702899805356917533568464676388139646375428672963166179195068598151281371257194504585841093793839395650845610926648109572258492939595755257365373933070389630472653689013126017949327894990489298737653365715988979332512434078769667348823685912244894781737313914836664516065957138517559326581342054743671758588525077162581407673185846537298567731587623466662668965331219942232876126805776154479929067858195988298884117512270609234796215665110161424476976183155141571999928317984954233887457148964861186281331717586741864481230449415833625714626688790936048967056974054589229138545803524229471466266303321366287418561753869502117502678323954511972425333402051483337774423361615142150681768642267112953116658289815337266108268411554598342353829582929906997675110243473552994152327761757676279858849242414906545744744433077668966512437127796441511879714854071191696929282137767143077225054711766526549731841957157955277581680295266484444434989745534697475606219831414202668687441584682362876172355155019406921971655611976636425894845612966591157979243703546766187623262605272805952339645994728177993677570752860838371372333298426968738125442497655364688177626747759377580669219537619994545379466289179499023885046848027686923641366929861705576504316693747604428915124649618731316129963415883392577325019499126909275766122615915509376578127792656575922232370408865683974245599602254365257748061462239542985218741249130851526762971691514334887201255488862977146768387214695389750593027138647234190213872206772564713348472361890929071567679279533622644473986395154908863808944828643249146945178943482706044102656475891541020402265315372593162762311162235978615734798731493493435506166533322837180867428296976777448481097825383606281707631729574257286225754259220292731364378362835282273741670226677702942683286393467858017544741874258261387519579652153133263288979233592281242507633483545221132161883135755221862451231636746828261326675621064486029745820462390542356457144243884843052273826361685386760713616606033683288851956193723656263925314983799831249105350537718865386793931464971654121393250386441397723528862899445465122352138156574265454419211697891624268108071595545824385397040634052453530712083708328304475884853817412258751536042954048393193254674344248263447951981647577921527672465628482728554857173481367559146204945374125602727428261844517593065429235677544133211111389931232226566831244275597229957166399996144944874836178609088196191838295409151345737151164959448351214184664227447496558901838165539642569236697896937164337118016122933216119761230169018112549138055728494326048334429602230958957355866577196851815207893409595174878759717415190436787433159964318782791845185346414364052251777774283835650916380309176806317552111505232518748654244917442988012791114201492225664788328782739431645331420308440599353391472384720106752292519488663211131681586943693281652997275398435387480519873893042735069804555648121536279615648726295479596957757246546867210948274258183928821303346826965279546548042859538715346474642142135323327479269554666363070333114513563659762213433712429113498721123804084829288105578148888623531878090438295824444975457335683167971507168426317139596237841384395618361402165459435453988691024821537267574277523386322996175106268394673891724883269878919804347662711737457122732591675645478182612518069964229303715633962917135432548148661846977429149954454267841221094149649669554967857494279696258799894532761609818963157807033154247891227345474388763629187405128765149805960521399671658858798716893347527188273918133794998846356254131486473526928838744352231415749373220675965872637938521826274176343738877618568447974483849885350738033681555493364432115372532958530519752679999156177411653423330704898683720766973801237939779693411364660935958882998378425431283113637587799268020108433914487999010447312436528159257685446981541413855791621574887616538718059772099983182197732481539425134997350609376701910854313531887918486202655929618693886255169402491504249759962543552903731648359141969492521324552377021733880969981437889507395967935957318457725996147219935224528972982878452441245894142145071561252594627404437164858807296379081327876728889685864134677216184158649972432467960653359762434241981339232405628495980572245753530835915512657618439353427786852436224646786961499509532974880667448287133833868609092326912769222459423337870884214919550107261867819996983494411237964273678279113823660931458319528214417765588925865544910627626931470847553465637599225259024368449627485315929531972462169238932396386583950997526193267102978664610705752767860668744943611579469934999576866368758656199983563306614128277364216287623231438199624326191151598314680699712125721175791286354827823903175916634707640401624219640886077822138657763642085634791448377288877164256294496512378662176324193988721705948394738944062298120371151978826967529374294666564923987636981765160155965755119884269407561822554468214409786373096896942613038614493386816714863158748547936273736109220998638703419524919894465544854666493492682316539726862446145666723286378775580704784413241168113691340796937997830106024413562161358635074786074909528929068171638881257398736849659464693378420855063476792776418759466209376739892476115831577554455756423412771702514895747526541722845564492639355917884943157118981534661699193594311143161815666427344457338418364714455603433186442595193805333149328582168876085357771847486273426841441129997991768166410964124234235144658474575464867945672274517649438931727938726371244503426276192812584946439537844405410955083177617253747329849853330408056854159361161278920692869727527695826571875528723692572491069233614244949366914197423672382469165785448292220628723233319909021303526508715177437473972498148727137459519279643988897384531594954491582408173805278268365964065264886519565327920895795605476374139432620177494212860282761854667212991782664881918996632379294433769882173133340717373948423403999567684587070756753491146697085182628817122315994863758381458945970618296544625392433382416664913511854864276655644899021805967926988848378504023868410465757196736947513204483312298512858328698665713592647675912128174748115351022875895546541542193479427996839608779896346403156429072466378682168546138121518455823873724944846775112624153367461827463136311262797847982316872804838351840127281436652917214185232828032651278282936445962712881889142646444649714703974364221931131966920969152343286907629826877897166437198624178598461414764664924941375766577405081145070968361852562419336978534423684345215277015459539627135831375307784643230252490148996507266636886592099286468679915355467579425364050857985956164227377252734976252809226106725262365927770129529989477324434428196842993859288466464226778486072983143475456438038794685918719931714337538868766829110954594388893138548807061566173609516356587826572299576362348741178118293675012975645335870892292812548622813886571829118572716601775445078903831284057485639117075248596202093765234118111311812477852546495994911877710466950609262837532229978179012105361146821513265499666983343549030745083237614401771592046537982823071603259926432271272323432187933654710414269388493632050365649735642883539515122954670857488511275851242365732773871764940542510889796774068491893774264341244376058197766802830283535573822874386136999105525405996908414602538565790936932409580164860895967886570813615603352555489676960601220191534688715994447193682635082907936646525104347714989571072888775957817477595213067773463734638198391111370551721214744346897299220119288999660422227744147502726622713155653335134533684951234938484494970402458932585116772429725344644482665158552398151918868183027888225611042479283674279162344875691884372897684162115704772794646709249189418744832142610575760988298127477815696817685451961415115287222732471699639237996484918477942246010532382159179554649448842921153214127218346761532418582432323723469154920132716853120302851454865213083997771693733934342899580599685927881367897916011947570937266944585765434754721466396833735774155997813706632435272299134646265538417319669681471804286853388938350968616491593536529751347851797566457364546437325823578854879209330723368563593165532815587101422808779712385648570331982906510403239151877467494335460198887182186154023778094204925713748493533789456685748112042965129192493654229981191499783743659153968547226828190525342208327193761409011388936673886748216645456144871218283857167109374485260239849677858119398492666778950595772108221477963754057307413473612853948235051202439834695191285314884501530512234971030387850591043872778944271476688911528773171266237805658501823363056889794246681351233537854416479472840149236278798679035528010636342103294981621101023903327642259345962398373998984361235331470457928447325559878655056707815537589858569311455495215207938763918506897874991116671159479584984151415665495421343782212623956787270444033224764551392192514317118326950484532938521875036344326212563572988992399495732771459951615454528483292797522857181477853403552668172434433819149954310153658174064554933125665177310968980685118703970188574684861221584468248994146478446667971229147938241146955807730329987489137811523474556723686765322439679753686732462513578437942631032122350643838426844146584348755314134249716103745104392594464144385613943942936107394568333408028505794541472404936712178852394137447596668131387351863981920264510698423977786906674551334404755601431984554173377868862423290558524866156469048451541133521408852292560975155994221956451559290788056184642543665247736784325756753127162563772404463559311274346966092161643467582402941543523556827658250301491285344972332534496754578776990709943918291866897813964545979367316261818222430959711254639674918421959483953152122297543564094878719473745331597476852689317572282631022508170588377576359334879458415564222179182976898863810103455164590951642797870574778793696758237774527504642303581699129451017395274428937157275874957934042136354217032159489843278296512883154569961374860108874943454192978376114517069876768369793404110828962205297425850585157233320869023132534744482425637561642436417114110692566951772949938382753541541893783304874606795398430379627109632523046581510483751334082396695776293307745621164895972194757419621883178369567205534661122241678417329841454425556687956352475833162887791169125109551366051728979286092544167427975894777661537975890893145582923445026179867945078203621371917966129388068719614405859394851427068339748599037898454203574504745993512432880439698945546705772834325453326695177187818305171707091661061854228592529771341281473564548114054309654698883579288265947979026587617643176382029182529551635787728247616505878272377145625119810129070381125876838576125316794207893268092115176494139508252932431883415382098105195993329183071399248469632111920157581244332623118819455308557102258111553643696143162981958727915814316654638905976706653105993272065431052891218936888285425232592684680539891595920888060745516657624492821696350256673672360357613696557758256872826207917973119196755595698907731185260397649379435634026122888808932392979715684305244597354974995777710847572163571541452474778722734987867915442144254587299108298375777422972701118363467713717813837443444811352381576375613296210241049909289446983841144515184113819831943538853759971573260351572802747634985673558868671831675939740654562189627745842243737228774403272897834491464925344261537818636726755999767594871619589352165352542659215577161327997699048297119267315682329789054702014855580837397632236164013346123586750205018539918384946414760524926167915531119668131233134857869413770586718679331913827803616882475246249226224981738285185999875808558565035454286479842244428678460406133507372291137209546507378112279909392213094811711129691989884694196659329167522144239904984732871579326225244402160976737334499955048328134514425344472336684607511997464849142883386931949942264903264231392971627769723255570157277962117864120384515161332992784407869374211558339786075524223868351361954164213988186157473675090549741588079275046842760606762436190392991265762489728463027972627966843989054564455869798172898389882293044204592272320842671283276782323416697612931289135218533934079514514104869908021794340736618771597374342746948216365838882943517992682695755843687415051637977672314787552856246168570248952559867533716175198184227752843481729381781216867266859861371485314451050991665586032241489999833128778865953806573956116235676226784437060173068653638504187294860145994612890643054407989309474597992923864215264635596553250711464996094442119782690535263971137711037446832792564415178858281641972979551809015554224476373914754896675553355602694912065576350767382829515377971878827689591881926223467257135492669936096154068118688897023291898242037924015194111214643626560501152278517788310704962743074407486684167461990955234323236428586889694313040908048256267837147436491478357947884361991977668552821713430144210413458543274782730908874132149166589952279927526432553274412426745951364947044837095953325307896892279335671507450713331687076889321393656728491253141656643827327684345638757393741615870877484793973979966599995581890214516379082869053242382852060547312504844661161579971583174558872264348993789995922329969855851521542822185792094277433113383337446575171114991471863928837787470132715591798237732375435793159449895484540481031214554473362553767211087473599418635688666904928288964911924509224672821467999463624379376242174754698263563263413838250382481693137918416211986858421692253816477163996499037413470334122529546509737863581481624831260533430988953987858192285644518908993356143505487809622439928816954747477542171927491279131952867127094324148994194781379669919761837364690187770142961496477608490634143586291486879986516634383661616276140769327197779391364484013974278789622739221329844601373858516558321145999184891471770519163304812343836317044665742971322404466954323183167124890856685335889243118368961599915867497192710835089239884203065819241604649226610476231124098966931769336568538623434889644262599163580152789187282946585607419973779851254439822334074794795142092934669408621573526192162799387742736507038258357525750641941152725613121826517659453854218219895276322268495313711519029594023743574365167296390391130493129779977536541822163347880157753548786999817503259959532442537527950859478127340769869661310136019562199321240197914938193534373874188499731413139514423367040202426426422672429908217424052862377982923831416323811897592251913501965782343407555418881681572376146588215572779438417198036638714744288746495234191868773568414875235709816578295817852122036816285917940742776476677803824663697486350772774524280469459251365435949845765213439973894982419731755309278552124498038949952763930916565126030682849161321234132118137789210731719691292292583631335338552821269932222733722467559746744509722552581928392911115282149204123145130851932763454452075281379579090839678509261358826195474283863591726773718291988596777906632375339735558516578245576632994329896383676618860397086751489488011535797422616902344233919468986344615868489654195629647797647577784953254773091434152933564635765635755153584799596802853484186274210491117568276191863725536654185725746762738569547551282526683495254584114498785874144222482855616453146883425492641498227806959856246988732432136844894804992873038412115151996208533678077519450999826157321263880748419763047936576559581199012659517443944722384491437272998416999872855567083677611513889136441489999414835772340574447954879896272549992582243515419241299895256399431995842932787141449956380592596123215536515416030226317607360964868709930813060857552489142283350875327379448565854853061636967149981626130456965453151967065988451352348525731959848408182389996546913976495543459939054285886658831682843618932535814171114438197883653728853222130594564864744441038783868852826536551612015804964522593586360619353693615775723907386158717374578212092554580483649655928618327531221239955674711332855809757675543703674315775253562681617452676219462243011908861663512959839708970396169625394472214936984757471702192348335417664434746618730335514233745628394974495136680446468107034622228195159961518273837874852378877851376442988346677162343838667483833615236251720706976629891616498984010926253887699306945977312288173639176179326936714127977238087111915404221252515824153157847751793884258377184741089973092498270194534976530281768487394978816153036825083946734643116527419296857156189528177828760859629847596364380798025503837407662709346216199707577139285866226307887279058989814118466119788783334155742308672204498196257723855493249662359851095196577766964971185478222182382528536276610362870729295331773676760121415461349915838609731774789654947218743436285845286392247348377689333885752655932528559555914164583455552184795518487891513588517811023178256499442817634953794103632166493818196882328842889138964234822674573324191725438366284759810667734471129393019581871796167549475781697262571369733926025171577344756398676704098876949616127895947977851913488927985712183827082246426107524474338111465395071824836572650411478924073505117958847386126691779762339233678926636111753955144133597231367787655572818564080672577369680967829592726754919936079883139909326459288929967387589219277603576339364449516712219102056389355594183335641303395622251393343984085969467639233879729924393603272497215396288878465408847437549434964246967197988911832842551361840696864285446241534789147969628672515211183689062211962388540862123764243362018895318244474349264228462398430579761464481231110257476978928924725588623949917932771327359307276552182973577417276912356352199486228703539192392941165553362367282843282168225466477728261818439314898277343372787829610937334532865357125856019997629525875992712821925353272483718646857237164516961615845706036601996426587799139751865992383646080915297633677105217553772527389156314382559588226509349572814771631325443636038762528757751187514701328447021477112719641205578573496744170378814563836747217489495397460651024729711129139328416117745186972846335502061375596385671875437232082359953549513248746855686588541221041163125423194539625546789892158905432902936838836155280232865554635506929898159425763928956505874386727537616843692161558354328821744427468882970779735681382638711616866819340657274967354567697387163112485852376761256576084636070156290423799666044138983747822882236455212671685611625141013103225555712767557775483874343492718547841731577672337161435105457205266237460208614693525633981872359894382618014204736218557909357439136262672118514519987771175659251362712921846156075877355499738289849371283772531193346809353169779818239712095955069677462625697902861716783369198901533636536818757547811997752276148786749611542322369328246952314488468853081165940794312871168751445586290896688838189797062535242141825506085386017656574909119914391143616586014588564515719779315813789202470517588215991568342437041255249771625158961153361575828407696565648152681305822511440765786633384607364854666603698508084379168877763496576257074123815351167143885323269702990881916245377122117164162415742881732799685633320647019706222188828922487595677212552353050885346865643643675506243806573564551229771202032912364689991322054902364556947709441888729497898412445937895104537109454507073374360725427203183934932609119528594843582473185277537451078775933499743616134729929347316512439189892544941329321397655919499806196371735279058792152568041249887683253544770323856777777778746238585492311429111345167186861883621652029393410224157193579523391708864666936201792855150486091667112196050929615808583567850206774803862619331376692502919506044302828847445568461405124229275913765381797866991605196926170197821799432125560635244394358674928585092813614296489221667333073275866868652971943676872868735769617432660577624798895582186793957175211904120493028133753621485397550408525619157928161249840255684323254322330823696755525725195189749355634977870528 diff --git a/src/bin/09.rs b/src/bin/09.rs new file mode 100644 index 0000000..1ee3fb3 --- /dev/null +++ b/src/bin/09.rs @@ -0,0 +1,91 @@ +use itertools::Itertools; + +fn main() { + let text = std::fs::read_to_string("input/09-1.in").unwrap(); + + let mut map = text + .trim() + .chars() + .map(|c| c.to_digit(10).unwrap() as usize) + .collect::>(); + + dbg!(map.len()); + + let mut sum = 0; + let mut i = 0; + let mut pos = 0; + while i < map.len() { + sum += (0..map[i]).map(|c| (c + pos) * i / 2).sum::(); + + pos += map[i]; + + // println!("{pos} {sum}"); + while i < (map.len() - 1) && map[i + 1] > 0 { + while map[map.len() - 1] == 0 { + map.pop(); + map.pop(); + } + + let l = map.len() - 1; + if i < l { + map[l] -= 1; + + sum += (l / 2) * pos; + // println!("{i} {pos} {sum} {map:?}"); + map[i + 1] -= 1; + } + pos += 1; + // println!("{pos} {sum}"); + } + + i += 2; + } + + println!("{}", sum); + + let mut pos = 0; + let mut map = text + .trim() + .chars() + .map(|c| c.to_digit(10).unwrap() as usize) + .map(|d| { + let r = (d, pos); + pos += d; + r + }) + .enumerate() + .filter_map(|(i, d)| if i % 2 == 0 { Some(d) } else { None }) + .enumerate() + .map(|(i, (d, pos))| (i, d, pos)) + .collect::>(); + + // println!("{map:?}"); + + let mut i = map.len() - 1; + let mut id = i; + while id > 0 { + while i > 0 && map[i].0 != id { + i -= 1; + } + + let size = map[i].1; + if let Some((l, _)) = map[0..i] + .iter() + .zip(map[1..].iter()) + .find_position(|(left, right)| size <= right.2 - (left.1 + left.2)) + { + let (_, len, _) = map.remove(i); + map.insert(l + 1, (id, len, map[l].1 + map[l].2)) + } + + // println!("{map:?}"); + id -= 1; + } + + let t = map + .iter() + .flat_map(|&(id, c, pos)| (0..c).map(move |i| (pos + i) * id)) + .sum::(); + + println!("{t}"); +}