From 61d7d0f48689b30e60ccee972d1382ed5539dc8d Mon Sep 17 00:00:00 2001 From: Moritz Gmeiner Date: Sun, 1 Dec 2024 18:12:35 +0100 Subject: [PATCH] day 1 done --- Day01/build.zig | 91 ++++ Day01/build.zig.zon | 72 ++++ Day01/input1.txt | 1000 +++++++++++++++++++++++++++++++++++++++++++ Day01/src/main.zig | 157 +++++++ Day01/src/root.zig | 10 + 5 files changed, 1330 insertions(+) create mode 100644 Day01/build.zig create mode 100644 Day01/build.zig.zon create mode 100644 Day01/input1.txt create mode 100644 Day01/src/main.zig create mode 100644 Day01/src/root.zig diff --git a/Day01/build.zig b/Day01/build.zig new file mode 100644 index 0000000..fafca6f --- /dev/null +++ b/Day01/build.zig @@ -0,0 +1,91 @@ +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 lib = b.addStaticLibrary(.{ + .name = "Day01", + // 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 = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + + // This declares intent for the library to be installed into the standard + // location when the user invokes the "install" step (the default step when + // running `zig build`). + b.installArtifact(lib); + + const exe = b.addExecutable(.{ + .name = "Day01", + .root_source_file = b.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 lib_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/root.zig"), + .target = target, + .optimize = optimize, + }); + + const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); + + const exe_unit_tests = b.addTest(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + }); + + const run_exe_unit_tests = b.addRunArtifact(exe_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_lib_unit_tests.step); + test_step.dependOn(&run_exe_unit_tests.step); +} diff --git a/Day01/build.zig.zon b/Day01/build.zig.zon new file mode 100644 index 0000000..73c3e90 --- /dev/null +++ b/Day01/build.zig.zon @@ -0,0 +1,72 @@ +.{ + // 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 = "Day01", + + // 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", + + // This field is optional. + // This is currently advisory only; Zig does not yet do anything + // with this value. + //.minimum_zig_version = "0.11.0", + + // 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. + // .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/Day01/input1.txt b/Day01/input1.txt new file mode 100644 index 0000000..e68b7bb --- /dev/null +++ b/Day01/input1.txt @@ -0,0 +1,1000 @@ +60236 87497 +27507 18604 +69810 73952 +60448 56269 +92139 61722 +26802 60858 +77131 85480 +11665 30619 +15622 29287 +50472 55227 +31601 15873 +49999 20227 +87160 41386 +58198 25660 +73480 83349 +34374 31670 +56594 81327 +86225 89142 +41024 80952 +53973 84059 +21336 23710 +68229 32979 +31907 49347 +96121 92108 +91843 55227 +64469 46797 +24945 51234 +38171 20077 +85337 65084 +53614 29287 +33205 92108 +92278 65084 +12348 54841 +74217 64534 +31880 67672 +40671 61530 +97622 51354 +83858 54841 +54900 29287 +75694 32657 +12808 92108 +22110 45441 +24842 80522 +76019 85395 +29801 55227 +62119 24086 +84085 42315 +97633 50312 +55735 84085 +37423 24578 +21883 38297 +24725 85063 +94882 96754 +78917 23949 +38963 37984 +58484 54841 +25045 60224 +44549 68472 +96565 28888 +96544 13183 +56476 62216 +93786 36255 +47385 31017 +53003 74502 +34763 90916 +30965 15026 +33970 18604 +59311 87287 +46656 23655 +90690 94389 +90916 85428 +39888 99400 +80325 15572 +66759 90916 +69011 61530 +42661 76190 +62994 36115 +83076 85495 +22038 90916 +28058 16827 +43772 87497 +97469 84059 +73587 46618 +17373 28474 +67819 22858 +83212 71090 +31111 21853 +50655 80499 +46790 54841 +65893 85337 +32657 30300 +39263 63606 +34654 37756 +70982 97980 +47054 90916 +36308 85428 +78055 25699 +82921 57074 +22806 38778 +52324 18604 +69119 87497 +93821 85337 +72610 45392 +81035 32462 +83645 61530 +40083 12785 +65512 39639 +94178 32657 +94616 23243 +48110 85170 +48871 31323 +19056 85337 +41244 32657 +10055 57273 +11393 29287 +41282 89929 +44215 69138 +65273 49214 +51527 88002 +37756 20404 +62112 28474 +20891 84085 +77160 89946 +66205 46881 +48418 77312 +75464 55227 +61530 59969 +21267 29270 +37370 54841 +88644 90916 +78623 63606 +21706 46881 +44144 28524 +92794 62981 +73104 55227 +41050 17824 +18975 74502 +24323 55737 +65877 13086 +26231 46430 +50875 22462 +28235 13325 +19413 52280 +12895 20227 +99917 14861 +12606 85116 +17157 55498 +65577 49347 +83633 75113 +16385 85022 +13523 14114 +56269 63606 +21797 31017 +15276 42139 +60621 92519 +47097 50088 +93922 70323 +47866 84085 +12491 65028 +97496 99400 +50622 92695 +80936 57661 +90350 61530 +24297 31017 +17654 79327 +25402 46881 +22077 18604 +40903 54120 +50315 84991 +84107 90341 +82297 69059 +31420 92108 +32948 13185 +38573 48477 +41469 92536 +47687 67672 +75858 43056 +87492 55227 +43146 63726 +50630 30114 +69070 31922 +99400 80499 +95819 84085 +28688 85428 +34161 47281 +25281 43111 +72346 85337 +43428 65084 +35269 52541 +69977 15275 +21657 15308 +93149 28977 +26372 22264 +53919 26147 +83653 17007 +52437 55338 +58577 81095 +10669 78580 +48716 61530 +44555 54841 +26276 85428 +44928 27597 +78761 83134 +29197 63606 +10381 16778 +19398 74893 +88620 52809 +30433 65084 +80557 31629 +31479 12679 +50801 83534 +65484 55227 +12778 94213 +22907 98741 +27492 21018 +24335 55895 +92027 63071 +42748 38490 +39736 28474 +29251 15930 +18312 38297 +92108 36420 +56841 23667 +24134 80499 +30573 37975 +35062 82320 +10140 77160 +20132 75245 +34245 41016 +39368 39229 +83603 98755 +98047 98888 +96938 15597 +51784 58196 +70081 78652 +46881 49347 +24066 76805 +23023 84959 +89425 67672 +93763 73425 +86478 87497 +12302 87140 +73930 39280 +25718 63606 +90416 18604 +35786 14027 +93942 65398 +65754 86637 +11744 49347 +49585 93684 +15897 67695 +10695 39893 +96173 28474 +76947 29182 +48784 84085 +91121 20227 +49311 23670 +85372 15774 +16681 89841 +45260 90848 +35560 95034 +83401 21784 +12219 96734 +71067 55227 +99026 80166 +69983 15346 +83934 80058 +64979 12968 +41092 63606 +12308 40087 +75169 96117 +15784 77160 +38824 41537 +40870 85116 +19711 99400 +70507 14564 +94676 54768 +14777 80499 +40418 75245 +64438 83035 +57843 35948 +59713 80499 +34140 70427 +11775 32188 +34406 40942 +56896 74472 +86220 28474 +70476 71102 +96477 98192 +18922 51619 +59519 71811 +10789 47031 +86572 92108 +61550 75245 +84059 23351 +96041 23599 +10215 18375 +45040 17072 +58869 16995 +88390 93777 +91416 55497 +17847 55636 +76901 85287 +18097 38297 +96115 87497 +18055 61095 +45533 87184 +63882 29287 +73200 53074 +22109 89509 +83241 80499 +93489 64603 +56777 31017 +99138 62596 +58373 30342 +99202 25402 +58133 60035 +80500 90916 +73469 17731 +35801 99400 +91384 52122 +27573 24457 +28721 28503 +88538 26807 +92971 14100 +48598 63606 +61286 29287 +91745 56269 +34448 25850 +79106 49347 +34898 19376 +40111 67477 +93372 30265 +63851 32657 +59546 48474 +42335 84059 +36277 54841 +87127 99224 +81168 23408 +92976 92195 +98032 25026 +48696 29287 +20385 32499 +74076 65084 +92372 29299 +17059 74502 +42532 30262 +38449 11418 +43881 32657 +54403 63392 +94721 84085 +95152 51611 +51782 62179 +53025 55227 +36643 49347 +87706 65084 +12976 55227 +44534 26147 +30917 87497 +74502 61774 +67952 95476 +11806 29287 +90030 79066 +68902 98976 +68040 90916 +98301 41380 +36051 49347 +16026 69194 +67357 46509 +67306 73846 +82448 20227 +23641 85428 +84634 31017 +69185 43828 +46097 72892 +17794 26147 +43848 49347 +96312 56999 +56019 92546 +56919 86411 +40237 51598 +55120 87497 +92755 87497 +34906 32657 +54889 75245 +41064 62527 +65738 99349 +64355 75245 +52805 80499 +85428 46881 +37626 83414 +53792 34959 +54369 72387 +38094 68402 +99008 33894 +24058 29287 +18736 51209 +25865 80139 +63486 65084 +62108 92108 +65689 14359 +42333 10289 +69333 38259 +18491 62168 +47310 78249 +54570 84023 +20551 78951 +59475 12064 +50311 56215 +65474 70037 +85663 67672 +69259 33147 +83117 61530 +36840 85337 +34169 47016 +88946 97794 +18112 58674 +46551 38349 +82216 72623 +28051 67042 +92921 53931 +49329 21408 +72000 82465 +16291 64726 +89421 28474 +79147 20446 +10763 18604 +44616 79180 +42112 59021 +16110 11993 +95856 75245 +68250 47047 +73900 18604 +83070 70285 +45039 59298 +34384 91759 +69096 40774 +17871 54746 +12711 13323 +61219 85337 +62596 53507 +65641 61530 +44822 29849 +27694 41410 +18316 40475 +88264 27106 +24559 73846 +51165 95419 +41966 80890 +31980 27305 +18954 73225 +48469 99400 +21431 77160 +61521 58997 +91768 50704 +64691 54596 +26116 67672 +38956 90916 +26542 38297 +55188 23379 +59315 70559 +29191 25198 +66379 38297 +25324 66593 +40836 92108 +61397 12807 +33898 61530 +45415 28400 +33802 90916 +62390 21461 +59153 29287 +36034 30152 +98520 22569 +22195 88748 +33327 60661 +45322 26147 +96822 16363 +16125 34893 +87248 22080 +85116 74502 +46038 18604 +37571 85337 +58238 69159 +41500 16532 +83727 38297 +68575 54551 +31681 49347 +38631 22657 +23561 58198 +76931 45582 +28565 80499 +75538 41731 +53316 85116 +63565 16082 +43704 94497 +70404 44010 +30526 10924 +68736 25749 +66570 15277 +78906 63311 +97695 18604 +83519 65084 +41333 17910 +65117 58198 +83816 13162 +82827 99400 +38854 61353 +10453 97813 +39642 62478 +12432 75245 +73646 64603 +69031 52168 +87888 61523 +66253 60542 +50475 48019 +48201 29287 +76179 83869 +69129 16884 +25515 12228 +19375 84085 +66375 55218 +27327 63518 +58580 23667 +21807 37166 +40268 24090 +97745 23667 +32093 80017 +32735 15898 +38297 72737 +66743 99400 +83349 91011 +58316 49347 +72291 61430 +86837 75245 +73810 18232 +84307 18570 +61747 85337 +31550 89967 +47416 65879 +25093 48062 +65307 65580 +84094 89485 +52515 23667 +49044 38003 +78523 89584 +86184 32463 +35308 81918 +27716 92108 +97833 99584 +55235 33021 +92138 55227 +18414 92108 +66833 28474 +29162 83349 +46906 95696 +91988 73664 +56574 18604 +84481 40052 +92467 20808 +68595 18734 +54857 63112 +46447 89520 +38937 31017 +91755 49347 +69591 73958 +71215 56038 +37212 54841 +68160 93928 +50127 58436 +58834 79630 +64554 56269 +53994 58198 +71503 61530 +41132 89321 +73137 39227 +90473 36281 +34280 10905 +32327 73846 +56962 85428 +93849 17923 +91613 43897 +41600 82433 +68548 40140 +57540 69227 +27024 72558 +84038 29287 +68797 90123 +34585 80998 +17224 44725 +67565 65979 +53439 66006 +10746 34397 +60407 96273 +41402 44313 +74625 94199 +48717 97274 +43053 61530 +60079 92108 +51815 18591 +78297 94936 +75363 62686 +34845 85337 +52300 45188 +18321 54841 +30997 13211 +12201 27495 +57013 81991 +32067 85116 +98986 63998 +56366 80499 +88571 19705 +54841 23231 +21485 36499 +79031 67538 +92188 61530 +39376 16112 +49347 23042 +98452 63606 +52132 12521 +22842 28546 +35826 18604 +62218 99676 +14172 32657 +18364 55674 +49002 62596 +51392 45821 +66873 17929 +67156 74502 +28175 32657 +90671 77571 +84511 54841 +18862 18604 +76865 55383 +17699 69478 +79215 56269 +85442 92108 +10959 45021 +48776 77160 +14892 92936 +41140 55337 +84764 80499 +40023 75245 +67389 46881 +45778 36995 +21318 55227 +59744 75245 +67409 38297 +18164 29287 +61584 96614 +41358 38413 +61352 60463 +70015 35917 +39628 81795 +21915 18604 +16171 90414 +15423 55227 +36716 80499 +55056 90916 +63466 15943 +98203 73846 +99741 99459 +85505 38297 +25032 86060 +66980 54841 +98311 31229 +25350 69484 +83835 14362 +46839 78034 +18604 13604 +73846 50472 +18269 56269 +91727 56269 +68873 59307 +15175 87497 +77802 28474 +28913 82745 +22215 60110 +70059 61531 +36612 33386 +67672 88985 +78181 61530 +39317 18558 +77810 35422 +82429 21444 +76561 23667 +58649 38653 +25363 12807 +61473 87684 +36559 71640 +88918 85428 +74396 16237 +33853 55227 +54850 67672 +39779 28474 +64336 74608 +80980 29287 +45483 42757 +73198 85576 +63606 90916 +52630 16429 +35416 12807 +95544 99400 +44801 64603 +80281 80499 +52182 38598 +23258 51480 +42093 51758 +74897 94719 +98978 70168 +42208 87497 +32037 97893 +65966 61530 +77038 29502 +56448 77741 +83791 74727 +25720 31017 +24543 85306 +83743 51386 +83607 36001 +27371 85337 +96010 42013 +29241 74502 +96301 54841 +60646 92108 +56101 38297 +82779 81249 +16670 32639 +24164 52565 +28474 73988 +41717 29287 +55403 32986 +75739 96626 +55398 85116 +55227 55331 +46733 74502 +50659 81687 +25155 98435 +90938 67672 +90142 98291 +29740 71006 +95852 54841 +26912 47695 +41728 65084 +76264 55227 +81182 67672 +88932 55227 +15524 31186 +75333 51809 +98264 37756 +81912 95741 +62285 60469 +47664 93362 +60019 33153 +54501 98948 +46192 20511 +92628 85116 +58318 82669 +44457 87772 +32136 77838 +74012 58198 +20227 35179 +53716 67847 +53411 73846 +68876 49541 +70125 49347 +75562 11521 +72306 47501 +88484 92108 +70619 38297 +86166 36298 +18487 80499 +92524 87204 +52742 67742 +58671 24114 +90475 27557 +19915 80499 +76602 66482 +28497 44495 +41756 84884 +73642 48266 +11516 75245 +97800 63544 +89184 31017 +41929 71798 +61689 46881 +29287 41446 +39444 47202 +35248 23667 +94851 74119 +86784 88863 +74864 73704 +23002 61530 +75245 36530 +83133 55227 +95285 57184 +85832 28474 +95588 74502 +83915 11024 +71644 96251 +71313 18604 +73235 87497 +33469 49347 +21867 38297 +95267 57786 +89935 28171 +21251 12807 +25478 40283 +99474 11515 +80499 66656 +14837 84184 +64729 40572 +40809 32336 +33236 85592 +20441 71171 +64603 66164 +55291 76339 +28881 12197 +58350 51927 +96845 74322 +97597 82513 +62008 54841 +45790 16819 +77813 80499 +81381 17092 +65519 80499 +26031 96119 +16212 90916 +95140 18604 +10312 92108 +61438 19227 +95084 10260 +99043 72732 +89491 54841 +90269 76128 +48016 44722 +20288 67010 +41063 71020 +74315 22618 +99142 76172 +26666 18604 +45041 76748 +47713 89106 +80657 38823 +81404 85337 +47976 85029 +44477 92418 +55367 96148 +63834 14134 +23382 48292 +77597 33494 +55880 93836 +18367 49347 +82262 76051 +95008 56543 +22783 77168 +12807 87497 +28138 55227 +70523 80353 +74070 90870 +76864 65482 +69003 13575 +24843 29287 +69768 20648 +87497 67672 +18271 94218 +45836 28474 +97769 28474 +80076 65084 +72050 46881 +79328 59489 +37514 80499 +42555 32578 +20199 70816 +49460 73487 +90204 93607 +56298 25196 +99974 70377 +56276 28721 +89189 77206 +60768 80376 +21319 63606 +31997 50398 +32416 90916 +54694 22346 +35088 18897 +51752 26147 +55308 95249 +34420 18604 +23667 33821 +82510 99101 +60857 80499 +36607 87497 +49184 18604 +24850 69833 +74238 29287 +57022 18604 +19800 65084 +75401 99400 +69619 16727 +76302 81630 +26599 78516 +91798 49272 +44256 29287 +26147 51140 +49429 29968 +73408 67672 +22299 47662 +95945 46881 +64434 12499 +25710 19594 +33442 63606 +93780 22452 +79459 23892 +82288 56995 +72573 49347 +48719 58670 +32164 38484 +86403 10414 +38984 39863 +86164 79095 +92712 22911 +89181 49347 +87082 40962 +90401 93895 +52697 15438 +34642 38297 +64004 58198 +42756 66637 +89884 63606 +72848 84059 +81971 22307 +61080 37101 +52293 92108 +10451 93989 +85724 84085 +40006 98335 +34725 36933 +85585 99400 +31017 57385 +18673 92108 +63900 58933 +10042 23667 +89735 55825 +11855 90031 +23358 92108 +74765 61530 +18606 54841 +29355 37589 +48513 60953 +53374 55970 +71218 28711 +39215 18604 +35955 94504 +60316 77721 +82523 96106 +29011 56269 +82545 77160 +69538 64794 +21067 28474 +31600 29287 +48638 79513 +77490 38714 +27808 31017 +51472 96525 +59950 83349 +94201 58024 +74719 32598 +44779 24776 +79697 57387 +53766 37236 +79207 49094 +21981 85337 +70180 38991 +92776 26555 +62229 29287 +22837 90916 +47999 85425 +65084 67672 +24224 63606 +45488 92101 +93347 80499 +57265 69150 +35677 90916 +12235 90916 +23634 17105 +69570 77160 +10364 99400 +64953 58607 +94336 72861 +35805 46881 +54374 49061 +56695 28474 +67989 96030 +66652 99400 +98664 85337 +94703 85337 +34924 46881 +89844 36377 +43375 90916 +62268 92108 diff --git a/Day01/src/main.zig b/Day01/src/main.zig new file mode 100644 index 0000000..46fbdfc --- /dev/null +++ b/Day01/src/main.zig @@ -0,0 +1,157 @@ +const std = @import("std"); + +pub fn main() !void { + // // Prints to stderr (it's a shortcut based on `std.io.getStdErr()`) + // std.debug.print("All your {s} are belong to us.\n", .{"codebase"}); + + // // 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. + // const stdout_file = std.io.getStdOut().writer(); + // var bw = std.io.bufferedWriter(stdout_file); + // const stdout = bw.writer(); + + // try stdout.print("Run `zig build test` to run the tests.\n", .{}); + + // try bw.flush(); // don't forget to flush! + + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const alloc = gpa.allocator(); + + try part1(alloc); + try part2(alloc); +} + +fn range(comptime n: u32) [n]usize { + var array: [n]usize = undefined; + + for (0.., &array) |i, *elem| { + elem.* = i; + } + + return array; +} + +fn argsort_cmp(comptime T: type, comptime lessThanFn: fn (ctx: void, lhs: T, rhs: T) bool) (fn ([]T, usize, usize) bool) { + return struct { + fn cmp(array: []T, left_idx: usize, right_idx: usize) bool { + return lessThanFn({}, array[left_idx], array[right_idx]); + } + }.cmp; +} + +fn part1(alloc: std.mem.Allocator) !void { + const filename = "input1.txt"; + + const num_lines = 1_000; + + const file = try std.fs.cwd().openFile(filename, .{}); + defer file.close(); + + var left_list: [num_lines]u32 = undefined; + var right_list: [num_lines]u32 = undefined; + + const file_reader = file.reader(); + var buf_reader = std.io.BufferedReader(4096, @TypeOf(file_reader)){ .unbuffered_reader = file_reader }; + + const reader = std.io.Reader(@TypeOf(&buf_reader), std.fs.File.Reader.Error, @TypeOf(buf_reader).read){ .context = &buf_reader }; + + var line_buf = std.ArrayList(u8).init(alloc); + defer line_buf.deinit(); + + for (0..(num_lines + 1)) |i| { + reader.streamUntilDelimiter(line_buf.writer(), '\n', 4096) catch |err| switch (err) { + error.EndOfStream => break, + else => return err, + }; + + const left = try std.fmt.parseUnsigned(u32, line_buf.items[0..5], 10); + const right = try std.fmt.parseUnsigned(u32, line_buf.items[8..13], 10); + + left_list[i] = left; + right_list[i] = right; + + try line_buf.resize(0); + } + + var left_idxs = range(num_lines); + var right_idxs = range(num_lines); + + std.debug.assert(left_list.len == left_idxs.len); + std.debug.assert(right_list.len == right_idxs.len); + + for (0..1000) |i| { + std.debug.assert(left_idxs[i] == i); + std.debug.assert(right_idxs[i] == i); + } + + const cmp = argsort_cmp(u32, std.sort.asc(u32)); + + std.mem.sort(usize, &left_idxs, @as([]u32, &left_list), cmp); + std.mem.sort(usize, &right_idxs, @as([]u32, &right_list), cmp); + + for (0..(num_lines - 1)) |i| { + std.debug.assert(left_list[left_idxs[i]] <= left_list[left_idxs[i + 1]]); + std.debug.assert(right_list[right_idxs[i]] <= right_list[right_idxs[i + 1]]); + } + + var sum: u64 = 0; + + for (left_idxs, right_idxs) |left_idx, right_idx| { + sum += @abs(@as(i64, left_list[left_idx]) - @as(i64, right_list[right_idx])); + } + + std.debug.print("Day 1, part 1: {}\n", .{sum}); +} + +fn part2(alloc: std.mem.Allocator) !void { + const filename = "input1.txt"; + + const num_lines = 1_000; + + const file = try std.fs.cwd().openFile(filename, .{}); + defer file.close(); + + var left_list: [num_lines]u32 = undefined; + + var right_map = std.AutoHashMap(u32, u32).init(alloc); + defer right_map.deinit(); + + const file_reader = file.reader(); + var buf_reader = std.io.BufferedReader(4096, @TypeOf(file_reader)){ .unbuffered_reader = file_reader }; + + const reader = std.io.Reader(@TypeOf(&buf_reader), std.fs.File.Reader.Error, @TypeOf(buf_reader).read){ .context = &buf_reader }; + + var line_buf = std.ArrayList(u8).init(alloc); + defer line_buf.deinit(); + + for (0..(num_lines + 1)) |i| { + reader.streamUntilDelimiter(line_buf.writer(), '\n', 4096) catch |err| switch (err) { + error.EndOfStream => break, + else => return err, + }; + + const left = try std.fmt.parseUnsigned(u32, line_buf.items[0..5], 10); + const right = try std.fmt.parseUnsigned(u32, line_buf.items[8..13], 10); + + left_list[i] = left; + + if (right_map.get(right)) |count| { + try right_map.put(right, count + 1); + } else { + try right_map.put(right, 1); + } + + try line_buf.resize(0); + } + + var sum: u64 = 0; + + for (left_list) |x| { + if (right_map.get(x)) |count| { + sum += count * x; + } + } + + std.debug.print("Day 1, part 2: {}\n", .{sum}); +} diff --git a/Day01/src/root.zig b/Day01/src/root.zig new file mode 100644 index 0000000..ecfeade --- /dev/null +++ b/Day01/src/root.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const testing = std.testing; + +export fn add(a: i32, b: i32) i32 { + return a + b; +} + +test "basic add functionality" { + try testing.expect(add(3, 7) == 10); +}