| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- const std = @import("std");
- pub const kShapeSize: u8 = 3;
- pub const Shape = struct {
- data: [9]u8,
- pub fn getCell(self: *const Shape, row: u8, col: u8) u8 {
- return self.data[row*kShapeSize + col];
- }
- pub fn setCell(self: *Shape, row: u8, col: u8, cell: u8) void {
- self.data[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");
- }
- }
- };
|