From: Skullheadx <94652084+Skullheadx@users.noreply.github.com> Date: Wed, 3 Dec 2025 14:23:53 +0000 (-0500) Subject: day3 part1 X-Git-Url: http://git.skullheadx.com/nixos/static/projects/suckless.html?a=commitdiff_plain;h=34f9088216ea033bc1fe92edcc231db23dd79fd1;p=Advent-of-Code.git day3 part1 --- diff --git a/day3/build.zig b/day3/build.zig new file mode 100644 index 0000000..ede4aea --- /dev/null +++ b/day3/build.zig @@ -0,0 +1,156 @@ +const std = @import("std"); + +// Although this function looks imperative, it does not perform the build +// directly and instead it mutates the build graph (`b`) that will be then +// executed by an external runner. The functions in `std.Build` implement a DSL +// for defining build steps and express dependencies between them, allowing the +// build runner to parallelize the build automatically (and the cache system to +// know when a step doesn't need to be re-run). +pub fn build(b: *std.Build) void { + // Standard target options allow 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(.{}); + // It's also possible to define more custom flags to toggle optional features + // of this build script using `b.option()`. All defined flags (including + // target and optimize options) will be listed when running `zig build --help` + // in this directory. + + // This creates a module, which represents a collection of source files alongside + // some compilation options, such as optimization mode and linked system libraries. + // Zig modules are the preferred way of making Zig code available to consumers. + // addModule defines a module that we intend to make available for importing + // to our consumers. We must give it a name because a Zig package can expose + // multiple modules and consumers will need to be able to specify which + // module they want to access. + const mod = b.addModule("day3", .{ + // The root source file is the "entry point" of this module. Users of + // this module will only be able to access public declarations contained + // in this file, which means that if you have declarations that you + // intend to expose to consumers that were defined in other files part + // of this module, you will have to make sure to re-export them from + // the root file. + .root_source_file = b.path("src/root.zig"), + // Later on we'll use this module as the root module of a test executable + // which requires us to specify a target. + .target = target, + }); + + // Here we define an executable. An executable needs to have a root module + // which needs to expose a `main` function. While we could add a main function + // to the module defined above, it's sometimes preferable to split business + // logic and the CLI into two separate modules. + // + // If your goal is to create a Zig library for others to use, consider if + // it might benefit from also exposing a CLI tool. A parser library for a + // data serialization format could also bundle a CLI syntax checker, for example. + // + // If instead your goal is to create an executable, consider if users might + // be interested in also being able to embed the core functionality of your + // program in their own executable in order to avoid the overhead involved in + // subprocessing your CLI tool. + // + // If neither case applies to you, feel free to delete the declaration you + // don't need and to put everything under a single module. + const exe = b.addExecutable(.{ + .name = "day3", + .root_module = b.createModule(.{ + // b.createModule defines a new module just like b.addModule but, + // unlike b.addModule, it does not expose the module to consumers of + // this package, which is why in this case we don't have to give it a name. + .root_source_file = b.path("src/main.zig"), + // Target and optimization levels must be explicitly wired in when + // defining an executable or library (in the root module), and you + // can also hardcode a specific target for an executable or library + // definition if desireable (e.g. firmware for embedded devices). + .target = target, + .optimize = optimize, + // List of modules available for import in source files part of the + // root module. + .imports = &.{ + // Here "day3" is the name you will use in your source code to + // import this module (e.g. `@import("day3")`). The name is + // repeated because you are allowed to rename your imports, which + // can be extremely useful in case of collisions (which can happen + // importing modules from different packages). + .{ .name = "day3", .module = mod }, + }, + }), + }); + + // This declares intent for the executable to be installed into the + // install prefix when running `zig build` (i.e. when executing the default + // step). By default the install prefix is `zig-out/` but can be overridden + // by passing `--prefix` or `-p`. + b.installArtifact(exe); + + // This creates a top level step. Top level steps have a name and can be + // invoked by name when running `zig build` (e.g. `zig build run`). + // This will evaluate the `run` step rather than the default step. + // For a top level step to actually do something, it must depend on other + // steps (e.g. a Run step, as we will see in a moment). + const run_step = b.step("run", "Run the app"); + + // This creates a RunArtifact step in the build graph. A RunArtifact step + // invokes an executable compiled by Zig. Steps will only be executed by the + // runner if invoked directly by the user (in the case of top level steps) + // or if another step depends on it, so it's up to you to define when and + // how this Run step will be executed. In our case we want to run it when + // the user runs `zig build run`, so we create a dependency link. + const run_cmd = b.addRunArtifact(exe); + run_step.dependOn(&run_cmd.step); + + // By making the run step depend on the default step, it will be run from the + // installation directory rather than directly from within the cache directory. + 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); + } + + // Creates an executable that will run `test` blocks from the provided module. + // Here `mod` needs to define a target, which is why earlier we made sure to + // set the releative field. + const mod_tests = b.addTest(.{ + .root_module = mod, + }); + + // A run step that will run the test executable. + const run_mod_tests = b.addRunArtifact(mod_tests); + + // Creates an executable that will run `test` blocks from the executable's + // root module. Note that test executables only test one module at a time, + // hence why we have to create two separate ones. + const exe_tests = b.addTest(.{ + .root_module = exe.root_module, + }); + + // A run step that will run the second test executable. + const run_exe_tests = b.addRunArtifact(exe_tests); + + // A top level step for running all tests. dependOn can be called multiple + // times and since the two run steps do not depend on one another, this will + // make the two of them run in parallel. + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_mod_tests.step); + test_step.dependOn(&run_exe_tests.step); + + // Just like flags, top level steps are also listed in the `--help` menu. + // + // The Zig build system is entirely implemented in userland, which means + // that it cannot hook into private compiler APIs. All compilation work + // orchestrated by the build system will result in other Zig compiler + // subcommands being invoked with the right flags defined. You can observe + // these invocations when one fails (or you pass a flag to increase + // verbosity) to validate assumptions and diagnose problems. + // + // Lastly, the Zig build system is relatively simple and self-contained, + // and reading its source code will allow you to master it. +} diff --git a/day3/build.zig.zon b/day3/build.zig.zon new file mode 100644 index 0000000..31c47ea --- /dev/null +++ b/day3/build.zig.zon @@ -0,0 +1,81 @@ +.{ + // This is the default name used by packages depending on this one. For + // example, when a user runs `zig fetch --save `, this field is used + // as the key in the `dependencies` table. Although the user can choose a + // different name, most users will stick with this provided value. + // + // It is redundant to include "zig" in this name because it is already + // within the Zig package namespace. + .name = .day3, + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + // Together with name, this represents a globally unique package + // identifier. This field is generated by the Zig toolchain when the + // package is first created, and then *never changes*. This allows + // unambiguous detection of one package being an updated version of + // another. + // + // When forking a Zig project, this id should be regenerated (delete the + // field and run `zig build`) if the upstream project is still maintained. + // Otherwise, the fork is *hostile*, attempting to take control over the + // original project's identity. Thus it is recommended to leave the comment + // on the following line intact, so that it shows up in code reviews that + // modify the field. + .fingerprint = 0x9d38bdf64b83ecab, // Changing this has security and trust implications. + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.15.2", + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. If the contents of a URL change this will result in a hash mismatch + // // which will prevent zig from using it. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + // + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. Only files listed here will remain on disk + // when using the zig package manager. As a rule of thumb, one should list + // files required for compilation plus any license(s). + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + // For example... + //"LICENSE", + //"README.md", + }, +} diff --git a/day3/day3.txt b/day3/day3.txt new file mode 100644 index 0000000..b859e43 --- /dev/null +++ b/day3/day3.txt @@ -0,0 +1,201 @@ +7351233232223322522345345322222228225558344335123455325352426422353243225281562241322434848323342852 +5433445525443544954444443576444458442455326353465134665547447342574645424544275242443465156444215643 +2344323254238324344443324333412234342243363246314375326354514244431354834344246137562233387223242853 +4331233243234124332321224232222322334244422332133222221322231323335323534433623642333331512232233432 +1233323223333253234423353115222332342333122432232133232122232142334334332513246153353423132223343321 +5475443754463456443553623757544552542473236644533515443525753533335455813422441375454516465356431656 +3276322333332353334423643343456353344346332432253323323762333323733333633333636333353413233334554424 +2312222312481252235552224312342146252529326662211733375132422225462424664226716222242225435316534726 +5647566942478436876464969445631974775545947778858663954749446977584553756576839855679547564255237657 +4244444412141433344433323424434413423444244432524143445324343444345334443254333342231332441216232453 +5443446554346664654544444455354344534734466444644324435465424465432332532354454235344543435342453644 +6544455236356461335366354531255254443355242635645521555255146456113545536723155522252234543584534422 +2322222221124122252222222212222222223122222222123312222211223222222111222122222322532212122213222242 +2232111242252322522242224451439133426512215326244222221652425326222412463476122354223372154222136212 +4142224826172737614322257272692678732262146755524451413837432232457622532576238453234431772276232536 +6353216344333463332333331333133213334331233334426343334532233245434246423223333233133333135353342332 +1323453342343234233123233234233233321732131232321331546642233322323841323333363321334213334253335222 +4211441422524221222364232242631222222424242342122222222122432232236333112212242222234415223321323233 +5354448594454757396534735446545664463543225335479544554845536645625465645436435355897374475486736299 +9222214122721422222222322222222121361222112222113212221262232521232222222222324322212222221222212221 +2231324325523223321243232222322222321535133212225217222232223421523231332222233332221331243122513125 +2213344232553255233322233223912331634323331382432333635523233212315533331223233345245545382532522234 +3544382325444439444743245235339234451763361533432362433334454541344432443274354332315334343153134222 +2223112231142222222232334321222323321332122133222222322222231123322234222243523242221215432212422221 +3323223355224223414213333123225232123334323322324322233232232342334223322233311213333433323222344341 +4222824134232232215272455381232232241642243222622351243424551522212542728343252548243431374825372242 +1124561327327812339323348248942747979232333741438863422422578244842338233727234333652124317332763333 +3754136545825784277857794164587452752154428542332755567495876582575631741333322824247923954541645224 +6667376434644527447463454444544385956695544837336538345663335144564648444938467454754453565556944434 +2222224222125342222222221221242225231122222242122232134244322221132222225222221332213223232222323221 +3322222221223132332222122411212222242222323222222222133122322213132222132222222112223222222213321222 +4423433343231342442334436172444432243544544423342341424152122354142233444434443434434243443232345324 +1224421114333322123143425242342244342122343222242424543322221122342223122242224222132222144323324213 +3323243132222412233243423254525235222942213213522232234432142534222222232233333322515232123321534232 +1432336332222232231212221324322572261211227163222312323322333322227231411212221434232221216222212233 +4548547539233235233163624563334343333423435256334434438164454423614443425533444745362624433344466436 +2122361745311436322422452322523121392225236222325233666122222235612624123512267622722263512222341621 +3231382222262144422225631222422423411227244232322221252212222227127423127221424252432412222125343422 +8228352452134623333232525588523223344453343212358333835335525235563332551233333322434553235144354414 +4564548556564529545833357794374649862438898656788566553845753595444459158285554475574347475466465836 +2211322231122235322222323243322342223212833223118222224214322222224123212512212333224322211734252224 +5456566444656684465766457566665563459364736455675556643546474267565939667565456666594654425744364564 +5842526447335556634425344475565367653747675643375432345572334546287535373443366243444263525346442365 +2795775723662574538342222373931736324236744273722269664533333258373334445232731561722515426772737133 +5452235542452424555623315444943425225255441235434441514544445423612527322634642422245362474344424454 +4442427244422593232534324222433434226426532226322516322233512222222241323527223122427223324252322343 +6433434435332662333338355533422335435433532654233355554433346433333376633544332266242236332213556556 +1131554446233435321243244222565223533422322434522222438342212433431422313422343322455146235535345532 +3152557253372232123312222625252325252123127628253243324865273576325261225245325342832253327542535824 +2222223213222322211222221222222421222222222322122322212322222222212221222231242122432223222221421222 +1642635362633243533454633762353668621331657645336323235347274463338632954672463643336448424544725657 +2141223332283222321222242222232121422232223131222231242222223224221223223312225222123423323323414222 +4444694453436342782665244777846939475677434444576566333635133442244444165541433427765655444454753345 +2224424483456223814452233842667264452512273244422233332276353865643523343215233422372524243232523521 +1237824656322266422332233224222363624233552313533622435213371633323533353353631332324343242643333633 +1231211221222323222212232212222222222222221322232232222222232122212131232212222121222312222222212232 +3223232211223732341332333522132512551611222232211334442322333523224312123317165232212223272232224122 +2222222223112241342232212222222235622222122532222225222422232222252241322413229111222212212221222222 +2422332523222722227145223652362626152222254242141512321511223533257152222722321217434822242223223232 +5434344535342216453455553422511435545243253352331542332222529518348323354344447523455252253327424343 +7352469452742223126134342343334224336235442634344428422443425426333843544453444223244422444354651513 +1432344474232424532323342445839724873113364433535252243333343336148373423242153323213354228443341344 +5415223341143133142522553345331224312311115524312553434123151355154321522452213441233251252445246789 +7947595557484664238974656954344733365568666563778863215641447545559736732549337586374634544896346297 +3313521345213333233222323312322223121233342223222232351121232331232121343222425231132323432353222332 +5221223112221323121222226222222126213222222222222322222222351224222222222132423212214222231662222522 +3685347663449334343544694926774751934769593327363542531343516483616764739768951356625233374693264843 +5495273527547555222455233422555964573458591465559254322973547456495543478293423723258585364493445652 +2246552216532634222224412261623355452435275322712243422222321242435285524346245223234412356222222424 +3434452737333139322342333333535433773734333123236385334523433341344545533824337445336334433346227259 +2126625567366647366226546343376272475243533462343577547163212662766625668737141326477386353826582622 +3334144314213431143144443233314244243444444121123313134411432222111432144422311232112322424211356789 +5124222223223242422523262351112225322372362224224322122222241232221222222223132231222142231273222213 +4955745669535556665548572752574554536686455275333755347665545853723177525625429748546845574867359777 +1354425433245815164742457722954243764446232225714722294227534419347629545742224242535679243633535325 +3249925837323336773135677573656587863474853775343454598728683383636935568874487662556343385935836438 +4433334223533414235234123435226313433132152394544231323133333263363521613333382343329353347433334352 +2121322222122222221222222162223122222222111322222295224232322221222522225213222422322323222122222122 +2532444134433412344442452234233322421233132221143133333447213442246322233324514323422244241343242323 +1332333247132213224433332362332515425333633323333332743435534352235428245246224328312323335333325325 +3323433431451361446345442434352322544314415433472336444436442254355444424443351263623522334446322424 +3235242125423424222223122223122323134231212421232222252522124322222323412221224122234122224223232443 +4222222422242121224223322224232423222312221242242412224224144121122232132132212222221223223211242211 +9667559676865945685656555856576574889344955459959695568659898998679874657653558967857887855885685875 +2232522625535276163432652446663255546152454263734332245344452254535455255645332455554585665652347642 +3691424432344252982339232423433232852232451252433223224334423642322333215372313243222224433273132623 +1211264137133621365373376434721663626276414713254212344646451711141324366741132176514351671667725789 +4238352435453344364644467333433563553422633344323541347465433433245445563435224356453536333459463464 +3333332272343121323212333322265833544225232335523232352312263221373333333325233322331323234243332341 +4245422242334333223222335212332122432233212243234333233313212214313323233224333334213233335232212342 +3583533221848265253422226873272522192932233625622562212443525221623434764821682624544222263872226444 +4436728764735376733333367833562893341374269723293778542836662627931783323754325675623236253236817376 +2414344244245443334353423424444444344434444234334324344443342215513444326432334243314112444444526423 +4233311352329131234224523242323221565512453354225223252322242322152112434325423333524322332233235222 +2532164227224553572324443844315135565451545255724252535551654423257355332346254322144652325122535371 +3355561389336333561653712247712573443288844528736652262723526784373427226712282369245434857623845682 +3737322222163266422223162323442624312524422222233322632322422222222227272432222124432325236224657242 +5165455435455453241458445555388453557335554984225555255254555545459444454525459252354524534795523395 +4522153322221332233922335582246314235452223534324323343928223223122433152312122132423226232154221273 +1332222332333212133325223243212123341223332213123223321733323132313222222221112122135222223222522226 +3724154661335853639652222523253463324361245122335365213141433326223217122322322328525152876431244286 +5557231813565275555558458564753485855353554754446775455366355565665552485555258455568565553153853753 +2343238733112232956425323743363427223133321322231322442413232126333515532232323355322234414184332312 +1722545461555424552523334224166356244425225526332245535452354442512433843244424663345243532534528245 +3452563333332533333333315451322323333233533132233331573623234122332331323323733335464232264335343346 +3654434335253423464454333334321454434534344446363443454352524432441354573444454244355332442443434224 +6456368336466758556365756764559593646564665346467465456455445684516536645545562666766655555773655535 +2441263434432222222625224335242135241433242422324545247512551115221542265232222224334225232344243233 +2354415334324223431534325523232232234224544423338334245332552232333756322535522335233335255334245244 +3656447676687773455334475373892287865748944389514287778826924545596896362555922859295744487358589588 +4223223423333474313423253363222623373414249433433343242223233222143344314214342333333232632384833422 +3333343233323334331333362333333224331333243353323324333354337234331343553234335323333333333333232443 +4241125324231232212222485234122213232432231222324112124312522552423223242232212241323212412232325252 +2422222322232223423222122241412142244224417122242424443421342224211234424124122422323323222331483374 +4234333333334423452233333333322334232222332363333335333323333353243433322223335232333333333323463332 +3224222332524244222215232624124233362355348235226542632111522274427222511366424272275653262261522542 +6222344452372565123555352525335343534255542526225562443235526455535666554445451552646455323455263224 +3566135768261756336524563132444442442844443372334554823442482431146734472445426414444164444434462284 +6273544363263556455426442634266324447556555664764656665583866464645654563535564656435335526736457452 +2241522723635233224243422352455372373143423452324523151415354323222435235412124252334253241532236223 +4422334354442342222543442455333444434144544431284434234263345343774144324562433133943545634341125323 +4332232235322222213123322232233443321122333323213221132324221212322332243223321333122311224232233222 +1326242349524429461442127461433727627425152326343562366515353362343424725714327432374263445643352343 +1222212222319222312212222622124121412142257122221223212222226122222221221222322222222122262222132227 +4234245976744642245223524244555276224464325472234624645636422436336373544443116325552544572362647645 +2523411122223223223542425323223324222242351322224332212442542243433212122422442311323231512322242233 +3166566676554545563569644445333665446558666466633233565663656445992446656656165356646163453546545667 +8689464664844579384662579662255564357673536345666546645755459466354885353477669568665444295956736515 +7539785857849884837353988848869717756844487883754535135354663896777677647665445844285584573579787363 +4464257431365154543244324552235333343443466545324436354772454554674646552344225543545425425322544445 +4781753735344366434357735325246435464583386538335362264444635636343252337434426345525434647534444733 +2244622455224226523342254245117625662226564542333133417232454543321431325223624222232543223762237241 +4223316225421512144252513221341523163243235242322312433432322313252545424622425123243535532424422214 +6286263335534344533368253462322323435673433431163813565522531643177234656225265534256563324135332135 +3413423343333345344223332435343242421333222334315235223322241134432224333434232323334144234322332415 +3231323443325333433334332333233222343324223312334334343335332433343258335333333533333324233432323324 +4232222442231221123222232222221132254114312334244241322232412223142213222122324222211242451222424423 +7445444555357445234334235324424524344516231364354245547334444457366534757345454433544345255453585453 +5342211343221423332342523322331328243422322233431322245124123424122433344125242331723432424444134222 +2269125224122622367346642456643913624258715426767751334326644642521326341622756213212334345327362125 +3122533539325621812333823542376436391645683352666455572324423747353286353333245424637238552469573213 +3333323133336333343232433333333323233743342343133333212763233467423333333333234343334341324723373333 +3382121222211222221441224221193188222223131253242522921225532222328242523251341222274262222142211243 +3132132322234463434236445344243245642523142264256442622632433253333424463242564362362343345664434653 +2462213422584194441225422933243263223514223243332333132722117212272525534324413424411442331222222533 +2342122122121222322232222252322142232435253232223522224332227222433361632132322354242322272223426535 +2223221312312222323232121233212122122222232236222122322422422242222221122122142221211423223322222112 +2233512223822212432322422433122324272222223332312222121312223511122222222211131132222182223222222343 +2233336283933336652335355983357249278727433343523332743812473471443433336223323252153328532328362952 +1222112243123241222242822242223222121542233222412222342242124432312243222442222212432721223223428122 +7442343345544344443532345433443633444424434234433544333374238254423242234641444243243434245633432234 +2344423531415323365674416336433665352536544323265216115635434215452543525362572644564446435613414454 +3433325442264133212641423426873374324266366623443747633354644343537742546433533434361331644232373245 +2254244223533222222134542424764222122224225352234543633224422532474324232234242115363243422523342522 +7416828637345128812345271376757255835481783716186273472242481671666526273244614764157167775281326229 +3354342767259694535333657467696136333254238755374454643457722464133263263245736343156323392645235252 +2642723233212334231142223422322236225353227272242232234634422431251242222223344424434773223313335152 +4134551666245414412323214355453515546332543535212345233563611321456153622432323661446255536346344789 +6222225432225224211211283554321446133541551252521514622525123345222332227314643222222531314727342222 +2228323523722385226422122215142933721423895833477228153322384385264644834155241223426335286123341243 +3422133231513233223133241232232223624341123232311222233112223332223214231222212212222233222132223322 +3223215622213122211222122322222222224422212235212222231222222212213221211221212212213233213112221232 +3224243242233532434322424444332453444244235334423423232434543244442342344344434544244212242524313324 +2222122341322212222223223213312212813312522242222222322221335122224221422223123322333421222132422212 +7367756444653576685622297767274774455555367782771388566565879281285686272758528442226456684774675224 +4523313423434331232222332323333332233434443341341364333312342334313333322333323338342331332324334746 +3554349243312335222634334333535331612445436321852254235135223232534563145333625551653332233558232333 +5443562757664375145583357346556145435548265564455355755457426647844555666365484557355236465593454664 +1542231222222332222612212222212212242222222232223222212121222923221212227332232322212235213222222232 +3921463432434353253436547534244535363423223632566373333623354313214336432664122333345354385353344116 +4423347247473626465182435422322828235553313123584561363224322317354238346432531241327222238324343426 +5332242431232412223422312326222252221153323442323326223232425134442222344722421242121221612342222342 +3553143446543343374433358293446645542476147382494745533634333454245844444833345455433543927354459733 +7446677431564345156165326478334467656665667638634659522953556636337226265546526842137726966668686886 +5354532557536443799635474854765647777555648751766232338344583646963276518567333266434448768344673335 +8875576976428467897439359854855678576592675983997869996766458536766698779484389656968578698855669899 +2912222122232324222226221143122332121232412222213212232152223234122724222213463121614422224242225122 +4471333233454324354344414343347344336343414234434723334382347324133345333421644313447143644244644432 +3412422431212252523234424122432342211435224222422122222144512212242233223225122111342222242224323322 +3123422422222252222182322213232122222332442222222221221124232122282223221222222442322321212222212232 +5422326643542245242122222272632321542246221222226512322242121231425211342522222522452441532323546321 +1212363528222221232223212233222122472252213252521232252252321322225112112322232221322132322134322121 +5822123451221521232214126622552122242542123312224332327123243835255262543121533272241223434422422323 +3232334333674533549432674137262333537735635235252322317436533632523372236423733323632236875963255437 +6273332634772216436653553827352935462474334636639539125742664396451862379335616486554426223332348727 +2212222221222222222222112211222424221122122224232122122244131223231132222222222222222222222122221222 +2224212221323121443113221122522313322222221331631222422123222422224384221232223241124221323212126225 +3294385374644624373853643362443494344343364577434653152744239464433486851332349324534393836233474642 +3273235323312533322423353131333323241323271223323232333322213433112333322224233231122333423253522324 +1244423333322233223235235323234233334363333333233474453352225333423322234232332233323233333333233323 +4674556453665334326164532656563634435424443534343455344832623434633443634435415465344543435443442353 +3441224265212321235534442343443454441232123223413322542214334353343366543413424453234244223442334242 +1321222123222132222221225211222242221222212322222222222331223321525242223232322221222122312212333242 +3212222422222222211222222322132222222222312222421122212421322352221213221123122211321223723212233232 +2222231221512223222221122222221322222222224212122222222222222122222122232122122222222112234228222212 +5745127246656354258454752676611822326658657444666273665255336326476424623442564676773562325567776761 +2234345254543472364236535652232322243431542451326722423432353244325426322324362232239224754123321353 +5311474353345337334323253357343334153332313242123235333533247333223223444223272363533336621352323364 +2113321622353266323322337212258272559612637563223212222122323524122223231223433433223251542744472224 +2222414441224345244433322212544342444333132413533622441534143243232433242625254243433385231332255231 + diff --git a/day3/day3_test.txt b/day3/day3_test.txt new file mode 100644 index 0000000..7255fca --- /dev/null +++ b/day3/day3_test.txt @@ -0,0 +1,4 @@ +987654321111111 +811111111111119 +234234234234278 +818181911112111 diff --git a/day3/src/main.zig b/day3/src/main.zig new file mode 100644 index 0000000..b9ad3be --- /dev/null +++ b/day3/src/main.zig @@ -0,0 +1,59 @@ +const std = @import("std"); +const day3 = @import("day3"); + +// test "is invalid" { +// try std.testing.expect(isInvalid("11", 1) == true); +// } + +pub fn main() !void { + // Initiate allocator + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + + // Read contents from file "./filename" + const cwd = std.fs.cwd(); + const fileContents = try cwd.readFileAlloc(alloc, "day3.txt", 24000); + defer alloc.free(fileContents); + + // Print file contents + // std.debug.print("{s}", .{fileContents}); + var splits = std.mem.splitScalar(u8, fileContents, '\n'); + var joltage: u64 = 0; + while (splits.next()) |chunk| { + if (chunk.len < 1) { + continue; + } + var prev_largest: u8 = 0; + var prev_largest_second: u8 = 0; + var largest: u8 = 0; + var largest_second: u8 = 0; + + for (chunk) |v| { + if (largest != 0) { + largest_second = @max(largest_second, v); + } + if (v > largest) { + if (largest_second != 0) { + prev_largest = largest; + prev_largest_second = largest_second; + } + + largest = v; + largest_second = 0; + } + std.debug.print("largest:{c} largest_second{c} prev_largest{c} prev_largest_second{c}\n", .{ largest, largest_second, prev_largest, prev_largest_second }); + } + + if (largest_second == 0) { + largest = prev_largest; + largest_second = prev_largest_second; + } + + joltage += (largest - '0') * 10 + (largest_second - '0'); + std.debug.print("joltage running total {}\n\n", .{joltage}); + } + + std.debug.print("joltage: {}\n", .{joltage}); + try day3.bufferedPrint(); +} diff --git a/day3/src/root.zig b/day3/src/root.zig new file mode 100644 index 0000000..94c7cd0 --- /dev/null +++ b/day3/src/root.zig @@ -0,0 +1,23 @@ +//! By convention, root.zig is the root source file when making a library. +const std = @import("std"); + +pub fn bufferedPrint() !void { + // Stdout is for the actual output of your application, for example if you + // are implementing gzip, then only the compressed bytes should be sent to + // stdout, not any debugging messages. + var stdout_buffer: [1024]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); + const stdout = &stdout_writer.interface; + + try stdout.print("Run `zig build test` to run the tests.\n", .{}); + + try stdout.flush(); // Don't forget to flush! +} + +pub fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try std.testing.expect(add(3, 7) == 10); +}