From 7c4d518400682910090c492455d6b2a9280e55ba Mon Sep 17 00:00:00 2001 From: Inga Date: Sun, 24 Dec 2023 16:42:01 +0000 Subject: [PATCH] day 24, part 1 --- day24-easy/build.zig | 70 ++++++++++ day24-easy/easy.in | 301 ++++++++++++++++++++++++++++++++++++++++ day24-easy/sample.in | 6 + day24-easy/src/main.zig | 274 ++++++++++++++++++++++++++++++++++++ 4 files changed, 651 insertions(+) create mode 100644 day24-easy/build.zig create mode 100644 day24-easy/easy.in create mode 100644 day24-easy/sample.in create mode 100644 day24-easy/src/main.zig diff --git a/day24-easy/build.zig b/day24-easy/build.zig new file mode 100644 index 0000000..9e5ab34 --- /dev/null +++ b/day24-easy/build.zig @@ -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 = "day24-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); +} diff --git a/day24-easy/easy.in b/day24-easy/easy.in new file mode 100644 index 0000000..50cf2ba --- /dev/null +++ b/day24-easy/easy.in @@ -0,0 +1,301 @@ +200000000000000 400000000000000 200000000000000 400000000000000 +119566840879742, 430566433235378, 268387686114969 @ 18, -130, 74 +433973471892198, 260061119249300, 263051300077633 @ -16, -170, -118 +44446443386018, 281342848485672, 166638492241385 @ 197, 16, 200 +102165762267068, 293235409083300, 334966976680379 @ 19, 15, 9 +309136088713586, 389177164114113, 550425584489999 @ -190, -76, -196 +200646738999134, 287902166440707, 307735317823961 @ -18, 10, 22 +20220675054975, 341076721722584, 312699491042075 @ 121, -37, 28 +185069726140701, 208617741620427, 322061937615924 @ 95, 120, -21 +272847962518124, 292003940163156, 323928914434373 @ -53, -9, -20 +74655578395201, 217832940396064, 393411530178059 @ 65, 91, -56 +346881253177456, 180432238623373, 274168028477635 @ 40, 284, 8 +394072216109686, 265110428733744, 286173083651949 @ 134, -93, -190 +382811183259174, 275147350759333, 281759174503969 @ 22, -86, -66 +379673844120790, 264139003916364, 347941997621597 @ 134, -57, -518 +271190259814106, 281444237877888, 284779832411765 @ 25, -8, 25 +244965431440366, 412668386906164, 350596834013769 @ 77, -263, -103 +304488581083505, 431119357060255, 385455243164373 @ -123, -194, -94 +256354668197090, 267402500413377, 356815597076273 @ -6, 26, -82 +369205658987606, 256386215015602, 276645518949721 @ 30, 14, -22 +108087436432664, 332682709751646, 352566450139529 @ 72, -39, -26 +335105850184286, 464048556041508, 98258921420113 @ -75, -405, 416 +325909902735036, 362796309859509, 140359509341884 @ -52, -192, 329 +253626200182004, 247728843793474, 137686136674585 @ 9, 58, 282 +440351282328917, 247271799691112, 221109121051146 @ -171, 35, 407 +266572372410705, 354899126343944, 413548426258060 @ -91, -77, -115 +192228734570342, 345491793035910, 261626901012513 @ 6, -66, 78 +225929477960677, 241842668991392, 490941185831172 @ -17, 67, -237 +192654532791044, 91841497941588, 203153310797195 @ 177, 359, 183 +389256835309354, 407643696775532, 361895935384073 @ -257, -119, -32 +416512955881909, 248457945466542, 265840652725119 @ 103, 21, -96 +359937652141979, 248154474182481, 278858339023475 @ 33, 50, -20 +255548733693729, 443803456899881, 377867455881882 @ 89, -356, -177 +381376879971722, 227081119973884, 311026563750909 @ 84, 163, -256 +432770437503764, 221819413882155, 289883661066284 @ -156, 246, -231 +291737243217343, 241779781669767, 303859237058954 @ 161, 72, -71 +437533082927394, 328700702557418, 236188807558157 @ -164, -754, 220 +400977651018206, 352930318467552, 271208224991129 @ -160, -280, 24 +442253143579350, 273465624571232, 246227971867506 @ -122, -421, 143 +413813398262822, 190072796516682, 285496963430258 @ 86, 621, -277 +233063656145963, 250488941778974, 379071318147132 @ 57, 53, -133 +309633492430817, 317501530392424, 270954647097791 @ -51, -77, 52 +351663654752272, 186308138054931, 264122523811079 @ -124, 180, 64 +239442014129771, 137613987397417, 323379874883257 @ -75, 190, 6 +230796533010198, 294564425304696, 274340842705941 @ 34, -18, 53 +317946207060476, 292654290983988, 45343089073811 @ 195, -121, 877 +324866361067533, 233065837183778, 268746624014522 @ 47, 97, 37 +154373229342330, 364699611911802, 240836256464787 @ 68, -96, 105 +339593883678022, 333576752873542, 353346484739247 @ -106, -110, -109 +399730816092937, 396210915286001, 351497547881999 @ -121, -514, -296 +94124785494716, 476172897408204, 281174751526601 @ 75, -197, 58 +412785343818014, 339369413667948, 309269889114321 @ -216, -195, -70 +235568209020026, 380107428057574, 323263196564489 @ 60, -178, -38 +385953427998158, 375704757891196, 299270487965353 @ -189, -206, -9 +355467167006834, 264643318092603, 227348971071569 @ 34, -10, 174 +404489456633638, 306074449612356, 263467597956705 @ 23, -369, -7 +180353281586522, 309198040260156, 263277511167737 @ 19, -19, 76 +342533432678504, 280362061821549, 447549592683380 @ 32, -52, -547 +349580529443783, 295361333909073, 282186634905239 @ 83, -136, -36 +37451954275424, 424278317011107, 459776643813962 @ 72, -110, -109 +336747067735987, 312511064713738, 386534477570465 @ -83, -79, -194 +318022951439204, 482563563785184, 399286556828171 @ -129, -287, -127 +395535668805766, 380774230441668, 312947573957569 @ -20, -651, -241 +311506831010714, 369701087580851, 181740622082499 @ -54, -178, 224 +185384576343390, 110163414853616, 285681032321197 @ -28, 214, 53 +301789626461842, 261975550170755, 317137026374831 @ 34, 22, -68 +366825111187626, 284711776540284, 289988479761059 @ -40, -69, -41 +251521728002326, 229534891057074, 349143463985668 @ 70, 93, -103 +50696647471819, 184956498370923, 195970684569977 @ 217, 144, 166 +401798645055950, 235766735524329, 338114637087564 @ -66, 108, -351 +259137820407881, 377947255148589, 331579905063874 @ 9, -168, -49 +325908997085078, 240651807109140, 514716405174593 @ -111, 70, -358 +309745445617946, 488132941765269, 458817589645994 @ -158, -226, -155 +404354180831270, 208231581845316, 268827328185923 @ -154, 191, 25 +8831802251766, 415975478183170, 193414185664639 @ 110, -106, 149 +146184401524130, 197088992520388, 259562718467205 @ 84, 127, 80 +361335313023945, 272644297380301, 302008513411324 @ -233, 34, 39 +267228606403592, 7940619522762, 104125398448025 @ 223, 762, 522 +386723414190681, 69130176102329, 155341578926689 @ -29, 842, 510 +313498365855596, 412624559985012, 255645432841295 @ -135, -169, 85 +355472080913456, 264478353995004, 258990402047069 @ 32, -9, 58 +267489911446850, 50030718954360, 391047695580651 @ -36, 369, -129 +443176091365346, 286986567582870, 310754569085021 @ -240, -270, -392 +201556804344488, 336775931596716, 353835937864073 @ -11, -53, -38 +353298112769358, 263368965030972, 351198068480014 @ 62, -9, -301 +369326634002876, 211342603812690, 297902776764488 @ -80, 161, -49 +196585676695373, 377190465592782, 476692653244482 @ 9, -111, -205 +263095050169776, 338495268845829, 473885151570604 @ -140, -30, -129 +355502511286642, 176944591757910, 362167337200099 @ -124, 204, -141 +353764851084204, 344754095502324, 499106834500409 @ -209, -57, -206 +207903011958043, 227786538588871, 121297200838017 @ -83, 80, 222 +400322666930381, 239076690018234, 273251280825029 @ 147, 113, -118 +391078158073877, 117528134183047, 433332188664325 @ -39, 646, -750 +376138874869286, 232834105114475, 121001080625137 @ 150, 136, 878 +192558251296636, 360271530822300, 70526024965075 @ 160, -153, 428 +410974412236871, 249818677399329, 261861879293789 @ -97, 36, 26 +172533506155178, 342988047261648, 328433122922657 @ -17, -46, 6 +156389690101806, 298856487897756, 296290475717841 @ 100, -15, 26 +371186783945750, 242335671196308, 382736522465489 @ -124, 69, -236 +381247284722207, 249737024226648, 375510625719144 @ -153, 51, -211 +89712214395264, 158323068151594, 246403893012623 @ 71, 158, 97 +135741866358765, 212596424961682, 344692672339662 @ 7, 97, -7 +377236802106981, 465279501413555, 345362990332186 @ -35, -778, -273 +317392078318254, 151061840760574, 263984669747357 @ 58, 328, 52 +423993882670526, 246333711743284, 264920659351984 @ -18, 47, -67 +154397864483252, 196493134418052, 234551562304991 @ 267, 159, 123 +171893378980836, 226953722759994, 236802876450239 @ 289, 102, 121 +406485535029040, 256991369533148, 270208351972743 @ -31, -13, -37 +325146699921030, 316997334752984, 278604500576813 @ -106, -62, 43 +352136542238726, 299191820056884, 476980017745209 @ -138, -39, -333 +260879887039622, 28861675981016, 340942910969751 @ 54, 491, -88 +282182650025525, 263920895309773, 283738165176073 @ -51, 32, 38 +175222281505950, 327795926311236, 288919373378961 @ -7, -33, 48 +203610741056006, 315351993213924, 457954035620069 @ -74, -9, -119 +411793133122106, 213959029928154, 259301058861179 @ -48, 266, 27 +432476452726972, 219019219129154, 260340538662435 @ -184, 233, 20 +294956139409926, 150225926998084, 328484305292289 @ 185, 368, -162 +94933805761992, 304133466189231, 116629685467707 @ 88, -6, 248 +213869806234850, 303957659662224, 381330986230895 @ 36, -27, -107 +324249965155280, 244015749073255, 325538321457167 @ -156, 64, -8 +204020974949300, 403929635408298, 213676122801494 @ -12, -139, 139 +127458406687238, 321498770403396, 308703413814017 @ -6, -13, 35 +74677283405558, 327179173350676, 420997721856185 @ 48, -19, -77 +411923014614368, 277182276970632, 325892221313033 @ 128, -285, -730 +340480374813716, 241083943648374, 287046603718142 @ -20, 73, -9 +146998255935190, 240681773236476, 341682571138657 @ 122, 69, -42 +418042573814204, 211686321670392, 265375766654441 @ 39, 397, -70 +153113647436501, 390443394869484, 280157774459059 @ 117, -153, 48 +291193961676373, 446102671092464, 273139299874422 @ -119, -197, 63 +437202043430383, 507590135550187, 472074955160631 @ -313, -194, -124 +305271083160219, 244329490183735, 28424126720066 @ 13, 64, 601 +334606092991871, 277726705572609, 303630625363749 @ -57, -12, -30 +52701888828311, 427349826081516, 61587071723162 @ 75, -121, 283 +409342157477252, 290010033116358, 251190233215076 @ -44, -223, 81 +123330289629590, 269411919288759, 314201988615575 @ 6, 38, 28 +356151164350527, 319457618422235, 413853524132942 @ -131, -88, -239 +208511227288886, 165191277452439, 400256484537239 @ 38, 182, -132 +234024451953536, 320401500816094, 291976790272679 @ 87, -80, 13 +409293892854108, 272490640711028, 295622194172247 @ 49, -169, -295 +416023395266618, 220868746022940, 263218713478589 @ 67, 309, -51 +369701854650686, 242470611374364, 316430261867329 @ -108, 69, -82 +302621904642824, 272924550548016, 316619895987515 @ -17, 5, -45 +278696653845302, 314773950749835, 283848906473669 @ -26, -58, 34 +254476567908506, 477997411600848, 447174328601195 @ -40, -275, -193 +419278018209926, 314332656372884, 241567341433409 @ -162, -268, 130 +388197377298901, 262093729552319, 275014479304134 @ -35, -14, -21 +368463494528032, 273116154496546, 330175669854590 @ -8, -43, -209 +435502215902390, 475377165120544, 363644592608225 @ -306, -227, -50 +396040999044278, 305320135854616, 352163776032517 @ -126, -148, -265 +372323293976554, 287487597551212, 298673880630717 @ 32, -125, -124 +249752767647558, 337289538285090, 296268868943363 @ 22, -96, 13 +403231753534114, 260771761896228, 270809706768379 @ -22, -33, -36 +264560139571342, 268592681281924, 537450818592033 @ -11, 23, -390 +362726932792526, 306161335328334, 341931426985109 @ -134, -68, -104 +117146704444912, 382001451385394, 314522853407372 @ 88, -107, 13 +82767869152437, 3046588441864, 248422696416366 @ 193, 403, 95 +323877330400272, 314735991447588, 289950152172953 @ -61, -81, 10 +263415925669301, 285548057377140, 311158147352890 @ -105, 15, 21 +249501612597355, 370062168626200, 138020884512325 @ 17, -149, 282 +78199485003510, 268188095418988, 217633358241545 @ 136, 34, 133 +125551895956388, 286134772662261, 308783339273627 @ 12, 20, 32 +193940218153623, 135055421160157, 29714845391155 @ 43, 220, 408 +323516371612517, 465248597127741, 319241378877158 @ -201, -154, 25 +366012263272902, 289478212874424, 263186414674133 @ 86, -147, 28 +432908028867378, 305991102542080, 366345047323985 @ -239, -194, -398 +313321834838454, 75086843290440, 286043238039853 @ 202, 702, -45 +250474230721356, 319964394470294, 334510598935879 @ 31, -70, -57 +174057969960112, 254199660145119, 49743897631456 @ 116, 48, 411 +253177291825270, 53817592049140, 253057205443001 @ -22, 355, 88 +355344773415848, 261920538064950, 286268172926843 @ 127, -16, -77 +196022554950364, 281831799479831, 174999662721115 @ -44, 22, 176 +198372779523446, 210384581267364, 110794121849969 @ -17, 105, 263 +74950461783371, 204122419949464, 225117671869479 @ 81, 107, 120 +274666675507656, 383477870793127, 387846688516452 @ -20, -176, -145 +176666777627130, 347549498739685, 36099454742560 @ 154, -116, 465 +302170899346194, 276390135444516, 440203278827669 @ 17, -9, -341 +373478072021574, 549991932033956, 252868929266841 @ -178, -509, 87 +326740322874906, 235103828274684, 304201254729269 @ 67, 93, -77 +418499092530565, 256376817516450, 263933243869099 @ -13, -39, -36 +250109542779460, 272540705284271, 320641776342750 @ 16, 16, -27 +116804600346347, 397098223254051, 277098219462087 @ 129, -144, 56 +187978517375262, 288408969435508, 424659037481449 @ -49, 17, -91 +339191055319202, 151667299947840, 536230758560789 @ -123, 230, -421 +340243013079030, 201885678579198, 353809170437066 @ -58, 165, -155 +374144971770712, 336457904720858, 285135827249281 @ -44, -263, -34 +269170359043526, 329644604610052, 333189346702721 @ -105, -40, -8 +269643356026626, 336228039046564, 364807710285969 @ -55, -73, -78 +367992368125868, 380920487317984, 275820144218247 @ -171, -187, 45 +402546492005113, 241998521818676, 369986050552796 @ -29, 79, -605 +428677508309675, 361043244998508, 337606351117349 @ -289, -121, -46 +407590175992926, 252398117828784, 257964316442509 @ -16, 12, 35 +137988172923457, 248734710615088, 437703690599499 @ -11, 59, -96 +400832879392470, 273404505436604, 254420959905185 @ -86, -67, 70 +266451172849762, 167755695252392, 311520767079681 @ -137, 142, 30 +263519107877516, 377602753401470, 287347285988531 @ -120, -83, 52 +332999742107231, 296940351010324, 92141170299094 @ -69, -49, 431 +438366527906854, 375545401928802, 487377337879179 @ -298, -195, -376 +406358374840474, 254936333841958, 236219231607151 @ 92, -26, 207 +177175227803066, 404182655437704, 39505448036189 @ 112, -191, 428 +330867273686662, 239980059316676, 260411984238785 @ 32, 77, 61 +359677373677451, 400839040597194, 255039143627771 @ -105, -307, 80 +308185196445801, 383509033989592, 77955699427429 @ -5, -245, 474 +346245979219652, 80809916466234, 374001682834457 @ 83, 688, -382 +201258569999502, 378066653586832, 406219616846161 @ 128, -178, -190 +145076119863526, 319821294251302, 315678552432493 @ 70, -34, 8 +356714124329066, 311961375460764, 165868328765549 @ -62, -121, 322 +324610616006643, 257549413990302, 265875369603904 @ 97, 21, 39 +105595504927318, 276354596080458, 349920077884107 @ 134, 21, -40 +404202446877986, 342335629663644, 359500619847449 @ -204, -181, -182 +397862037520494, 228296168014236, 113267297610263 @ -82, 135, 688 +401334151379056, 355179757322464, 496345849150189 @ -221, -163, -412 +455160135943041, 416020055225069, 478237633141809 @ -331, -236, -306 +280149964482584, 393889541798430, 260139147294329 @ -13, -208, 74 +290734909608806, 321836159927316, 195123004584785 @ -60, -64, 183 +350760823472473, 430559698017442, 362327994089754 @ -197, -175, -51 +258839444803333, 359307272174162, 491040464305920 @ -101, -71, -189 +438852036816038, 234662011691490, 230447917164767 @ -154, 176, 303 +319019287398668, 116780954839674, 124625877397421 @ 99, 468, 488 +352917393750542, 374422600929240, 331383505608641 @ -152, -164, -50 +195344845860214, 301121886409204, 332964776613930 @ 38, -17, -25 +400522757703600, 308426702399630, 196988468040207 @ -161, -136, 257 +284703994903406, 285213238709084, 384924770280109 @ -36, -7, -141 +362232770762533, 530818901488834, 343940129727285 @ -217, -287, -22 +45157694746670, 320876742566838, 375111995820257 @ 138, -24, -50 +289981591630079, 268210148916372, 256460299333274 @ 7, 15, 79 +331311259361931, 87468879551434, 251328459377219 @ 7, 493, 88 +255647357313974, 117174591735492, 264243000879137 @ 144, 367, 58 +39001138288358, 114608685277716, 247611338464993 @ 216, 234, 96 +78345398144248, 340057542653440, 372356987549459 @ 128, -53, -56 +424107853080798, 240875316375644, 320063937315273 @ 101, 118, -909 +156344745449478, 302595795634244, 241698220535297 @ 31, -7, 103 +259179525889580, 255577897264032, 245657998679240 @ 176, 35, 103 +210437756820600, 308843950567838, 334302104162039 @ -29, -16, -11 +304722473056688, 167943309812562, 245775315224337 @ 27, 246, 102 +248122674393056, 235916327443281, 327786852013169 @ -120, 72, 14 +277043010072101, 123245559791018, 410107190763725 @ -105, 217, -110 +366478985554055, 303131976216915, 315679631866466 @ -93, -94, -85 +137462543579714, 193702341645138, 279591949608261 @ 24, 120, 60 +326574684867086, 300236980784568, 384613730429168 @ 30, -93, -287 +315741107573126, 240787447589859, 243709950017684 @ 54, 74, 109 +355983983336914, 231680004259508, 342129249097317 @ -119, 91, -105 +404485560780974, 268790246483668, 317108024873793 @ -159, -19, -138 +419149424470741, 226367239846582, 244911238158061 @ -95, 185, 122 +407961452161350, 418167010527684, 231986081894817 @ -88, -837, 183 +67044622101206, 292746298461236, 438291405083389 @ 90, 11, -111 +274658774358620, 321359106006246, 308958759350345 @ 67, -106, -38 +173007315404390, 264144818818980, 34813586299265 @ 164, 29, 470 +102839643978206, 304673187492884, 348410364511329 @ 81, -7, -22 +354380589155632, 388319481114849, 286851917289450 @ -152, -192, 27 +96907155993854, 281599092045428, 384838864876129 @ 39, 25, -46 +291523929178939, 384166057783565, 354070361522453 @ -13, -208, -110 +345463466774126, 269347673310984, 253030914846059 @ 34, -19, 81 +249599421151031, 394069474260792, 366619266165953 @ 5, -181, -98 +394927184083574, 236003527736340, 249133632375809 @ -44, 105, 94 +362710918466496, 490756620149074, 390801202288303 @ -175, -352, -145 +220642186771519, 81912509824171, 355772958887750 @ -47, 260, -35 +367491008763696, 250945847161850, 440745640166155 @ -175, 52, -247 +409446291207698, 330788391219960, 292713233046131 @ -12, -543, -213 +265825149596573, 244764814652484, 385353795717239 @ -39, 63, -116 +86994969742866, 55589210487038, 266168402419143 @ 80, 274, 75 +320921915321526, 254694248869940, 238822296842297 @ 20, 37, 121 +179360093396906, 300619763168811, 350320133311184 @ 10, -6, -31 +16801569357148, 350089851801349, 165789472544968 @ 148, -52, 185 +433009745308238, 267184125912896, 273312821311953 @ -48, -230, -222 +309970609487918, 492679271429412, 562469959691093 @ -162, -225, -270 +330256938537206, 202170127764104, 344108272059359 @ -114, 137, -71 +213152807168420, 273421736762004, 323641629860561 @ 7, 23, -10 +172814593254919, 102501683328829, 247297979068407 @ -23, 218, 96 +219063685290956, 245608985586714, 349589973549179 @ -51, 62, -25 +430972008921002, 276609779270530, 253785144061591 @ -198, -115, 68 +141649807653506, 439100473497764, 78375461552199 @ 108, -209, 333 +312663199862950, 493743864927560, 363529412231925 @ -98, -344, -93 +423775394632430, 316536435784716, 224041240411841 @ -163, -326, 230 +327003532119926, 456110225504124, 400015395503489 @ -100, -318, -178 +435439277730906, 246965061962354, 245922460626269 @ -8, 26, 148 +283854002218376, 234052132379684, 271898082205509 @ -75, 79, 60 +208829340415406, 226635123507144, 482805207137969 @ -86, 81, -138 +349118117289530, 297778463181064, 357393083055225 @ -141, -32, -100 +299777008641782, 223081277864628, 246108022591457 @ -23, 106, 100 +438922841832386, 250584839183004, 267893924213549 @ -77, -31, -209 +438166800949636, 330151804608232, 277338384714133 @ -245, -379, -52 +420579599545694, 243376742146548, 280885790949095 @ -134, 71, -89 +258938745011974, 234495963857860, 323177451263361 @ 32, 82, -43 +36448776939491, 418742104071604, 231340500503399 @ 93, -113, 112 +404233472314214, 421003477346676, 157922394665657 @ -102, -735, 507 +418557987222777, 382699811698471, 419900143401830 @ -161, -584, -707 +232576226848342, 10003025240182, 203381334758715 @ 262, 689, 216 +255015067009391, 261956436770199, 352283365082099 @ -63, 40, -44 +169534023774211, 286707269739714, 329134981894044 @ 37, 9, -9 +299376592913146, 402426294254590, 407612010835957 @ -140, -130, -100 +215005453311763, 469310214941092, 228082501098752 @ -91, -161, 115 +65509617858912, 478414643265824, 223736911874359 @ 53, -167, 119 +290296833488420, 405978755186600, 456496601688775 @ -63, -199, -243 +83587437987463, 176759381379671, 129955793868008 @ 221, 164, 271 +304597538450668, 82725932045064, 153572240068869 @ 31, 453, 324 +228877113530886, 334430535336004, 330141973131265 @ 35, -82, -37 +353863067391134, 267957912015969, 257969098038089 @ 126, -42, 54 +206869829642681, 263886870857978, 400371713273807 @ -45, 41, -80 +132676076258186, 168321498105084, 311987025981449 @ 49, 153, 20 +411199564272509, 257136439666427, 222473651711960 @ -81, -8, 247 +201361581214124, 268475095057634, 370530268050177 @ -63, 38, -34 \ No newline at end of file diff --git a/day24-easy/sample.in b/day24-easy/sample.in new file mode 100644 index 0000000..94d8c9a --- /dev/null +++ b/day24-easy/sample.in @@ -0,0 +1,6 @@ +7 27 7 27 +19, 13, 30 @ -2, 1, -2 +18, 19, 22 @ -1, -1, -2 +20, 25, 34 @ -2, -2, -4 +12, 31, 28 @ -1, -2, -1 +20, 19, 15 @ 1, -5, -3 \ No newline at end of file diff --git a/day24-easy/src/main.zig b/day24-easy/src/main.zig new file mode 100644 index 0000000..364135f --- /dev/null +++ b/day24-easy/src/main.zig @@ -0,0 +1,274 @@ +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 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, + }; + } + }; +} + +fn readNumber(comptime T: type, line: []const u8, index: *usize) T { + var result: T = 0; + while (index.* < line.len and line[index.*] == ' ') : (index.* += 1) {} + + var is_negative = false; + if (index.* < line.len and line[index.*] == '-') { + is_negative = true; + index.* += 1; + } + + std.debug.assert(index.* < line.len); + + while (index.* < line.len) : (index.* += 1) { + const char = line[index.*]; + switch (char) { + '0'...'9' => { + result = result * 10 + (char - '0'); + }, + else => { + break; + }, + } + } + + return if (is_negative) (0 - result) else result; +} + +const Hailstone = struct { + initial_x: i64, + initial_y: i64, + initial_z: i64, + velocity_x: i16, + velocity_y: i16, + velocity_z: i16, +}; + +const RationalNumber = struct { + numerator: i128, + denominator: i64, + + fn init(numerator: i128, denominator: i64) RationalNumber { + std.debug.assert(denominator != 0); + + if (denominator < 0) { + return .{ + .numerator = -numerator, + .denominator = -denominator, + }; + } + + return .{ + .numerator = numerator, + .denominator = denominator, + }; + } + + fn isNonNegative(self: *const RationalNumber) bool { + return self.numerator >= 0; + } + + fn isIn(self: *const RationalNumber, first: i64, last: i64) bool { + return first * @as(i128, self.denominator) <= self.numerator and self.numerator <= last * @as(i128, self.denominator); + } +}; + +const ProjectionIntersection = struct { + x: RationalNumber, + y: RationalNumber, + time_first: RationalNumber, + time_second: RationalNumber, +}; + +const TestArea = struct { + x_first: i64, + x_last: i64, + y_first: i64, + y_last: i64, +}; + +fn getProjectionIntersection(a: Hailstone, b: Hailstone) ?ProjectionIntersection { + if (@as(i32, a.velocity_x) * @as(i32, b.velocity_y) == @as(i32, a.velocity_y) * @as(i32, b.velocity_x)) { + // projections of paths are parallel + + if (@as(i128, a.initial_x - b.initial_x) * a.velocity_y == @as(i128, a.initial_y - b.initial_y) * a.velocity_x) { + // both paths are on the same line, the puzzle does not document this behavior + unreachable; + } else { + // paths are on different parallel lines, and never intersect + return null; + } + } + + // set of linearly independent equations: + // t1 * a.velocity_x - t2 * b.velocity_x = b.initial_x - a.initial_x + // t1 * a.velocity_y - t2 * b.velocity_y = b.initial_y - a.initial_y + + const determinant = -@as(i64, a.velocity_x) * b.velocity_y + @as(i64, a.velocity_y) * b.velocity_x; + const dt1 = -@as(i128, b.initial_x - a.initial_x) * b.velocity_y + @as(i128, b.initial_y - a.initial_y) * b.velocity_x; + const dt2 = a.velocity_x * @as(i128, b.initial_y - a.initial_y) - a.velocity_y * @as(i128, b.initial_x - a.initial_x); + + const t1 = RationalNumber.init(dt1, determinant); + const t2 = RationalNumber.init(dt2, determinant); + const ax = RationalNumber.init(@as(i128, a.initial_x) * determinant + a.velocity_x * dt1, determinant); + const ay = RationalNumber.init(@as(i128, a.initial_y) * determinant + a.velocity_y * dt1, determinant); + const bx = RationalNumber.init(@as(i128, b.initial_x) * determinant + b.velocity_x * dt2, determinant); + const by = RationalNumber.init(@as(i128, b.initial_y) * determinant + b.velocity_y * dt2, determinant); + std.debug.assert(ax.numerator == bx.numerator); + std.debug.assert(ay.numerator == by.numerator); + + return .{ + .x = ax, + .y = ay, + .time_first = t1, + .time_second = t2, + }; +} + +fn solveForHailstones(hailstones: []const Hailstone, test_area: TestArea) usize { + var result: usize = 0; + for (0..hailstones.len) |i| { + for (0..i) |j| { + if (getProjectionIntersection(hailstones[i], hailstones[j])) |intersection| { + if (intersection.time_first.isNonNegative() and intersection.time_second.isNonNegative() and intersection.x.isIn(test_area.x_first, test_area.x_last) and intersection.y.isIn(test_area.y_first, test_area.y_last)) { + //std.debug.print("found intersection for {d} and {d}\n", .{ i, j }); + result += 1; + } + } + } + } + return result; +} + +fn parseFirstLine(line: []const u8) TestArea { + var i: usize = 0; + var x_first = readNumber(i64, line, &i); + + i += 1; + var x_last = readNumber(i64, line, &i); + + i += 1; + var y_first = readNumber(i64, line, &i); + + i += 1; + var y_last = readNumber(i64, line, &i); + + std.debug.assert(i >= line.len); + + return .{ + .x_first = x_first, + .x_last = x_last, + .y_first = y_first, + .y_last = y_last, + }; +} + +fn parseLine(line: []const u8) Hailstone { + var i: usize = 0; + var initial_x = readNumber(i64, line, &i); + + i += 1; + var initial_y = readNumber(i64, line, &i); + + i += 1; + var initial_z = readNumber(i64, line, &i); + + i += 2; + var velocity_x = readNumber(i16, line, &i); + + i += 1; + var velocity_y = readNumber(i16, line, &i); + + i += 1; + var velocity_z = readNumber(i16, line, &i); + + std.debug.assert(i >= line.len); + + return .{ + .initial_x = initial_x, + .initial_y = initial_y, + .initial_z = initial_z, + .velocity_x = velocity_x, + .velocity_y = velocity_y, + .velocity_z = velocity_z, + }; +} + +fn solveLines(lines: []const []const u8) usize { + var hailstones_list = StackList(Hailstone, usize, 400).init(); + + const test_area = parseFirstLine(lines[0]); + + for (lines[1..]) |line| { + hailstones_list.add(parseLine(line)); + } + + return solveForHailstones(hailstones_list.getSlice(), test_area); +} + +pub fn solveAll(reader: anytype) !usize { + var result: usize = 0; + while (true) { + var allocator_buffer: [50000]u8 = undefined; + var fba = std.heap.FixedBufferAllocator.init(&allocator_buffer); + var allocator = fba.allocator(); + + var lines = StackList([]u8, usize, 1500).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.getSlice()); + + 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}); +}