entry #1
written by vintium
5 likes
guesses
- Edgex42 (by SoundOfSpouting)
- Edgex42 (by Olivia)
- GNU Radio Shows (by razetime)
- GNU Radio Shows (by Edgex42)
- IFcoltransG (by Palaiologos)
- Olive (by GNU Radio Shows)
- Olivia (by pyrotelekinetic)
- vintium (by Olive)
- vintium (by LyricLy)
comments 0
code-guessing-fifteen.zig ASCII text
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | const std = @import("std"); const stdout = std.io.getStdOut().writer(); const assert = std.debug.assert; // positions start in the top left corner of the card, // counting from zero. x increases rightwards, y increases leftwards. // x and y must be in the range [0, 4]. const Pos = struct { x: u3, y: u3, }; const Card = struct { c: [5][5]u7, fn find(self: Card, needle: u7) ?Pos { for (self.c) |row, y| { for (row) |number, x| { if (number == needle) { return Pos{ .x = @intCast(u3, x), .y = @intCast(u3, y) }; } } } return null; } test "find a number in a card" { // zig fmt: off const c = Card{ .c = [5][5]u7{ [_]u7{ 67, 64, 11, 28, 16 }, [_]u7{ 63, 26, 20, 15, 10 }, [_]u7{ 68, 44, 00, 53, 70 }, // <- 70 is here at (4, 2) [_]u7{ 22, 56, 38, 51, 09 }, [_]u7{ 47, 33, 17, 39, 59 } }, }; // zig fmt: on try std.testing.expectEqual(c.find(70), Pos{ .x = 4, .y = 2 }); } test "don't find a number in a card" { // zig fmt: off const c = Card{ .c = [5][5]u7{ [_]u7{ 67, 64, 11, 28, 16 }, [_]u7{ 63, 26, 20, 15, 10 }, [_]u7{ 68, 44, 00, 53, 70 }, // 69 is not in this card [_]u7{ 22, 56, 38, 51, 09 }, [_]u7{ 47, 33, 17, 39, 59 } }, }; // zig fmt: on try std.testing.expectEqual(c.find(69), null); } }; const Marks = struct { msk: u25, // bit mask storing marked positions. the lowest bit is 0, 0, the highest bit is at 4, 4. // mapping diagram: // 0b_00000_00000_00000_00000_00000 // y: 44444 33333 22222 11111 00000 // x: 43210 43210 43210 43210 43210 // set the bit at the mapped position to 1 fn mark(self: *Marks, x: u3, y: u3) void { assert(x <= 4 and y <= 4); self.msk |= (@as(u25, 1) << (@as(u5, x) + @as(u5, y) * @as(u5, 5))); } // wrapper to use the `Pos` struct. tests depend on passing in x, y as args, so the original // function wasn't changed. fn markPos(self: *Marks, p: Pos) void { self.mark(p.x, p.y); } fn row(self: Marks) bool { // zig fmt: off const rows = [5]u25{ 0b00000_00000_00000_00000_11111, 0b00000_00000_00000_11111_00000, 0b00000_00000_11111_00000_00000, 0b00000_11111_00000_00000_00000, 0b11111_00000_00000_00000_00000 }; // zig fmt: on var match = false; for (rows) |r| { match = ((r & self.msk) == r) or match; } return match; } test "detect five-in-a-row" { var m = Marks{ .msk = 0 }; // O O O O O // O O O O O // O O O O O // X X X X X // O O O O O m.mark(0, 3); m.mark(1, 3); m.mark(2, 3); m.mark(3, 3); m.mark(4, 3); try std.testing.expectEqual(m.row(), true); try std.testing.expectEqual(m.wins(), true); } test "noisy row" { var m = Marks{ .msk = 0 }; // X X O O O // O O O O O // O O O O O // X X X X X // O X O O O m.mark(0, 3); m.mark(1, 0); m.mark(1, 3); m.mark(2, 3); m.mark(3, 3); m.mark(4, 3); m.mark(0, 0); m.mark(1, 0); m.mark(1, 4); try std.testing.expectEqual(m.row(), true); try std.testing.expectEqual(m.wins(), true); } fn col(self: Marks) bool { // zig fmt: off const cols = [5]u25{ 0b00001_00001_00001_00001_00001, 0b00010_00010_00010_00010_00010, 0b00100_00100_00100_00100_00100, 0b01000_01000_01000_01000_01000, 0b10000_10000_10000_10000_10000 }; // zig fmt: on var match = false; for (cols) |c| { match = ((c & self.msk) == c) or match; } return match; } test "detect five-in-a-col" { var m = Marks{ .msk = 0 }; // O O X O O // O O X O O // O O X O O // O O X O O // O O X O O m.mark(2, 0); m.mark(2, 1); m.mark(2, 2); m.mark(2, 3); m.mark(2, 4); try std.testing.expectEqual(m.col(), true); try std.testing.expectEqual(m.wins(), true); } test "noisy col" { var m = Marks{ .msk = 0 }; // X O X O O // O O X O O // O O X O X // O O X O O // O O X O O m.mark(2, 0); m.mark(2, 1); m.mark(2, 2); m.mark(2, 3); m.mark(2, 4); m.mark(4, 2); m.mark(0, 0); try std.testing.expectEqual(m.col(), true); try std.testing.expectEqual(m.wins(), true); } fn diag(self: Marks) bool { // zig fmt: off const diags = [2]u25{ 0b00001_00010_00100_01000_10000, 0b10000_01000_00100_00010_00001, }; // zig fmt: on var match = false; for (diags) |d| { match = ((d & self.msk) == d) or match; } return match; } test "detect diagonal" { var m = Marks{ .msk = 0 }; // X O O O O // O X O O O // O O X O O // O O O X O // O O O O X m.mark(0, 0); m.mark(1, 1); m.mark(2, 2); m.mark(3, 3); m.mark(4, 4); try std.testing.expectEqual(m.diag(), true); try std.testing.expectEqual(m.wins(), true); } test "noisy diag" { var m = Marks{ .msk = 0 }; // X O O O X // O X O O O // O O X O O // O O X X O // O O X O X m.mark(0, 0); m.mark(1, 1); m.mark(2, 2); m.mark(2, 3); m.mark(3, 3); m.mark(4, 4); m.mark(2, 4); m.mark(4, 0); try std.testing.expectEqual(m.diag(), true); try std.testing.expectEqual(m.wins(), true); } fn wins(self: Marks) bool { return self.row() or self.col() or self.diag(); } test "loses 1" { var m = Marks{ .msk = 0 }; // X O O X O // O O O X O // O O X O O // O O X O O // O O X O X m.mark(0, 0); m.mark(3, 1); m.mark(2, 2); m.mark(2, 3); m.mark(3, 0); m.mark(4, 4); m.mark(2, 4); try std.testing.expectEqual(m.wins(), false); } test "loses 2" { var m = Marks{ .msk = 0 }; // X O O X O // O X O X O // X O X O X // O X O O O // O O X O X m.mark(0, 0); m.mark(3, 0); m.mark(1, 1); m.mark(3, 1); m.mark(0, 2); m.mark(2, 2); m.mark(4, 2); m.mark(1, 3); m.mark(4, 4); m.mark(2, 4); try std.testing.expectEqual(m.wins(), false); } }; const Player = struct { id: usize, card: Card, marked: Marks, fn play(self: *Player, call: u7) ?usize { if (self.card.find(call)) |p| { self.marked.markPos(p); } if (self.marked.wins()) { return self.id; } else { return null; } } }; test "a test game" { var me = Player{ .id = 0, // zig fmt: off .card = Card{ .c = [5][5]u7{ [_]u7{ 67, 64, 11, 28, 16 }, [_]u7{ 63, 26, 20, 15, 10 }, [_]u7{ 68, 44, 00, 53, 70 }, [_]u7{ 22, 56, 38, 51, 09 }, [_]u7{ 47, 33, 17, 39, 59 }, }, }, // zig fmt: on .marked = Marks{ .msk = 0 }, }; try std.testing.expectEqual(me.play(0), null); try std.testing.expectEqual(me.play(11), null); try std.testing.expectEqual(me.play(38), null); try std.testing.expectEqual(me.play(50), null); try std.testing.expectEqual(me.play(10), null); try std.testing.expectEqual(me.play(17), null); try std.testing.expectEqual(me.play(20), 0); } pub fn entry(cards: []const [5][5]u7, calls: []const u7) usize { for (cards) |crd, i| { var player = Player{ .id = i, .card = Card{ .c = crd }, .marked = Marks{ .msk = 0 }, }; for (calls) |call| { const winnermaybe = player.play(call); if (winnermaybe) |winner| { return winner; } } } unreachable; } test "example in prompt" { var started = try std.time.Instant.now(); var i: usize = 0; while (i < 1000) { // zig fmt: off var calls = &[_]u7{0, 72, 3, 8, 59, 66, 61, 58, 23, 14, 16, 42, 10, 17, 2, 48, 44, 26, 70, 21, 31, 19, 9}; var cards = &[_][5][5]u7{ [5][5]u7{ [5]u7{67, 64, 11, 28, 16}, [5]u7{63, 26, 20, 15, 10}, [5]u7{68, 44, 00, 53, 70}, [5]u7{22, 56, 38, 51, 09}, [5]u7{47, 33, 17, 39, 59}, }, [5][5]u7{ [5]u7{51, 24, 53, 70, 62}, [5]u7{54, 44, 57, 72, 35}, [5]u7{32, 05, 00, 20, 38}, [5]u7{36, 04, 73, 29, 69}, [5]u7{63, 42, 07, 08, 58}, } }; i += 1; try std.testing.expectEqual(entry(cards, calls), 0); } // zig fmt: on var ended = try std.time.Instant.now(); std.debug.print("took: {} ", .{ended.since(started)}); } pub fn main() anyerror!void { std.debug.print("It was as if an occult hand brought you to run this program as an application. It is a library, yet lack of zig expertise gently drew me towards creating a application project.", .{}); } |
post a comment