parent
7c4d518400
commit
120b14f067
@ -0,0 +1,70 @@ |
||||
const std = @import("std"); |
||||
|
||||
// Although this function looks imperative, note that its job is to |
||||
// declaratively construct a build graph that will be executed by an external |
||||
// runner. |
||||
pub fn build(b: *std.Build) void { |
||||
// Standard target options allows the person running `zig build` to choose |
||||
// what target to build for. Here we do not override the defaults, which |
||||
// means any target is allowed, and the default is native. Other options |
||||
// for restricting supported target set are available. |
||||
const target = b.standardTargetOptions(.{}); |
||||
|
||||
// Standard optimization options allow the person running `zig build` to select |
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not |
||||
// set a preferred release mode, allowing the user to decide how to optimize. |
||||
const optimize = b.standardOptimizeOption(.{}); |
||||
|
||||
const exe = b.addExecutable(.{ |
||||
.name = "day17-easy", |
||||
// In this case the main source file is merely a path, however, in more |
||||
// complicated build scripts, this could be a generated file. |
||||
.root_source_file = .{ .path = "src/main.zig" }, |
||||
.target = target, |
||||
.optimize = optimize, |
||||
}); |
||||
|
||||
// This declares intent for the executable to be installed into the |
||||
// standard location when the user invokes the "install" step (the default |
||||
// step when running `zig build`). |
||||
b.installArtifact(exe); |
||||
|
||||
// This *creates* a Run step in the build graph, to be executed when another |
||||
// step is evaluated that depends on it. The next line below will establish |
||||
// such a dependency. |
||||
const run_cmd = b.addRunArtifact(exe); |
||||
|
||||
// By making the run step depend on the install step, it will be run from the |
||||
// installation directory rather than directly from within the cache directory. |
||||
// This is not necessary, however, if the application depends on other installed |
||||
// files, this ensures they will be present and in the expected location. |
||||
run_cmd.step.dependOn(b.getInstallStep()); |
||||
|
||||
// This allows the user to pass arguments to the application in the build |
||||
// command itself, like this: `zig build run -- arg1 arg2 etc` |
||||
if (b.args) |args| { |
||||
run_cmd.addArgs(args); |
||||
} |
||||
|
||||
// This creates a build step. It will be visible in the `zig build --help` menu, |
||||
// and can be selected like this: `zig build run` |
||||
// This will evaluate the `run` step rather than the default, which is "install". |
||||
const run_step = b.step("run", "Run the app"); |
||||
run_step.dependOn(&run_cmd.step); |
||||
|
||||
// Creates a step for unit testing. This only builds the test executable |
||||
// but does not run it. |
||||
const unit_tests = b.addTest(.{ |
||||
.root_source_file = .{ .path = "src/main.zig" }, |
||||
.target = target, |
||||
.optimize = optimize, |
||||
}); |
||||
|
||||
const run_unit_tests = b.addRunArtifact(unit_tests); |
||||
|
||||
// Similar to creating the run step earlier, this exposes a `test` step to |
||||
// the `zig build --help` menu, providing a way for the user to request |
||||
// running the unit tests. |
||||
const test_step = b.step("test", "Run unit tests"); |
||||
test_step.dependOn(&run_unit_tests.step); |
||||
} |
@ -0,0 +1,141 @@ |
||||
222122122231132231322211344413343224131243444121251443112234151542155511322545515353242232442232122121113322132431241211221323312223212221121 |
||||
221212212121132323222322221441212241211143123341542312443541441234253155552535351521123434324424112444312231142314323423333123222232322121212 |
||||
112123212233331112123312314221334241332121421445511221433331444342123424512243235222323232332214423133232333134131224411112332133111112322222 |
||||
122112222212212132323131242113444331433142445424221251521555235313532112313322255134553552115315313113243412233442311143122123223213133222322 |
||||
121313323133211312321442111122342222424533324321412433351321412345435313521524255245334452224535431325224424132131433122113421123223322311312 |
||||
223322322113231323323121431311331442323325322143542121151325423243135154124441223123155211514115544224224533122432441422331233213213211112333 |
||||
211221333111231334442233213313212124115241213343454552345314425543233451244541113145233332551551121222453145312111413413443113322222213231321 |
||||
123311112122231441124143331243431411534132154525553531252355411333225115522455524445413424142111522433415132123423211111323311313113121231233 |
||||
322111112133321334412334314241211332332422125351345425423241534445446644362435542243452125422432123525432131353431343334422142121333313331233 |
||||
233211232323244144111142443214334334225331424324315412423655324625464225645225446224264513323334243452123134343154223113143324242113323123313 |
||||
231123211133314114321433243123453221334333341441132142235552352664454363455625644464636355113312211542322431555315234133432213313113212111323 |
||||
322231111311122134234331434525352421131241251443436635363264522235224325354443323522665466432342414322225322411415333244143412424112323321321 |
||||
233121113341412342331434214555411115225323135453463244323466652642665434364266544342443433434564424514332533354543241211421322413333221123232 |
||||
311131333233343324413424153323423345332242452565235662565663542524466536625224526262524624243565535253123511544432244121434324331124422211132 |
||||
122213133134314141322123243244335453335224456653226324263455333252343355243646225335622653352266644552344553545155152144214314143313312113213 |
||||
232311331231441124232413325415351221421424542236466444346645423332345642453356546255255534523644555255234141235541125144434344222121212111231 |
||||
323323212141143212223253242543455511144543333522666354523243426343544622622335456222525264352455366533321354135554533153334321211424144422231 |
||||
333111323344114441243414324425531413542626426526444523253254545343656655322645254625623544246365264223355552353144515344531112113413424431331 |
||||
231132344212442123351153414143113255223464232642634333545545466642533635554365353252353642366255342545634642331522355325314242332444344424312 |
||||
213234222433433231333222232355323332462646662456525466542353637457336556766575474556252626646262625523353652113132424511555234124113123423413 |
||||
232244322434331132311455332234251324564365335623223442646537665345454677376667537347544252333654532533342252631322251552241354433123422122231 |
||||
312114121134133442432314431145236635222643442556655643547466346537335363645633375743545647554364643326322443363541134232124312524314113432313 |
||||
113133413314323345412414354435446633262236345636624764744374547733536753443633745357344765443545344643242525363341212221532554514432312412422 |
||||
121341114244342531442555522144652356644324256322477437765734635435435657346767657336465566756564552336243532442625553323432344222141212234122 |
||||
323331332132215445234524223336654356565632454234645675454545644567457765447477766434777735444736666645443245633532213211243125231321212121222 |
||||
431242423432224422425152415342454325346345643333667766543746476463453573473553577633365757643767754326656542442444231142135431321331423431311 |
||||
414422414421443352213413216453433544624563244747675366653673453455637577536657367376654447437777473725633325253242434113325125241221234321442 |
||||
242312341232332541411233342324526442252543334546763337353554644476333653647465333554633446645743363534225555622646632331553242521431324323314 |
||||
341341311315443125523533535655464536255655765756445545457465775466756337467674675544754344466775555555322325244364554461451522115535331444313 |
||||
311444421242232535232543566462654535446555447575436754664366755378774578855758543436463465435767465436566534633233655452534141325434434332223 |
||||
341132443324155244522354642365632653556373633763467535566653475885475474685486557455477563575664464653366333556626336622351544334233252231224 |
||||
431344122531333525141422424664465256435374554563565374755757657855685856457576688768575335476567357737355643362652222423452154221351323413121 |
||||
232442322115534513553226545526236635776453767365563456587645655857668464487555677754868566754576577764637574363362555554463133323343251223213 |
||||
242423441314215242212254666632655654647544767767665345676744878455547584466575858744855874675757443553363377322466363433245225455222444441143 |
||||
321141243421353544154442523556255334545353474354354565858848856486755487556444884647455885473463335765344476346665252262344522522332531532244 |
||||
221343423231531241353245434425637453355346534643586845685644485487765558875466556874546665646656576777674463463626652323526344343514324531224 |
||||
322112455343245245662543426345546453447335453758858546684788878866474787775866665776676855746784357436546435645762663462265445441432332143122 |
||||
411135144511455525543223656645435753747563437786477448454457787876754768755666457664456858776456475754677374675364543435556532143413445231343 |
||||
431213424253515326266624236442565654763763637886675856865686864588677556675748567585578754757685657465674574573645536663422466352545254245334 |
||||
342411222453551556535633655655643475737344557668666556846788686564746455567667746746676478864487665743533477344773666262244534345544133453212 |
||||
313233435523244224224224224457565435764733787845577566456887775678987776797855566778588587578675746643444434455343422225566455444553321313143 |
||||
113412524114122623622546522675376537754356785565487477864745788559788586968757786878655585747654787544337446535535345655325245631121152534351 |
||||
333423224144335462322242456634775675775466865685888455647696985599766868557797768757957545847585668477476555565743465622323654265224221113414 |
||||
323223322231256466254463563674667755667465444457764655545999567659965579658779867755676846745556666655767373767436676243366263252431145144244 |
||||
144454412235442445532326654654473757557655487548675774686985975967586996555656956568697685767765476664743436574557535654226654554552425413553 |
||||
222222112245252223532344463573455655745555878466657545758575687776999867858597867868767855464784686754577747546434573663662322263235332511412 |
||||
135253144321522556463664545543434773376544465757845558798669956959696577568655897787679576864665545777585577434536475655223265436355523541453 |
||||
245134515145562655323535663476734667867884856685475797758586968688657659657766757775657856969465857777576845476736646555424464262243223424411 |
||||
325251343131336545335233634756567345656464664487566658685756588678576877778878558798569557767687488655487654745736453465445642226333343232325 |
||||
215145425525636535544555774546333357445567586844869688788667975778656855679557956788788868889685754746886647333367476673535456554634314554545 |
||||
211544421215625434644423576546464468767548584446979866788985569668798767877988599669676975557587785677878746445654364357544556224264422451424 |
||||
215334112242254254255237434647573364574547556887985699889956887968877879877899677989757685555569766585848776666355534576352532232466445113154 |
||||
321415512242235422652654466456533564477856466498778555668667976967999779989869886756789996979857645477788848587437536565655432365255323421523 |
||||
133142513525422364643655574747664864567874644777775775979879867796778887888696986689786696656575797885678556845554377345554334643264525543445 |
||||
142555214134363424562747533634657884744488786556975596759557767889797678789688799678999789558598657788485556564634773344344655623524412112144 |
||||
242131121452542363553433474344666488757567657968659857886677686697768777866769777988679799889687568446685586677443645564654323666545215444335 |
||||
335534251235355465626343634453776464886844685989658757998768886979679986776787678879896989786988959655867645454534346565775223344264443232412 |
||||
211412213632436324563636774777565788785567788999866656589696996699669896776798968879976969585957876654666747547644443774345426553645654414432 |
||||
131212555236333224233556664365456747864547576799788888899978689689966978889779986687997795878787686675478887484677356565335466522433642233421 |
||||
132555242335655624566773335547668764775754597599757859986996988689886786999679977889767776768679966867674668545453333436675632364235552555225 |
||||
543345554323425566277565775665677774588445899885889678677767679699698699786877768698889779969865599865455676567774333453546545325363465241153 |
||||
454315546552224634567466554455468787487467677878676599867977888696698889999989777776976778857995865995778645565583557535534454322666334443223 |
||||
344434333654356353646733364467866877477868886679769789797997776889897887887798697689776997957859788655556856477476446736547545646325653211524 |
||||
412325525232545264257647537337744575447875695879575868969886769677978877987998867987666866896586685666456655856555765556355555623263532545525 |
||||
213322543424652533234344353577647648644699869755765997768989689799797979989988989676967968685866767867667686848557774647574353236565445514535 |
||||
431223153622644655663364556534566485867588559795955969769899769877978898998998778696669997888787865695666648674684477336534533242223243543123 |
||||
331325156532322624267346777358454567758478665799855668877787968998987898789977899977799667866757659575665465647765534576373664454445554452244 |
||||
434244324534323244667474775445568788564785675958675699779767977898899988979898779768896976668976598795948655588644776646345435266334433423121 |
||||
411232445234266435556455576454578576464766868586997899868677787999799988978987878878687666887776768769568667846784536664343465623635642224352 |
||||
113545245462445532357533764338457885767798969868689789768779779877898989988788999777986677677557759888987456886468644457653543366355655433244 |
||||
453132215426636344446354376478646464757566966989878966888777688778999779897887997999687896968767959887886565485465567464763333562665654244234 |
||||
334442534553425264556336675764447446568897998855957968797669787787988789797978899867698889999889757676755565486758744755544563666264662223333 |
||||
154123456625353626446467377666845574758876977886667899687798677988789987997788989997799897676595568766855655688764657735443435254424224125333 |
||||
111452122256632622533454665767486748878885899765988667886879798998998889899878889887976679998875885968858847754457535354553465435553523242445 |
||||
524454154564524426474757764744478478566487565575579998876766999879797899789878979779678979966986956768968564586565553667456372465452426551424 |
||||
454321552243556433745466563555855654867585979559888869689697699798789787797999788769898779867587859668746644665467334565476766622644362111455 |
||||
113513156335233644354475637437786466656466766555975869878969678799889987989889899696668887796897665697674566588884573757345744263236662351155 |
||||
534141434433454622664663637557877678668458969695669686778766768779989998979898889668866979875995668896865865876465745335566465642465564235344 |
||||
424514223452466324667675544677676665884567878958695799966699686987888787978778987899767899976766989699844485668476366434634654233245256521245 |
||||
243131413263565235576657334546677484445487888675975687966986989679877999979878876898966798875656867566456544557575346675735544244462546534354 |
||||
452333545653543346534575657363757888858485886596596696677986967996787997988896788876766979879789688877876554788865376557667723636356356434315 |
||||
434231214245456235464536355743687845587887968785685777996866898889787778998767768887987669799977558785466746454563674537564456465246622544313 |
||||
535224144435335653533536333334855754466485786667599868867687677897876986969686777899796669659568575888546474654477455363373544465352461545553 |
||||
333222451255262433456533356637785864558574885576757776966679778978977667688689768678699979978895586656875657467564567746766262326662321111234 |
||||
354145242522363436243567443354586676746458969898899895767769869789676866768798889668688898587779897757464848467734434665653252254266225455244 |
||||
424533211322646256544734764557788486568545899865566877996897699697698777778869968686897699769997687774446874767456553434653465336233625335222 |
||||
431251444525363244345435335753357677588765675769756979678686768997689978767776798699897659868976759846556775877445545473454233554242341342454 |
||||
413434435334664425434474545754337446778585859577767967986879799867667897679986768779875858597865756684466745647435575444762342565424531512315 |
||||
245123553436255345635447377443653744457664744976787799656967887787877777876699968789798598557968697485466576484543453345735366632434411343552 |
||||
545545254546625532565377367365754757576844684786579995796965989766866879999778799768889577887588994775786555546475363547444525543456225331151 |
||||
511225125113346243455335377476664477867745486869567979586795878869877798779889777588688987585877787567558577644733644536454222356226244334223 |
||||
415225245113442552522654646475745466554558784688685866799587697669997668687667796996958967686675445476768868673444654435355554454244325441444 |
||||
142423555252362223524453353664456647455566454547686898695859786666766768866788885877569679896876477774856675377776776354633342444243313444254 |
||||
551525511245533525664436346466634648855747546886675595567799566987895595596858568956657757988677854487568864767737656567465662324645541253314 |
||||
322333152454643544635262636753467547587777788548555688967796678896878566897789878876775866989648848785745775745636736575325533555452211552554 |
||||
243253231351334356625553657333767546856444585674749879977775668567795656899989885769587558558788784458578654677777434565524266565333433543151 |
||||
124322232154236565345326256474456756445445648887684458886795956558878756666688865966868976585845454845664435437655634544462324556521412123213 |
||||
435153425521352635625365563737336443534556845844854687676866689559786795868887885857957686887845867768765563673356446764524224636611535222551 |
||||
321232444555136553346446255633576636675648865568774747666868556899959997969886767585865657448467454667575664643535336243225323252353222114123 |
||||
222232144315226533365254247366356533346678444588564868475699685565967775967779765976695478474557567586455764576335655635352244444114532352451 |
||||
244325224221353222655235322363666575674468755478868866585597966759896568585968898778886848578645846758437633453674332364542324535333434444423 |
||||
124424212312322353445233226475343535756668457785745786768488688955858689978999989558857677668548788787576355363346435233655442654341541222241 |
||||
421212512423132224623636654663756774656365547647578747847785668479667586795699966884677644687777488667676456575337454246642663314215521452442 |
||||
442214222241212134665264656453354654645474574488754466674785465844468747886767758868848587687568478457673777577755564363335334413341454411331 |
||||
331314454144121322334255365536755456777476736747665748457788784667844688666746684648878565668548876634536745767735635445246456423415341211421 |
||||
424333432123434115336233266566554336363463776756766658678748776847584754467848457575465585586858554333633775735345256543636634154352131421122 |
||||
233331424153545552352442352626444664444465757367686564666786544445784586887864555557857885458476557657563443577355626352223242535413511154434 |
||||
422342433424245224363656362243227455664455744456745547675485468556445447584755747568554847756843566376337467375436624642464431315212142332334 |
||||
442241354145524235544332652243546744447536465436654485684558877577547445748658684767857867477555556755343463456255542354544222113323512322224 |
||||
424111213243521532246263565442433335777346375463546545688587647868586885577575774448758847463546645537646536745636253663435445242343541413242 |
||||
144334425213415114225455553624443367363446537443543666388445776647447748866864676764844733675676746763743344622564253522432441535231421221214 |
||||
431321344512412321544365653545534625657533676457765445745646575844654875648555666886784776755737637647464476454666563455654123232421221314411 |
||||
122342424114251114312525352625262244445757753554735333674364744557875446745667785854476555473444356747433232335453465556451312125322224131424 |
||||
113132223314141241533526346455635366446574373754677674373367655336747564575786335373446375544455464666435553233435355332523245311343121111444 |
||||
332333112412255435325143343656235535566446637646766573535554363446433434746537536376443544644463534344646534266536424235324331145343214124113 |
||||
124122413211343423245421166456246553242243446643754653635646644467475557665673365634766734537533337632334632333355345543142143112444332132121 |
||||
444313143421325143515234233434232255555524657645367337457377566343675745756466757665453667774467546644242536644552355245532525342251413331441 |
||||
434312112411442132335453223553655662324246336345737345565775675677733644547476453653467374554754642342454422223645424133235444233241413312443 |
||||
113212423114112225114141322132664664534555243356374637363546554367556644334644456647563756463373464226663546425523344414332531245142411322111 |
||||
234444141443224442522134515435355435524553322246464436734477574335677773376455563333333665757754355324246643562243321433511453433322143121313 |
||||
214214123222212215153114332531522235526564244224346555756476744656353773355574735363674766655456553436243653623231533312424521514413131333244 |
||||
113141423334143121543212434133516456244563335325362444446363647446667574574436544643633333554445226543645323644352445432451525232133421241223 |
||||
111114313221431443243545121323351564424366523652526426225337343743544676573555746454645555666423463245546354225244251513215531332112424211422 |
||||
213113311231312132333132511153334535556233555345565445526642364743333475434573674742622225234444246554252256522443153342111134222324223241133 |
||||
122211344223331421153243235413313443252256344446654633245652254255575645667426666625325464332444355434466235525232534134424134414444444433123 |
||||
112211124221241344423555124441242315332564626264334623363554545342556252544623255262642455366625252623246233112131245151133112343113311222111 |
||||
133111221334141421214144242232144552335532552324444232243353243252526452254333364533433453553252644234445234435413425244451344411441314131221 |
||||
131213234444314423312125512332533242432136346556355236633654555546643565454426655422566542446423344233554143323311513424413423413133111421222 |
||||
121222312311133122233322532442435241545245232265542624524536662526244424224346236345644365265664425415152112542223245451222342414433121332211 |
||||
211333231411123241113131141223155141544345441335455322556534542335455354263622353536343466643356441412352535345232321341211133434144312132322 |
||||
322222112242223211333432321333114242141314213534622442554556526233234333635625234626466235625256332555542421541415145231424122443111313111221 |
||||
113121232122233134113431122233313415524144123324233323352453524552653345365526632522356642336455533512145355141435543243231411314444112113331 |
||||
131332132232321342232114341425511223245413414454524454532222534326352333332362336255646325221145452153244232153354114412221141231112122322333 |
||||
123332211322241431411143233142354322543555524212535411245626566452233624544655464643565522312225341154431415534511133434123112424233123311222 |
||||
231131312211322343443432211133213534151424125325143452444332426652443445466552643335411254544415443441514543424441234121321422322313323213223 |
||||
311221322232321221323411343233241451214322412152215542334151314124313244315144325515431155121521143324335343243231432433113414211221221213323 |
||||
231112211121312112331231411213441231225145115214415324135545114132323421241452531252423155152553453344553352442222241111314112113323321232313 |
||||
123232321112211223421311232412243213155523454531314113314534241323524512313223512212144555532423424211123331122231124212412123321321311121133 |
||||
112123123131331332321321321322332421134115124515412532313535253212112215121215331212325345523543544343351422412111222342233123312231133121332 |
||||
221123131333132322331422421334124233324442451431352341434451351322541333553224243113224314111333145521233421223113321342233313211232321333222 |
||||
222121333332331113312142331122114113111443345244442341124351355143153213151445415332443455532433335114344332141114234343122131212332121313211 |
@ -0,0 +1,13 @@ |
||||
2413432311323 |
||||
3215453535623 |
||||
3255245654254 |
||||
3446585845452 |
||||
4546657867536 |
||||
1438598798454 |
||||
4457876987766 |
||||
3637877979653 |
||||
4654967986887 |
||||
4564679986453 |
||||
1224686865563 |
||||
2546548887735 |
||||
4322674655533 |
@ -0,0 +1,176 @@ |
||||
const std = @import("std"); |
||||
|
||||
fn StackList(comptime T: type, comptime capacity_type: type, comptime capacity: capacity_type) type { |
||||
return struct { |
||||
const Self = @This(); |
||||
mem: [capacity]T, |
||||
length: capacity_type, |
||||
|
||||
fn add(self: *Self, value: T) void { |
||||
self.mem[self.length] = value; |
||||
self.length += 1; |
||||
} |
||||
|
||||
fn addIfNotNull(self: *Self, nullable_value: ?T) void { |
||||
if (nullable_value) |value| { |
||||
self.add(value); |
||||
} |
||||
} |
||||
|
||||
fn has(self: *Self, needle: T) bool { |
||||
for (0..self.length) |i| { |
||||
if (self.mem[i] == needle) { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
fn getMutableSlice(self: *Self) []T { |
||||
return (&self.mem)[0..self.length]; |
||||
} |
||||
|
||||
fn getSlice(self: *const Self) []const T { |
||||
return self.mem[0..self.length]; |
||||
} |
||||
|
||||
fn init() Self { |
||||
return Self{ |
||||
.mem = undefined, |
||||
.length = 0, |
||||
}; |
||||
} |
||||
}; |
||||
} |
||||
|
||||
const Direction = enum(u2) { |
||||
Up, |
||||
Down, |
||||
Left, |
||||
Right, |
||||
}; |
||||
|
||||
const ResultsByDirection = std.EnumArray(Direction, u32); |
||||
const Results = [150][150]ResultsByDirection; |
||||
|
||||
const NextCellCoordinates = StackList([2]usize, usize, 3); |
||||
|
||||
fn fillResults(board: []const []const u8, results: *Results, x: usize, y: usize, entry_direction: Direction, depth: usize) void { |
||||
//std.debug.print("Solving for {d},{d}, {any}, depth: {d}, total: {d}\n", .{ x, y, entry_direction, depth, results.*[x][y].get(entry_direction) }); |
||||
var previous_cells_list = StackList([2]usize, usize, 3).init(); |
||||
switch (entry_direction) { |
||||
.Up => { |
||||
for (1..4) |delta| { |
||||
if (x + delta < board.len) { |
||||
previous_cells_list.add(.{ x + delta, y }); |
||||
} |
||||
} |
||||
}, |
||||
.Down => { |
||||
for (1..4) |delta| { |
||||
if (x >= delta) { |
||||
previous_cells_list.add(.{ x - delta, y }); |
||||
} |
||||
} |
||||
}, |
||||
.Left => { |
||||
for (1..4) |delta| { |
||||
if (y + delta < board[x].len) { |
||||
previous_cells_list.add(.{ x, y + delta }); |
||||
} |
||||
} |
||||
}, |
||||
.Right => { |
||||
for (1..4) |delta| { |
||||
if (y >= delta) { |
||||
previous_cells_list.add(.{ x, y - delta }); |
||||
} |
||||
} |
||||
}, |
||||
} |
||||
|
||||
const previous_directions: [2]Direction = switch (entry_direction) { |
||||
.Up, .Down => .{ .Left, .Right }, |
||||
.Left, .Right => .{ .Up, .Down }, |
||||
}; |
||||
|
||||
var current_heat = results.*[x][y].get(entry_direction); |
||||
for (previous_cells_list.getSlice()) |previous_cell| { |
||||
current_heat += board[previous_cell[0]][previous_cell[1]]; |
||||
for (previous_directions) |direction| { |
||||
if (results.*[previous_cell[0]][previous_cell[1]].get(direction) > current_heat) { |
||||
results.*[previous_cell[0]][previous_cell[1]].set(direction, current_heat); |
||||
fillResults(board, results, previous_cell[0], previous_cell[1], direction, depth + 1); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
fn solveLines(lines: [][]u8) usize { |
||||
var total_max: u32 = 0; |
||||
for (lines) |line| { |
||||
for (line) |*char| { |
||||
char.* = char.* - '0'; |
||||
total_max += char.*; |
||||
} |
||||
} |
||||
|
||||
const x_max = lines.len - 1; |
||||
const y_max = lines[x_max].len - 1; |
||||
|
||||
var results: Results = [_][150]ResultsByDirection{[_]ResultsByDirection{ResultsByDirection.initFill(total_max)} ** 150} ** 150; |
||||
results[x_max][y_max].set(.Down, lines[x_max][y_max]); |
||||
results[x_max][y_max].set(.Right, lines[x_max][y_max]); |
||||
|
||||
fillResults(lines, &results, x_max, y_max, .Down, 0); |
||||
fillResults(lines, &results, x_max, y_max, .Right, 0); |
||||
|
||||
if (false) { |
||||
for (0..lines.len) |x| { |
||||
for (0..lines[x].len) |y| { |
||||
std.debug.print("{d} ", .{@min(@min(results[x][y].get(.Up), results[x][y].get(.Down)), @min(results[x][y].get(.Left), results[x][y].get(.Right)))}); |
||||
} |
||||
std.debug.print("\n", .{}); |
||||
} |
||||
} |
||||
|
||||
return results[0][0].get(.Down) - lines[0][0]; |
||||
} |
||||
|
||||
pub fn solveAll(reader: anytype) !usize { |
||||
var result: usize = 0; |
||||
while (true) { |
||||
var allocator_buffer: [20000]u8 = undefined; |
||||
var fba = std.heap.FixedBufferAllocator.init(&allocator_buffer); |
||||
var allocator = fba.allocator(); |
||||
|
||||
var lines = StackList([]u8, usize, 150).init(); |
||||
var empty_line_reached = false; |
||||
|
||||
var line_buffer: [1000]u8 = undefined; |
||||
while (try reader.readUntilDelimiterOrEof(&line_buffer, '\n')) |line| { |
||||
if (line.len == 0) { |
||||
empty_line_reached = true; |
||||
break; |
||||
} |
||||
lines.add(try allocator.dupe(u8, line)); |
||||
} |
||||
|
||||
result += solveLines(lines.getMutableSlice()); |
||||
|
||||
if (!empty_line_reached) { |
||||
return result; |
||||
} |
||||
} |
||||
} |
||||
|
||||
pub fn main() !void { |
||||
const stdout = std.io.getStdOut().writer(); |
||||
|
||||
const raw_in = std.io.getStdIn(); |
||||
var buffered_reader = std.io.bufferedReader(raw_in.reader()); |
||||
var reader = buffered_reader.reader(); |
||||
const result = try solveAll(&reader); |
||||
try stdout.print("{d}\n", .{result}); |
||||
} |
Loading…
Reference in new issue