From 7ae660566e2335b127fe5a5be2691a1daa38e6c3 Mon Sep 17 00:00:00 2001 From: mnerv <24420859+mnerv@users.noreply.github.com> Date: Wed, 11 Dec 2024 08:08:46 +0100 Subject: [PATCH] aoc24: add day11 --- dat/24/ex/09.txt | 1 + dat/24/ex/11a.txt | 1 + dat/24/ex/11b.txt | 1 + dat/24/re/09.txt | 1 + dat/24/re/11.txt | 1 + sol/24/CMakeLists.txt | 1 + sol/24/aoc.cpp | 3 +- sol/24/day09.cpp | 240 +++++++++++++++++++++++++++++++++++++++++- sol/24/day10.cpp | 45 ++++---- sol/24/day11.cpp | 141 +++++++++++++++++++++++++ 10 files changed, 413 insertions(+), 22 deletions(-) create mode 100644 dat/24/ex/09.txt create mode 100644 dat/24/ex/11a.txt create mode 100644 dat/24/ex/11b.txt create mode 100644 dat/24/re/09.txt create mode 100644 dat/24/re/11.txt create mode 100644 sol/24/day11.cpp diff --git a/dat/24/ex/09.txt b/dat/24/ex/09.txt new file mode 100644 index 0000000..f96c390 --- /dev/null +++ b/dat/24/ex/09.txt @@ -0,0 +1 @@ +2333133121414131402 diff --git a/dat/24/ex/11a.txt b/dat/24/ex/11a.txt new file mode 100644 index 0000000..9990375 --- /dev/null +++ b/dat/24/ex/11a.txt @@ -0,0 +1 @@ +0 1 10 99 999 diff --git a/dat/24/ex/11b.txt b/dat/24/ex/11b.txt new file mode 100644 index 0000000..9b26c84 --- /dev/null +++ b/dat/24/ex/11b.txt @@ -0,0 +1 @@ +125 17 diff --git a/dat/24/re/09.txt b/dat/24/re/09.txt new file mode 100644 index 0000000..b850d7c --- /dev/null +++ b/dat/24/re/09.txt @@ -0,0 +1 @@ +1711667363201489516692347485192217635365349434435044488931569084485013753845513061771430407252283779331998184220387458285563566476829778442295236696801137714536486845442950189414868095302479991233632775942376657934177170283598917795799667891410104182419392339187517969491035465122756859453436583798834343326025273367348415683239757329184167226474709599591861482283189073133251543441593867508581995716916727337789608384426072199958997716228952229776154791437676856820746922531519209764398145592394346443968414651785515137508862611174927231413639909531477759192962857353847260813261644266442265161116767260365419448954994787588697531475753620916156769185462319773894601222722554809486626517207217284878889697801537542454892189257864329118233563701989127336775621154695851988472663616556118167148956988889342784653390165121316343128016585588371641637637299778288444205359792934541084262539406550841063507134166918616535106816479491712795959694467322527835569083273076436674864445543037743159199340379515952941247459439959809857182730144966414445538691249595361235931672554539497752794822157463742415398094717116154969395076957583902497193159778653598752395619345236782011146878681544781649658160546582597381606175123933757298155728366243985057992535533763583078706820699544238666686524171422586965149535912715721173635537467979489020117485841914425752117090397344247114398647433095546749231084716056985813534423644317207674913346975435116230218892192419239496239217683928561944187018852591458468324653471236438523784425701391342016106442102863734132303158551589317063396191202550996031231178611194259554614812115622907457473657441322112267588579734680596155107438315487104543175310358524618787386597245682169184983443924474545756687088531147207711353771914689445992763885945394162659751533586439682536248578279047186163423526133883824828162655881931302852211658904869717561864679616674259568999418139522477056126870596537523417373555796839919010189971236526461088553911618370964867975433274790972055644377674880629772513836668795567754491115975057932840407447595319172257928682171057235074936416899950336897676579799015429350969457157872224868828169944711987466221990636385467413907016164692587943396332657162914782482734197385148020185176403649255910111956878843641342561719496621911064268252136279939415947959448722656938608070965859324428793669177365799148837357248798815878359370617315177110865716903087106631654679525524738157378974688882681663426845291985428721762895486921378036943139917524476274319265717218422624476996198430458053327740782594166390418828698833538886757412604921646272161171546791593743601860133379398366325261234148408263662658947755106994605212162388972699255929717789311946836924766646393036201986489327175748411020854665976536807947569797724061667393524169458633525285951478541834308241411132529627253190599387567110402610511592981072351453852983971528976956909168502752298773546324292968995758929079499778998636575733839460164310502971578346432919269588825613487390478719138231569383997283783142662212569073515521773678264396277238944430181694966657553223601527825952314477189567683427821986969837465872482477775088116619187636643232606769199843705550456542761560567454952910516931776995984887871328164031946635461093939879664073187333472636965669794282391213976956879259554044607014683515879012599687927843623021681812784275103986223721455417216699734251812658183587401551839571264721817386643951542766782619541071512728392692983981505182398298435684535257349140131815779710649652251748187784809358445020159082119413702533805753153757643548702247335123982214753066177730417292558210546046786528203720881595342132367746282581995426866549663294344863663583373121398199973554237671954286444524154695712516648814834092223428253152728917318281456229298112798067293576152827495922252055129050766968152447668026226151583832644197457299717675241181214538669760471030613283787636609494215710503637784631905361614610242237521549317196196419517179973554253985887763706133158151793845323783401926584246237835739616853444118692731889497072534433244842898572487954279547363773787615577993601211483696739914675043703327156284187255935997886054757864874852584315958786255051568740948038256416297280421710605911654841746478408074184636183712179294555464812827366147415726966580249217709650735666956736603573724346129677835672761948651056686481532135962591816418902980546014299845895663177124981131498162193491163691351178561223763374677799914383744665596766224792847691827813261618479327274547943758229598637665192951397912575115115077397756648110693492718617266159981326438888113362697081242037981285844245448861387631575776294473954064162699959634986971734349517777568665116175975046101173794471684546989136519892523776503792386350265957125572139779674741939512269062685963742790188655281266596358311149666180558890247186577176152164373081214692341952568383265449643916175420448110586962319547407888666870912387787467283288889915441847425675367570758372242160474814259881483384816034945417153723628710147742385324587741587322728522364182218098662586893060514010291632389936126387896919578540908411146544583390392958218060598965114261561329721122441639217394784559702031964559885258788766178142434969341390772270151955965639319790205949506141518868855362195672676742514598993992573158873260437158234099431640801980649236271739146174821827689252985972307430316348576821618725484146399422192719905287865275227294171741276065154849472090846915751519928241162589453165799520822667749811988675377459559031113299595159292329552546883083559918798592697678764433151978147425973082929578109498912286784267874244452269863832866574243448473595311017119682877997252814757535879981661185543865692227625455258628472685673944131089494576403035807451317570111159846739924385291085149578972555234572802920258210809368179739638374101281643481789650938132347981396052968436528896667258769740962788965583829680104591514012684049172481565016963968176235893838987130577738718344693233615487746431869881769578271048951568181037609051195466875194998899795247555977616655283226737992708757684539801418618686692990235240264012503689169015583423989064791968474983172158632391588587507077373859346020417224107549586143736025941158819114676055258827147241323438527814162340402996305421606333759563805654755274968546801439651999536899356895118175806628163311146041261770616119829167229328431850101811605730572018402739961447416542827727195758868229781578344627515723451268446653659451673649823156643623409094309693259898791485256519926679247957535930765311307582291347275395795970985728338422676358376030588524907641945622398262646141289846405227586679721628914798867958832025177782483814554736317393478718478812745922489458809130304375917181917930229123212612218126175296825837612675395287643454665231995336337810745467616434807324764268538746929863208660211348242748299834387665848630365157767277373039837883974155303444315044874873473711281576554689583899553398827417585397123323733877604666619385949271477466491392411959673167449054444415621366623153443163921657821918629264316357676936693452335827919540634293168842509828978287873938133538411815307188779150872226758777767851817959201815515190258293344888688830866994705291878258235885115496971399287910745483131380907440723094746984928092846339187554528647911937941479731432631990396593375626922129709150605749968192107427286283531180822421877014456979496455597917536675628283244920698961869742965251178711508973222976397630399529388774826276587498194664667858621159215336923392317472239693972665586221128325754848236528493945117927459145508612794383446689641065251783569452401372665937384316821141977847706827913957729148573065513239632345884075706693935598281527421284725480962191301428691138868413493698218511534058396434317144858939136512897960279011858227924896947370487567512329637920557177476457241537716191248478835771604032217925135653498812731115775639525474668579175956158398157943272781744817654293182358805939517669554141131648809225305684515321389545305997798924181336117640393681549057115911988953697655301044955264365829701984432792542344574896821392274541704034403680194812655090924190595423389269617991562359817558276112593058648930589410167010849354246626493613439696329124428340689275236744188286717182253276452435278937975061368742408486271662263612859039446511214697189239642983271029877016809327307327276123474116816860651860932855465723874397898821286933979238923274973076809735939910457032304890669775738093709552765571155232934491604566437777351370711726609860317149995113689740413544883784697222847777835698281245453731463357199152865992673157123758287894472592279823893286717239558547898940502397538225235281575926914025804974443434135673199976531333437124935131633818673680558277982460231594152518581278123061367072813034865324745212991328523419203232451014602140679362684033364497992797948871189455307499832193339126513914235518926862302793191372997716841848734778145135464994995571449540923945409615488998695984992370923966477870634629183117539868273141506770363630776721964277325024906375898466557936678474355192731543713174626376915916183813321591903934884536102955782639766358339039111811821627343849669912738935946637833362809656303379461830472839862349777352119866701554924289779516753529618376992226468550234474951054701592676221941177215321525892596324645177784363722023341612981595825241259198905035134338832339347770477657933079544833165632712461482249135256594898225498362832409672516633559739565269409374735088778491937629353145254639885484385854826239981581239327755878894773155567741221484876338260321849764036247363499066464240938697944420109592744615783311632541713112474811593018185186223281934433197399985291925315937573737581643726822532272392732921363482396037819638511421887359448842756817244932544889198856969150206215278381449696144264202565398233588727251257617155968959721595533419763712622316661354686158908542869578289033221795482416875790505798787035244068687785805425558571684125692678965359883017571126344188699724457638123241938113938991128798632392211015558163891055598644296312693934486821277898729263979550581987603476766427613317284541847838647013781035141933143581426877376167115689194718454634463685459722143212772442212021556556393777782475173678433096398965272269619568209257834513291947355929948749386457317465162081177422584441532799862211486218988182614828329444551861667261913943448571797475525341265668341588532694481843221486389132278442196440843729419837699173374085449897847268813760172862521873543238546442821878957568768667267647208571381183541122677454247659572855523264372378862238595785473456149338985737385628281563426696544333898522601013472923592113919018222854355852611216131520161641118430994490587143262082197862476044117538687524755738169751162877442925837572329255273651931718201136496234364429734958157048465927538029881727533745715975675195514061265838234313203690952216474468565114994389409325487335244766269624764050984725615732951511497673385841252239183725197469502971299826127669853771906731472522369018897116875482438818402284452194574294217779978275955283449580603470659432947854734874432554816935746253223622485495482962444248331933677739877216115496174524408348117731756343547980341257611562888132307145987882322015456491309516575543727753706111393295112063557218819434661171898180808047708841388025273855723381574690301112446064397177303849136816727719944272888550926897813751705555775081664993262699947856542171151585922114643291121246112256458441223218148983225654326857192175348940854918354483533955231611864189997998267750732023544257973352196771185257136461903860853159896578909858125353488765243145914670784377176842951657282724444919922351234511612093503098797232384828366764473050758259158838635143651361803758473672224521911371648240723187444722281267454537805465747838632388821678968332711691137330313999644546612338984996488139245542928514147568111712546011398854563434845739448761655125726890577567156846285973135697623989183597195843797768471168335088127666968219218273875991167860467795598945778341259745315332542487811973461779379596514078164629861718959569797179146759518934358223479285916159978573731137372096521981119128801662753699916059424848274093159448304261942966844139595819229226596447775441377277824496621751367324398396572750869270859163266770701687597070784257705316508644232316456818807330976459467883365253536869224051429597546468548959551214442936356578977693786597423624885480337826254581224131137822159224937374854297231318393330655870888846638896741284382278638722697641215840487310951326517196359370495353318068983995146084908776132378434898718777484428446784821583125020347227717433627933684522989996108183834774693069607490614274429754731856275464673297605066528543279997624243203614733367713379825232896950751439627330996716393551494285399134923554506340167438582868881120267536766441281624935668817465956545172090622642341919925556409835344446172418272368249717669764932018348596153750473864867635313175518546248478345337434096502474165479184665447433583810506050838971644099238798748129185667139413139421589478637591902323298513403216321450708897302586186635532329499899513040787775361165467587958676838326192926556977914348155766886496624071295651627871715085687940325311971294238156637018676725395510575229979040695213753154595117845657908616488248246095713135461038953415365398959587145788243438666714871820488114137619758093522924571191377180706863361356717746183568284847171363359565759121814055675572903112633180253474338095108733978595154457837380184019295698237287386541895340412017499561735629751545191238347814701459434982322782684010685021481078885693692235659495648644913193791959482916996397989956457917213552124596381527147587654597131427814540401169985674138739852442688212981668624261579538212310317839453981874727548234311873547558144016474265593274761720513040129960161973482332171393317242135056103334822625224665261116318297168325119085354462417464227315219229598398112328583335661422933073642891349522872315265289756736683587289013818270475657129964334516953890702262107690745664742133243131676648187011231681568752575786958821412644519854223499588081436998245830601475871935884090686923688143971714816680188877733312934887437538257210286916378146473259365111714960252818222510397732853089578173253777912042672045653130183455575192364558817574458939569261271431838729295894619093846481472945647928945845589132965647553748968627736815531970615634851263937767635517232743295049811685762219999777194336552335168677334619284486664528528281456976786427567888977015473224664676466312328428473641249165942045849010483691559092508945538083416487927877192891547885717259288350828713602686935011437624222795362920254033108594736135163387643722344420264016543727434599655490592427545841167981552075412731698321483985762130641987391837935144532530856559794971337732473219203045749926392861987136663535665913477892807843451761481531412939522825391669389336874652859344532712558344444047508439381674798577855223516984907614555341368982592128912745382138803814656080281360425868566217628380539525126169203078915246304645184069752032498661947429745522454393345076111893893682606296511141768527627154109996541629331038735285108399432214408057615669221396904594486854751761248693364471906738771425858479747773337325159030103056378569961049145022529737226679871752779675784093626452858467216921197043743388582894767929479332414254102121897075569645206446197066949372657230146869605526343738993438439788325075341953771748557384308957268576522619309712869019807520364452275954961812332816665825332927728980686672134497661972985462585942777915525069661465128757975779687374257498415671986881377324588010173559596944132223968757937660708723971650307128527977272197909269329417395418534564997956468262979311837270941027416934561976207785446022364439179923492332648823142064665257632071177632293118461774877077373911605725699716674529845942975838916143262425608825816232402468652782739241875788682279491139379025622029232346358574672814764986452934203487689393994239514264808840166324252683771631559088526163552319911745263882206366168068216986899964183994532213133272201091886887945823907259983316104842878788835155377998612866872774965975383886804925916641596515325854176954543921412667504617504293652513774577103341231199609647394694185990246873227517663533213272477078746451633718568291271961752272443712816036144576473291759368749394654451911173832034144650702491468814398213147559764218292854606818421664172122409242108120602674179766755457835891695971592386925750108522749535854259923628804852537534183449118189972078685411723641444130534082858199782521919145683824196954343972638866293239354225868872155291262978392196472758803011269727263113488650526627231988746466109446854617218687969849632267744774449463468949588627249570344922844683324517573498453934832625962672195085939490919165569989463749841060128860803725589414829288142082313573974689591173767214208792867015273863754290796697653445804629232185544755562168702367591257191692289863486060902081729560823980311426157154667929295944355323124218736212283399614090244625885612272073667090107792297412429351404641242514346838768293262682108966954021195346983859175310594077337327711269363494196971802868916419175085812784741787754761721972503816151667864597166959577584393370889797587963468211762639707421585351595690115669838047197576156743267337256943676922355764574139481176339725913213539054371285305654334677355939638591933265659465766753697726379597391925438153668293728180611511998842373859336363674117536982974267629427213548885061709032486227408420332767437181288368844698426929265546754256434621326669953734319536364559663719944941354836353886472373862520783930197886688820644175263471822457855515265069685839476369311415838315279630624348119512112629983176357759356424277580536189627926852314374230725133523366392216718466523239302257483866709191823213178156612129715085631910746952695728987422312898156993588716231727155264234520832186129168272746933443433110528620406823382655938276276055211630678581504411991365929259723733388799328432513963457070361349892016361291902993571314579914719978133325207789354554287139173189764882638644374797824163479686856952404519984614261832882375686882944733985466709827112665553680383646171626377744892269408025235150136676313773968278745952166067986253659475667171388525803497478635229886165589659422343532831665923327634275404798247665351681481943249078234726995111861682927597955632377656786079505710306652216146726791188282434182902227785521546377849672322917511384352582507774309638208683449281607196374245222333129467811597717826295282881865242263539897882689965920424680479923785257856953502319205652843777236580761693676465822116531090276198359914333870851544742040605851875750838313519834851532996077607073821756446241426026981840497415151235493245868134886489565137863147438374908833357837869172175882182219863417593555853226533420613248247257628510256722115823258796351036889231126467471967764146268225183441498931843110634298841450192482203995639633736870608784133116816055793830119449984992801386901252346599878263604385992497613141419318935386384336582713743162861170404893134561782483407157467775456929853939535164446737251254873479946389602671867968648584621011144987306452937185479860951749409527777760171584742296357046412534112639248847106321843588426150887963622086272035604278248511747250732679479424732321396722673847735316773976263545243743881345884190559021294682839196953126144118351059309453346035611767424634912598121387802032784012401153192935187817795284993756653258717432848829369935469817352326372597201524293381377090474498155260328617142763325696745259484745383456779737819410984767426897473857507441137415824631761469626911682419927974407697355915671489116821275130177324177168472520916792638552199611278264109042728048208473178235945793775983307184611928315331225712552389141060876410112535909772932686689686963327642733725910415445908693891448356393983941501179825510153679738 diff --git a/dat/24/re/11.txt b/dat/24/re/11.txt new file mode 100644 index 0000000..bbc2705 --- /dev/null +++ b/dat/24/re/11.txt @@ -0,0 +1 @@ +112 1110 163902 0 7656027 83039 9 74 diff --git a/sol/24/CMakeLists.txt b/sol/24/CMakeLists.txt index 339a40a..27fffa7 100644 --- a/sol/24/CMakeLists.txt +++ b/sol/24/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES "day08.cpp" "day09.cpp" "day10.cpp" + "day11.cpp" ) add_library(aoc24 STATIC ${HEADERS} ${SOURCES}) target_include_directories(aoc24 diff --git a/sol/24/aoc.cpp b/sol/24/aoc.cpp index 99e9e03..ed6931b 100644 --- a/sol/24/aoc.cpp +++ b/sol/24/aoc.cpp @@ -26,8 +26,9 @@ auto entry([[maybe_unused]]std::span const& args) -> std::expected< case 6: return day06(args); case 7: return day07(args); case 8: return day08(args); - // case 9: return day09(args); + case 9: return day09(args); case 10: return day10(args); + case 11: return day11(args); default: return aoc::make_error(fmt::format("day {}", day), std::errc::not_supported); } diff --git a/sol/24/day09.cpp b/sol/24/day09.cpp index e1e7bf9..eee089d 100644 --- a/sol/24/day09.cpp +++ b/sol/24/day09.cpp @@ -1,7 +1,243 @@ +#include +#include +#include +#include + #include "aoc.hpp" #include "aoc/utils.hpp" #include "fmt/format.h" -auto aoc24::day09([[maybe_unused]]std::span const& args) -> std::expected { - return aoc::make_error("", std::errc::not_supported); +using namespace aoc::types; + +namespace dsk { +enum class format : u8 { + file, + free, +}; + +class block { +public: + block(u32 id, dsk::format format) : m_id(id), m_format(format) {} + + auto id() const -> u32 { return m_id; } + auto format() const -> dsk::format { return m_format; } + auto str() const -> std::string { + using namespace std::string_literals; + std::string str{"{"}; + auto const id = m_id == aoc::lim::max() ? "na" : std::to_string(m_id); + str += " id: "s + id + ","; + str += " type: "s + std::to_string(u32(m_format)) + ","; + str += " }"; + return str; + } + +private: + u32 m_id; + dsk::format m_format; +}; + +class page_block { +public: + page_block(u32 id, dsk::format format, u32 size) + : m_format(format), m_size(size) { + for (u32 i = 0; i < m_size; ++i) { + m_blocks.emplace_back(id, format); + } + } + + auto format() const -> dsk::format { return m_format; } + auto size() const -> u32 { return m_size; } + auto blocks() const -> std::vector const& { return m_blocks; } + + auto str() const -> std::string { + using namespace std::string_literals; + std::string str{"page_block {"}; + str += " size: "s + std::to_string(u32(m_size)) + ","; + str += " data: [ "s; + for (usize i = 0; i < m_blocks.size(); ++i) { + str += fmt::format("{}: ", i); + str += m_blocks[i].str(); + if (i < m_blocks.size() - 1) str += ", "; + else if (i == m_blocks.size() - 1) str += " "; + } + str += "]"; + str += " }"; + return str; + } + +private: + dsk::format m_format; + u32 m_size; + std::vector m_blocks{}; +}; + +class disk_map { +public: + disk_map(std::string const& input_str) { + m_pages = input_str | std::views::filter([](auto const& c) { return c != '\n'; }) + | std::views::transform( + [i = 0u, id = 0u](auto const& size_char) mutable { + auto const f = format(i++ % 2); + auto curr_id = id; + if (f == format::file) + ++id; + else + curr_id = aoc::lim::max(); + return page_block(curr_id, f, u32(size_char - '0')); + } + ) | std::ranges::to(); + } + + auto pages() const -> std::vector { + return m_pages; + } + + auto to_blocks(std::vector const& pages) const -> std::vector { + std::vector tmp{}; + for (usize i = 0; i < pages.size(); ++i) { + auto const& blocks = pages[i].blocks(); + for (usize j = 0; j < blocks.size(); ++j) { + tmp.push_back(blocks[j]); + } + } + return tmp; + } + + auto defrag_block(std::vector const& blocks) -> std::vector { + if (blocks.size() == 0) return {}; + + std::vector cpy = blocks; + usize free_index = 0; + usize file_index = cpy.size() - 1; + while (free_index < cpy.size()) { + free_index = next_free(cpy, free_index); + if (free_index >= cpy.size() - 1) break; + file_index = next_file(cpy, file_index); + if (file_index == 0) break; + + if (free_index >= file_index) break; + std::swap(cpy[free_index], cpy[file_index]); + } + return cpy; + } + + // auto defrag_page(std::vector const& pages) -> std::vector { + // if (pages.size() == 0) return {}; + // + // std::vector cpy = pages; + // usize free_index = 0; + // usize file_index = cpy.size() - 1; + // while (free_index < cpy.size()) { + // free_index = next_page_free(cpy, free_index); + // if (free_index >= cpy.size() - 1) break; + // auto const& tmp = cpy[free_index]; + // + // file_index = next_page_file(cpy, file_index, tmp.size()); + // if (file_index == 0) break; + // + // if (free_index >= file_index) break; + // + // fmt::print("{}, {}\n", file_index, free_index); + // std::swap(cpy[free_index], cpy[file_index]); + // } + // + // return cpy; + // } + + auto checksum(std::vector const& blocks) const -> u64 { + u64 sum = 0; + for (usize i = 0; i < blocks.size(); ++i) { + auto const& block = blocks[i]; + if (block.format() == format::free) break; + sum += i * block.id(); + } + return sum; + } + + auto debug_str(std::vector const& blocks) const -> std::string { + using namespace std::string_literals; + std::string str{}; + + for (usize i = 0; i < blocks.size(); ++i) { + auto const& b = blocks[i]; + if (b.format() == format::file) { + str += fmt::format("{}", b.id()); + if (b.id() > 9) str += "'"; + } else { + str += "."; + } + } + + return str; + } + + auto str() const -> std::string { + using namespace std::string_literals; + std::string str{"disk_map [\n"}; + auto const blocks = to_blocks(m_pages); + + for (usize i = 0; i < blocks.size(); ++i) { + str += " "s + blocks[i].str(); + if (i < blocks.size() - 1) + str += ",\n"; + } + str += "\n]"; + return str; + } + +private: + auto next_free(std::vector const& blocks, usize start) const -> usize { + while (start < blocks.size()) { + if (blocks[start].format() == format::free) + break; + ++start; + } + return start; + } + + auto next_file(std::vector const& blocks, usize start) const -> usize { + while (start >= 0) { + if (blocks[start].format() == format::file) + break; + --start; + } + return start; + } + + auto next_page_free(std::vector const& pages, usize start) const -> usize { + while (start < pages.size()) { + if (pages[start].format() == format::free) + break; + ++start; + } + return start; + } + + auto next_page_file(std::vector const& pages, usize start, u32 size) const -> usize { + while (start >= 0) { + if (pages[start].format() == format::file && pages[start].size() <= size) + break; + --start; + } + return start; + } + +private: + std::vector m_pages{}; +}; +} + +auto aoc24::day09([[maybe_unused]]std::span const& args) -> std::expected { + auto res = aoc::read_text("./dat/24/ex/09.txt"); + // auto res = aoc::read_text("./dat/24/re/09.txt"); + if (!res) return std::unexpected(res.error()); + auto const txt = *res; + + dsk::disk_map dsk{txt}; + fmt::print("{}\n", dsk.debug_str(dsk.to_blocks(dsk.pages()))); + auto const bl = dsk.defrag_block(dsk.to_blocks(dsk.pages())); + fmt::print("{}\n", dsk.debug_str(bl)); + fmt::print("{}\n", dsk.checksum(bl)); + + return {}; } diff --git a/sol/24/day10.cpp b/sol/24/day10.cpp index c5d1708..a4c60bb 100644 --- a/sol/24/day10.cpp +++ b/sol/24/day10.cpp @@ -14,25 +14,6 @@ namespace lpf { using edge_t = std::pair; using edges_t = std::vector; -class node { -public: - node(i32 id, i32 value, edges_t const& edges) - : m_id(id), m_value(value), m_edges(edges) {} - - auto operator==(node const& other) const -> bool { - return m_id == other.m_id && m_value == other.m_value; - } - - auto id() const -> i32 { return m_id; } - auto value() const -> i32 { return m_value; } - auto edges() const -> edges_t const& { return m_edges; } - -private: - i32 m_id; - i32 m_value; - edges_t m_edges; -}; - class trail { public: trail(std::string const& str) { @@ -71,6 +52,32 @@ private: usize m_cols; std::vector m_data; }; + +class node { +public: + node(i32 id, i32 value, edges_t const& edges) + : m_id(id), m_value(value), m_edges(edges) {} + + auto operator==(node const& other) const -> bool { + return m_id == other.m_id && m_value == other.m_value; + } + + auto id() const -> i32 { return m_id; } + auto value() const -> i32 { return m_value; } + auto edges() const -> edges_t const& { return m_edges; } + +private: + i32 m_id; + i32 m_value; + edges_t m_edges; +}; + +class graph { +public: + +private: +}; + } auto aoc24::day10([[maybe_unused]]std::span const& args) -> std::expected { diff --git a/sol/24/day11.cpp b/sol/24/day11.cpp new file mode 100644 index 0000000..932ac3a --- /dev/null +++ b/sol/24/day11.cpp @@ -0,0 +1,141 @@ +#include +#include +#include +#include +#include +#include +#include + +#include "aoc.hpp" +#include "aoc/utils.hpp" +#include "fmt/format.h" + +using namespace aoc::types; +using namespace std::string_view_literals; + +namespace ms { +class magic { +public: + magic(std::string const& str) { + m_stones = str | std::views::filter([](auto const& c) { return c != '\n'; }) + | std::views::split(" "sv) + | std::views::transform([](auto const& c_str) { + return std::stoll(std::string(std::begin(c_str), std::end(c_str))); + }) | std::ranges::to(); + } + + auto blink() -> void { + std::vector stones{}; + for (usize i = 0; i < m_stones.size(); ++i) { + auto const stone = m_stones[i]; + auto const str = std::to_string(stone); + + if (stone == 0) stones.emplace_back(1); + else if (str.length() % 2 == 0) { + auto const a = std::stoll(str.substr(0, str.length() / 2)); + auto const b = std::stoll(str.substr(str.length() / 2)); + stones.emplace_back(a); + stones.emplace_back(b); + } else { + stones.emplace_back(stone * 2024); + } + } + + m_stones = stones; + } + + auto blink(usize order, std::span const& stones) -> void { + std::vector compute{}; + + for (usize i = 0; i < stones.size(); ++i) { + auto const stone = stones[i]; + auto const str = std::to_string(*stone); + + if (stone == 0) compute.emplace_back(1); + else if (str.length() % 2 == 0) { + auto const a = std::stoll(str.substr(0, str.length() / 2)); + auto const b = std::stoll(str.substr(str.length() / 2)); + compute.emplace_back(a); + compute.emplace_back(b); + } else { + compute.emplace_back(stone * 2024); + } + } + + m_mutex.lock(); + m_tmp.push_back({order, compute}); + m_mutex.unlock(); + } + + auto size() const -> usize { return m_stones.size(); } + + auto join(i32 current) -> void { + std::sort(std::begin(m_tmp), std::end(m_tmp), [](auto const& a, auto const& b) { + return a.first < b.first; + }); + + m_stones = {}; + for (usize i = 0; i < m_tmp.size(); ++i) { + for (usize j = 0; j < m_tmp[i].second.size(); ++j) { + m_stones.push_back(m_tmp[i].second[j]); + } + } + m_tmp = {}; + (void)current; + + // fmt::print("join... {}, {}\n", current, m_stones.size()); + } + + auto data() const -> std::vector const& { return m_stones; } + auto str() const -> std::string { + std::string str{}; + for (usize i = 0; i < m_stones.size(); ++i) { + str += std::to_string(m_stones[i]); + if (i < m_stones.size() - 1) str += " "; + } + return str; + } + +private: + std::vector m_stones{}; + std::vector>> m_tmp{}; + std::mutex m_mutex{}; +}; +} + +auto aoc24::day11([[maybe_unused]]std::span const& args) -> std::expected { + auto res = aoc::read_text("./dat/24/re/11.txt"); + // auto res = aoc::read_text("./dat/24/ex/11b.txt"); + if (!res) return std::unexpected(res.error()); + auto const txt = *res; + + ms::magic magic{txt}; + fmt::print("{}\n", magic.str()); + + auto const max_threads = std::thread::hardware_concurrency(); + + for (i32 i = 0; i < 75; ++i) { + auto const& stones = magic.data(); + auto chunk_size = stones.size() / max_threads; + if (chunk_size == 0) chunk_size = stones.size(); + + std::vector threads{}; + for (usize k = 0; k < max_threads; ++k) { + auto start_index = k * chunk_size; + auto end_index = std::min((k + 1) * chunk_size, stones.size()); + if (start_index >= stones.size()) break; + + // Fix: Proper range span construction + std::span split{stones.data(), static_cast(2)}; + threads.emplace_back([&]{ magic.blink(k, split); }); + } + for (usize k = 0; k < threads.size(); ++k) { + threads[k].join(); + } + magic.join(i); + } + + fmt::print("stones: {}\n", magic.size()); + + return {}; +}