diff --git a/README.md b/README.md index 719ead2..d227751 100644 --- a/README.md +++ b/README.md @@ -12,20 +12,24 @@ Can be run with `zig run -O ReleaseFast benchmark.zig` | Day | Part | Mean (μs) | Min (μs) | Max (μs) | |-----|------|-------------------|----------|----------| -| 1 | 1 | +23 ± 2.24 | +23 | +68 | -| 1 | 2 | +24 ± 4.24 | +23 | +86 | -| 2 | 1 | +36 ± 5.29 | +32 | +88 | -| 2 | 2 | +287 ± 42.28 | +245 | +550 | -| 3 | 1 | +24 ± 2.45 | +20 | +41 | -| 3 | 2 | +21 ± 3.74 | +17 | +42 | -| 4 | 1 | +213 ± 15.75 | +202 | +300 | -| 4 | 2 | +212 ± 13.78 | +202 | +340 | -| 5 | 1 | +160 ± 30.46 | +120 | +479 | -| 5 | 2 | +160 ± 27.29 | +118 | +366 | -| 6 | 1 | +31 ± 3.87 | +28 | +128 | +| 1 | 1 | +23 ± 3.74 | +22 | +92 | +| 1 | 2 | +23 ± 2.00 | +22 | +53 | +| 2 | 1 | +34 ± 2.45 | +32 | +83 | +| 2 | 2 | +261 ± 36.08 | +239 | +764 | +| 3 | 1 | +19 ± 1.41 | +18 | +38 | +| 3 | 2 | +18 ± 1.00 | +17 | +44 | +| 4 | 1 | +214 ± 29.29 | +202 | +536 | +| 4 | 2 | +215 ± 29.09 | +201 | +558 | +| 5 | 1 | +139 ± 36.43 | +117 | +540 | +| 5 | 2 | +153 ± 44.35 | +116 | +475 | +| 6 | 1 | +31 ± 2.83 | +28 | +101 | | 6 | 2 | Too long ~60s | 0 | 0 | -| 7 | 1 | +191 ± 33.14 | +156 | +340 | +| 7 | 1 | +182 ± 18.06 | +157 | +439 | | 7 | 2 | Too long ~0.2s | 0 | 0 | -| 8 | 1 | +527 ± 69.48 | +481 | +896 | -| 8 | 2 | +803 ± 92.79 | +736 | +1328 | -| Total | | +2712 ± 346.82 | +2403 | +5052 | +| 8 | 1 | +540 ± 53.40 | +506 | +895 | +| 8 | 2 | +828 ± 81.50 | +769 | +1333 | +| 9 | 1 | +1770 ± 215.91 | +1389 | +5250 | +| 9 | 2 | Too long ~0.6s | 0 | 0 | +| 10 | 1 | +38 ± 5.66 | +36 | +160 | +| 10 | 2 | +27 ± 2.45 | +27 | +64 | +| Total| | +4515 ± 565.65 | +3898 | +11425 | diff --git a/benchmark.zig b/benchmark.zig index efcecbf..10c2f55 100644 --- a/benchmark.zig +++ b/benchmark.zig @@ -18,6 +18,8 @@ const d81 = @import("day8/part1.zig"); const d82 = @import("day8/part2.zig"); const d91 = @import("day9/part1.zig"); const d92 = @import("day9/part2.zig"); +const d101 = @import("day10/part1.zig"); +const d102 = @import("day10/part2.zig"); const NUMBER_OF_RUN = 1000; @@ -55,7 +57,10 @@ pub fn main() !void { try benchmark(d82.main, 8, 2); separator(); try benchmark(d91.main, 9, 1); - try benchmark(d92.main, 9, 2); + print("| 9 | 2 | Too long ~0.6s | 0 | 0 |\n", .{}); + separator(); + try benchmark(d101.main, 10, 1); + try benchmark(d102.main, 10, 2); separator(); print("| Total | {d: >8} ± {d: <6.2} | {d:>8} | {d:>8} |\n", .{ total_mean, total_std_dev, total_min, total_max }); separator(); diff --git a/day10/input b/day10/input new file mode 100644 index 0000000..94d285f --- /dev/null +++ b/day10/input @@ -0,0 +1,60 @@ +432109865210212123765432101234321098543289654320132112121058 +045678774324301012892343023445456787650198763013241001034569 +187678789465692321001056014896234986456787012894653212123678 +296589921056789433217837895687145675323891233765784589238987 +345437835434576544786921278761010014210710321212098676521067 +032126546323465435695430789760121223121653450303145125430678 +123010567810156543212345699859834321056544067654236012321589 +543213498987657665401030787348765430187432198765987622345432 +654100332394348972342321895201256589196343089543212331056741 +789011241003238981089400776100343678015434567630105449879870 +296721256210169895676510385011892349101325678921256756768987 +129830787323456765410321294332761058210012310123890891057610 +056745698234556786329454301245656567341110567894781232346521 +145894510149645699438765892398305678956923498965654343765430 +236586789838732388454326765567214307967845697874505652894321 +105675676545321267565810674354303212875430786543216701678912 +234321501656130054278989983289432120123421803403545810787600 +321030432567032123123678100176563018987578912012932921298541 +892349803498145031034563210327898101879647810123871092567432 +785056712387236567687654389410787632768756303294562783458943 +176120987656547858998545076585894583459843214785103698327874 +015431234543218947657898145894983298708941025672234567016761 +329122345692105438941763236783470165617652912341013053205430 +478031001785676323430154100102565674320543805432332122124321 +567649872434985610121894321211056589211230796901440345034234 +456659763323810765456765894323987401108921687878981236565105 +306778654310329876365498765012342322317834510967892387156076 +219865011078478901278321001231451015436123423456785493087189 +652104102569560110669125198340760896895034587655476334196892 +765233243458721321701034567654878987764105694344301243234561 +894310321029832459852567878723965430653298743213210358765410 +132123478010741069743478989014436321541056543401821569898324 +098034569123658978654321876101521087632347812114981678876543 +107765678034567867569270965437698794545938903003470549987432 +256872345621098654378187602348967003897821094012561232789501 +345901436438767789210094511059854112766123285723034341076521 +217894387589656231234543223456743245675054176894125652112430 +306105498678543140567672100145101230984169065765898763203431 +495218321067012056478981041234230121243078434965235678976521 +584349452652100987329891230765345698732154567874143454989210 +673458763643211011010010049874556781235463456963056763474321 +567647601781012567892102156743765470346322161012369812565232 +498678432692123476543103095652834387457210052623871001450143 +304509543543001989698234589501921098768921106780982341019898 +213219601982132670787825676501432349810123235691987432870767 +894348732676544561236910787432321056769894344302346549961251 +765210145690125650345210097899867892110765654219854678450340 +890100126780034743094303126934786543023234565678765012321231 +765987034621129802185412235025696541032167876789874349876012 +876856541234988012276543384110567832249054965694101256778123 +965987650945876543543215493201378980158345434783450126789894 +457871056876067875456906780110234589267210321692569034670765 +320432347780128965307878767820199674307890160541078765521254 +011876548991234534218349856936788765216543254332112340432345 +432965432781049621029256743245215656325321067210003451201056 +547876501632898756540178652101304567101452198760116764342767 +656983432542765987438769789012453898212968765641985895433898 +898792323101874104329054210589562456703879454332076016924567 +125601017652963265012123323676571329894312303549165327810430 +034340178943012378901012334568980016765601212678234456901321 diff --git a/day10/part1.zig b/day10/part1.zig new file mode 100644 index 0000000..b25356d --- /dev/null +++ b/day10/part1.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const print = std.debug.print; +const input = @embedFile("input"); +const MAP_SIZE = 60; + +pub fn main() !void { + var map: [MAP_SIZE + 2][MAP_SIZE + 2]u8 = undefined; + + for (0..MAP_SIZE + 2) |x| for (0..MAP_SIZE + 2) |y| { + map[x][y] = 0; + }; + + for (input, 0..) |c, i| { + if (c == '\n') continue; + map[@divFloor(i, MAP_SIZE + 1) + 1][i % (MAP_SIZE + 1) + 1] = c - '0'; + } + + var total: usize = 0; + var founded = std.AutoHashMap([2]usize, void).init(std.heap.page_allocator); + defer founded.deinit(); + for (1..MAP_SIZE + 1) |x| for (1..MAP_SIZE + 1) |y| if (map[x][y] == 0) { + founded.clearRetainingCapacity(); + try step(map, x, y, &total, &founded); + }; + + try std.testing.expectEqual(744, total); +} + +fn step(map: [MAP_SIZE + 2][MAP_SIZE + 2]u8, x: usize, y: usize, total: *usize, founded: *std.AutoHashMap([2]usize, void)) !void { + const height = map[x][y]; + if (height == 9 and !founded.contains([2]usize{ x, y })) { + total.* += 1; + try founded.put([2]usize{ x, y }, {}); + } + if (map[x - 1][y] == (height + 1)) try step(map, x - 1, y, total, founded); // Up + if (map[x + 1][y] == (height + 1)) try step(map, x + 1, y, total, founded); // Down + if (map[x][y - 1] == (height + 1)) try step(map, x, y - 1, total, founded); // Left + if (map[x][y + 1] == (height + 1)) try step(map, x, y + 1, total, founded); // Right +} diff --git a/day10/part2.zig b/day10/part2.zig new file mode 100644 index 0000000..946f35b --- /dev/null +++ b/day10/part2.zig @@ -0,0 +1,27 @@ +const std = @import("std"); +const print = std.debug.print; +const input = @embedFile("input"); +const MAP_SIZE = 60; + +pub fn main() !void { + var map = [_][MAP_SIZE + 2]u8{[_]u8{0} ** (MAP_SIZE + 2)} ** (MAP_SIZE + 2); + + for (input, 0..) |c, i| { + if (c == '\n') continue; + map[@divFloor(i, MAP_SIZE + 1) + 1][i % (MAP_SIZE + 1) + 1] = c - '0'; + } + + var total: usize = 0; + for (1..MAP_SIZE + 1) |x| for (1..MAP_SIZE + 1) |y| if (map[x][y] == 0) try step(map, x, y, &total); + + try std.testing.expectEqual(1651, total); +} + +fn step(map: [MAP_SIZE + 2][MAP_SIZE + 2]u8, x: usize, y: usize, total: *usize) !void { + const height = map[x][y]; + if (height == 9) total.* += 1; + if (map[x - 1][y] == (height + 1)) try step(map, x - 1, y, total); // Up + if (map[x + 1][y] == (height + 1)) try step(map, x + 1, y, total); // Down + if (map[x][y - 1] == (height + 1)) try step(map, x, y - 1, total); // Left + if (map[x][y + 1] == (height + 1)) try step(map, x, y + 1, total); // Right +} diff --git a/day9/part1.zig b/day9/part1.zig index f1b1305..75b0df4 100644 --- a/day9/part1.zig +++ b/day9/part1.zig @@ -11,6 +11,10 @@ const Block = struct { pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer { + const deinit_status = gpa.deinit(); + if (deinit_status == .leak) @panic("TEST FAIL"); + } const allocator = gpa.allocator(); var total_file_block: usize = 0;