W4P3BJJLXCXZEIOGWLTYBFEKD3NWLP2EAGN47ZXPBTHZ5KSC747QC const std = @import("std");const path = "../data/day04/input.txt";const retSize = u16;const grid_item_size = u7;const Grid = struct {boardMap: std.AutoHashMap(grid_item_size, Point),row: [5]u3 = .{ 0, 0, 0, 0, 0 },col: [5]u3 = .{ 0, 0, 0, 0, 0 },pub fn getValue(self: @This()) retSize {var sum: retSize = 0;var it = self.boardMap.iterator();while (it.next()) |item| {if (!item.value_ptr.marked) {sum += item.key_ptr.*;}}return sum;}};const Point = struct {row: u3, // < 5col: u3, // < 5marked: bool = false,};const Bingo = struct {numbers: std.ArrayList(grid_item_size),boards: std.ArrayList(Grid),};fn first() anyerror!retSize {var gpa = std.heap.GeneralPurposeAllocator(.{}){};const allocator = gpa.allocator();var bg = try parseInput(allocator);defer { // cleanup codefor (bg.boards.items) |_, idx| {bg.boards.items[idx].boardMap.deinit();}bg.boards.deinit();bg.numbers.deinit();}for (bg.numbers.items) |num| {for (bg.boards.items) |_, idx| {// std.debug.print("{any} {any}\n", .{ num, idx });var curr = &bg.boards.items[idx];const coord = curr.boardMap.get(num) orelse continue;curr.row[coord.row] += 1;curr.col[coord.col] += 1;try curr.boardMap.put(num, .{ .row = coord.row, .col = coord.col, .marked = true });// std.debug.print("{any} {any} {any}\n", .{num, curr.row, curr.col});if ((curr.row[coord.row] == 5) or (curr.col[coord.col] == 5)) {return num * curr.getValue();}}}unreachable;}fn second() anyerror!retSize {var gpa = std.heap.GeneralPurposeAllocator(.{}){};const allocator = gpa.allocator();var bg = try parseInput(allocator);defer { // cleanup codefor (bg.boards.items) |_, idx| {bg.boards.items[idx].boardMap.deinit();}bg.boards.deinit();bg.numbers.deinit();}var to_remove = std.ArrayList(usize).init(allocator);defer to_remove.deinit();for (bg.numbers.items) |num| {for (bg.boards.items) |_, idx| {var curr = &bg.boards.items[idx];const coord = curr.boardMap.get(num) orelse continue;curr.row[coord.row] += 1;curr.col[coord.col] += 1;try curr.boardMap.put(num, .{ .row = coord.row, .col = coord.col, .marked = true });if ((curr.row[coord.row] == 5) or (curr.col[coord.col] == 5)) {if (bg.boards.items.len == 1) {return num * curr.getValue();} else {try to_remove.append(idx);}}}std.sort.sort(usize, to_remove.items, {}, comptime std.sort.desc(usize));for (to_remove.items) |idx| {bg.boards.items[idx].boardMap.deinit();_ = bg.boards.swapRemove(idx);}try to_remove.resize(0);}unreachable;}fn parseInput(allocator: std.mem.Allocator) anyerror!Bingo {const input = @embedFile(path);var lines = std.mem.split(u8, input, "\n");var ret = Bingo{.numbers = std.ArrayList(grid_item_size).init(allocator),.boards = std.ArrayList(Grid).init(allocator),};var gridRow: u3 = 0;var gridCol: u3 = 0;var g = Grid{ .boardMap = std.AutoHashMap(grid_item_size, Point).init(allocator) };var idx: usize = 0;while (lines.next()) |line| : (idx += 1) {// read in numbersif (idx == 0) {var nums = std.mem.tokenize(u8, line, ",");while (nums.next()) |num| {try ret.numbers.append(try std.fmt.parseUnsigned(grid_item_size, num, 10));}continue;}// std.debug.print("line: {any}\n", .{line});// read in Gridsvar items = std.mem.tokenize(u8, line, " ");while (items.next()) |item| {const value = try std.fmt.parseUnsigned(grid_item_size, item, 10);// std.debug.print("{any} -> {any} {any}\n", .{value, gridRow, gridCol});try g.boardMap.put(value, .{ .row = gridRow, .col = gridCol });gridCol += 1;}gridCol = 0;if (line.len != 0) gridRow += 1;// append gridif (gridRow == 5) {try ret.boards.append(g);gridRow = 0;gridCol = 0;// create new gridg = Grid{ .boardMap = std.AutoHashMap(grid_item_size, Point).init(allocator) };}}return ret;}pub fn main() anyerror!void {var timer = try std.time.Timer.start();_ = try first();const f = timer.lap() / 1000;_ = try second();const s = timer.lap() / 1000;std.debug.print("Day 4 \t\tfirst: {d}µs \t\tsecond: {d}µs\n", .{ f, s });}test "first" {try std.testing.expectEqual(@as(retSize, 11536), try first());}test "second" {try std.testing.expectEqual(@as(retSize, 1284), try second());}
72,86,73,66,37,76,19,40,77,42,48,62,46,3,95,17,97,41,10,14,83,90,12,23,81,98,11,57,13,69,28,63,5,78,79,58,54,67,60,34,39,84,94,29,20,0,24,38,43,51,64,18,27,52,47,74,59,22,85,65,80,2,99,70,33,91,53,93,9,82,8,50,7,56,30,36,89,71,21,49,31,88,26,96,16,1,75,87,6,61,4,68,32,25,55,44,15,45,92,3558 60 65 4 9173 31 80 83 4498 47 33 21 2576 6 41 94 5055 30 52 11 368 27 21 18 9459 3 47 1 6467 16 90 20 8326 43 87 82 419 93 89 48 7228 31 21 56 3563 98 25 65 6146 11 91 24 3476 22 15 0 8783 80 39 12 7148 9 95 18 1267 20 40 29 510 85 21 69 8971 81 97 64 1375 24 65 50 561 27 24 93 4247 71 21 91 8839 7 65 64 5128 16 15 25 5648 23 32 20 2226 37 62 13 8253 54 16 50 7542 18 51 64 6025 47 71 38 7727 57 29 76 4097 20 68 90 7042 96 62 49 2756 67 12 13 9471 35 31 54 1773 3 5 23 1943 93 19 7 8088 37 51 49 5829 97 17 94 6561 22 75 98 8528 73 84 47 196 99 82 7 9143 32 48 39 6049 51 15 30 1116 72 52 69 1879 95 41 68 5990 3 81 44 999 40 94 19 3741 52 43 83 462 42 26 24 7049 74 34 66 7759 83 91 44 062 85 69 18 874 5 94 48 3250 46 25 6 9687 41 75 22 3136 61 69 96 8777 60 49 32 4592 97 34 21 7031 62 6 63 256 93 26 54 8530 96 54 85 8229 92 1 74 4263 26 65 34 9887 91 6 53 6462 45 13 50 563 67 61 56 2334 48 39 76 3744 83 35 72 2970 45 55 47 431 92 6 99 599 42 99 80 1481 57 72 63 1819 67 25 90 580 4 51 52 6993 60 73 28 235 27 68 24 3569 67 49 1 7973 64 4 42 3255 74 44 90 4592 43 33 2 37 45 24 51 3371 66 6 13 2377 55 76 48 4439 70 43 36 3288 5 69 47 9245 40 5 93 8064 37 10 35 411 6 75 30 3874 44 26 90 991 81 8 50 3292 71 59 47 3245 65 68 55 2899 88 17 36 8216 22 61 48 9329 5 69 24 5188 85 54 41 9210 67 40 4 3029 45 32 47 5196 81 94 58 3439 72 7 46 9852 33 11 60 6919 9 88 93 7582 45 72 48 4485 61 87 79 2671 80 21 0 360 1 57 93 8117 32 61 21 476 24 46 98 339 84 91 6 235 59 52 96 5422 35 20 41 457 8 9 97 1019 25 12 65 535 50 91 32 2358 96 15 7 7890 78 31 47 8881 56 55 95 1060 42 68 34 5753 64 85 50 3548 16 89 93 7764 4 0 7 1658 50 37 98 3485 71 75 22 5781 91 6 86 1961 63 95 40 366 88 55 0 3268 9 98 73 3615 45 39 67 691 11 79 23 8412 80 62 14 1779 31 43 75 9754 78 87 60 9941 34 32 61 2644 37 20 12 1816 73 10 71 2292 83 15 36 2728 21 35 42 1860 87 41 38 9545 10 70 5 8011 30 56 17 6163 25 57 42 1424 71 2 11 9356 73 16 47 2887 64 8 27 8321 50 78 48 6218 29 90 1 1639 72 21 88 5519 13 69 83 717 97 43 58 6123 96 9 33 8136 13 32 1 1157 17 91 76 7229 83 35 68 9087 12 39 19 099 31 16 25 435 52 64 73 400 1 51 3 1461 91 55 30 8833 83 31 13 7124 97 36 19 5381 15 67 72 7820 21 40 96 3713 5 33 83 6622 61 91 56 8435 86 75 41 4628 29 73 84 2642 54 3 15 1234 16 62 91 3053 13 5 46 5567 18 1 59 2416 39 26 11 6736 20 62 27 7885 25 9 87 666 70 60 98 5994 46 17 81 1035 86 49 59 3888 54 68 17 8710 9 90 30 6282 20 32 77 7681 83 79 0 6726 9 63 23 4522 44 36 60 484 91 54 6 7894 2 62 61 3152 88 42 21 291 32 44 0 913 36 26 6 1750 74 14 51 8825 10 73 43 1647 68 34 2 8174 67 38 24 3246 53 63 18 8233 41 48 90 556 3 20 99 1796 94 59 21 8749 72 39 25 6259 99 27 53 9833 46 92 38 818 82 90 70 203 2 54 0 7554 34 45 0 1995 11 27 62 503 77 79 17 8174 57 40 83 4788 72 39 92 1665 1 91 71 6781 23 34 48 9028 92 84 11 35 8 61 16 7683 46 24 55 8242 87 55 23 5979 54 81 48 9561 16 44 13 9153 98 72 30 8865 69 83 36 6445 80 47 27 7854 36 16 75 117 26 68 28 3943 87 49 0 8956 24 7 85 9218 50 95 70 4944 47 69 92 5496 28 79 67 1613 31 29 98 1453 20 5 66 2588 33 27 97 6720 69 22 35 050 73 70 52 9171 32 48 21 653 5 15 30 8677 85 98 6 1115 53 21 89 6751 40 62 8 3637 69 47 24 2939 63 64 72 4413 7 80 86 2945 91 82 41 4269 74 12 68 3884 51 6 10 1457 26 62 17 2431 88 98 2 1133 40 23 30 4325 16 50 41 2212 51 99 6 8991 66 90 97 3222 74 70 98 5425 20 76 40 3821 99 69 10 4111 59 46 61 3687 50 49 84 7857 25 39 22 8637 8 61 78 7349 30 95 3 4418 1 58 91 4624 64 17 13 6061 10 14 53 8332 28 66 65 4063 86 48 76 692 69 95 24 5559 71 72 30 3347 76 18 53 5637 42 9 28 20 80 99 48 2779 20 15 5 543 7 71 89 8731 8 16 28 7956 77 66 59 369 99 85 57 667 82 73 87 9137 52 43 58 819 2 96 51 6468 30 36 3 6633 57 41 83 5290 84 54 20 5614 88 62 76 3817 86 75 54 513 59 68 87 7444 62 31 57 987 24 36 71 7669 23 19 70 7355 40 1 98 2485 29 39 72 380 28 94 67 6514 70 49 97 9011 2 74 44 4849 91 63 99 671 69 29 22 8158 77 62 74 169 68 38 6 7872 24 94 64 7694 98 58 41 6377 76 73 62 4974 38 87 92 4683 89 48 5 1526 19 8 44 5672 21 44 61 9984 8 66 69 3212 38 57 86 3787 74 41 3 9190 78 45 89 4922 85 17 59 899 43 79 65 8456 36 66 78 5750 10 39 67 6912 14 34 68 232 8 58 29 8953 54 69 19 4822 52 35 36 626 46 44 15 6121 71 63 83 9984 25 24 59 9549 29 26 17 5839 51 15 72 211 13 35 85 114 91 18 89 5349 76 48 58 1932 11 53 24 6764 12 3 45 316 75 44 46 8059 90 42 39 8371 25 59 18 1254 10 77 52 1342 68 28 17 3733 82 47 22 2438 14 79 41 8481 56 34 28 7183 27 14 16 3063 26 6 45 2986 53 60 50 158 43 7 44 9132 30 59 58 5576 24 8 79 035 14 46 16 9920 19 98 4 9474 85 51 31 1732 37 22 91 521 20 88 17 8664 61 34 23 7928 42 5 67 724 94 13 74 1411 62 72 23 4560 65 56 81 2983 64 73 61 157 77 2 30 910 39 50 28 8850 17 65 72 1624 86 42 39 6820 84 27 98 1257 41 1 63 3294 22 38 81 1845 51 38 88 9421 24 44 74 6329 19 26 57 3240 31 56 80 5336 70 33 22 9399 86 22 85 1774 45 78 67 3918 42 77 46 2720 31 40 8 8173 47 19 96 1592 54 10 73 2444 18 74 32 1969 8 14 46 3363 57 97 65 362 34 4 36 3571 7 5 3 8894 79 47 41 2851 31 91 23 5299 42 39 87 5448 59 97 68 49 93 86 99 2614 83 45 43 4884 23 17 28 435 79 47 75 6154 65 59 81 4289 99 27 58 9623 52 50 24 7047 83 61 5 6540 19 66 21 055 10 13 81 5126 30 33 1 5741 60 44 96 7072 29 24 0 6276 69 16 21 9352 48 79 84 1465 36 74 22 8021 69 47 31 6142 50 92 27 1812 24 91 29 628 73 70 76 1160 79 47 8 3065 50 54 56 2314 98 33 25 7674 71 86 37 6672 77 85 39 5371 56 53 3 1988 99 40 61 3795 87 8 78 3454 75 57 96 1250 98 69 58 9416 9 85 2 8788 98 37 13 127 42 15 18 2229 58 25 99 7238 86 78 91 923 21 39 81 7666 47 80 44 1359 2 96 17 6235 51 8 37 4188 74 14 92 1871 15 74 60 3217 67 69 62 8019 1 78 89 8522 96 11 8 134 86 48 0 6122 29 68 36 5573 62 86 31 907 75 49 81 469 41 83 67 5132 24 59 5 9984 94 25 18 5776 6 98 79 2942 10 71 89 574 78 53 85 5164 20 49 47 3795 22 29 37 8713 54 0 28 7421 50 49 8 9281 58 34 2 4365 19 63 52 7615 69 67 23 2182 84 20 83 5349 59 86 2 3171 89 68 40 7976 25 42 22 1382 11 88 77 9248 8 61 13 6432 72 80 67 352 4 25 75 9453 20 33 6 1641 95 57 46 1750 3 99 74 2897 39 73 58 7075 35 94 51 8712 44 43 21 7174 0 85 48 2142 28 16 6 5351 25 18 72 4783 4 37 79 2996 39 78 44 5645 60 35 49 425 94 68 34 5654 17 61 74 5070 48 98 5 1415 85 21 8 2229 33 86 96 622 43 61 10 4611 12 76 31 8088 78 37 92 1453 57 90 74 132 56 43 50 600 77 72 51 3771 40 69 97 3865 2 67 47 8154 79 82 84 9082 26 28 87 9361 33 38 89 1225 13 5 37 3515 67 8 94 1695 59 64 41 7085 22 43 18 1364 73 96 16 8461 44 12 37 1147 35 80 32 142 41 60 29 8652 87 78 6 6150 21 54 4 291 1 35 48 7299 43 69 44 4030 41 19 29 2710 20 95 69 2266 53 77 37 2851 89 85 48 2116 14 60 73 4396 86 12 97 9048 7 67 47 9671 61 60 64 1813 15 57 86 9346 41 53 88 3282 65 26 34 1080 92 2 54 50 17 78 81 4333 15 51 73 6171 31 47 38 1152 29 23 97 1476 52 54 92 8074 50 11 27 7863 9 25 38 203 90 39 37 1587 45 17 93 62