const std = @import("std"); pub const kShapeSize: i32 = 3; pub const Shape = struct { data: [9]u8, pub fn getCell(self: *const Shape, row: i32, col: i32) u8 { return self.data[@abs(row*kShapeSize + col)]; } pub fn setCell(self: *Shape, row: i32, col: i32, cell: u8) void { self.data[@abs(row*kShapeSize + col)] = cell; } pub fn flipHor(self: *Shape) void { for (0 .. kShapeSize) |row| { for (0 .. kShapeSize/2) |col| { const t = self.getCell(@intCast(row), @intCast(col)); self.setCell(@intCast(row), @intCast(col), self.getCell(@intCast(row), @intCast(kShapeSize - 1 - col))); self.setCell(@intCast(row), @intCast(kShapeSize - 1 - col), t); } } } pub fn flipVer(self: *Shape) void { for (0 .. kShapeSize) |col| { for (0 .. kShapeSize/2) |row| { const t = self.getCell(@intCast(row), @intCast(col)); self.setCell(@intCast(row), @intCast(col), self.getCell(@intCast(kShapeSize - 1 - row), @intCast(col))); self.setCell(@intCast(kShapeSize - 1 - row), @intCast(col), t); } } } pub fn rotate(self: *Shape) void { for (0 .. kShapeSize-1) |row| { for (row+1 .. kShapeSize) |col| { const t = self.getCell(@intCast(row), @intCast(col)); self.setCell(@intCast(row), @intCast(col), self.getCell(@intCast(col), @intCast(row))); self.setCell(@intCast(col), @intCast(row), t); } } self.flipHor(); } pub fn getAllTransforms(self: * const Shape, allocator: std.mem.Allocator) !std.AutoHashMap(Shape, bool) { var allTransforms = std.AutoHashMap(Shape, bool).init(allocator); try allTransforms.put(self.*, true); var shapeHFlip = self.*; shapeHFlip.flipHor(); for (0 .. 4) |_| { try allTransforms.put(shapeHFlip, true); shapeHFlip.rotate(); } var shapeVFlip = self.*; shapeVFlip.flipVer(); for (0 .. 4) |_| { try allTransforms.put(shapeVFlip, true); shapeVFlip.rotate(); } var shapeHVFlip = self.*; shapeHVFlip.flipHor(); shapeHVFlip.flipVer(); for (0 .. 4) |_| { try allTransforms.put(shapeHVFlip, true); shapeHVFlip.rotate(); } var shapeVHFlip = self.*; shapeVHFlip.flipVer(); shapeVHFlip.flipHor(); for (0 .. 4) |_| { try allTransforms.put(shapeVHFlip, true); shapeVHFlip.rotate(); } return allTransforms; } pub fn print(self: *const Shape, allocator: std.mem.Allocator) !void { for (0..kShapeSize) |row| { for (0..kShapeSize) |col| { const cell = self.getCell(@intCast(row), @intCast(col)); try std.fs.File.stdout().writeAll( try std.fmt.allocPrint(allocator, "{c}", .{cell})); } try std.fs.File.stdout().writeAll("\n"); } } };