Parcourir la source

Converted to i32 for some calculations

Aleksei Dorokhov il y a 7 mois
Parent
commit
cd39576076
4 fichiers modifiés avec 39 ajouts et 35 suppressions
  1. 18 14
      day12/area.zig
  2. 9 9
      day12/day12a.zig
  3. 7 7
      day12/input.zig
  4. 5 5
      day12/shape.zig

+ 18 - 14
day12/area.zig

@@ -5,16 +5,16 @@ const Shape = shapeModule.Shape;
 const kShapeSize = shapeModule.kShapeSize;
 
 pub const Area = struct {
-    rows: u8 = 0,
-    cols: u8 = 0,
+    rows: i32 = 0,
+    cols: i32 = 0,
     data: []u8 = undefined,
     dataAllocator: std.mem.Allocator,
 
-    pub fn init(allocator: std.mem.Allocator, rows: u8, cols: u8) !Area {
+    pub fn init(allocator: std.mem.Allocator, rows: i32, cols: i32) !Area {
         var area: Area = .{
             .rows = rows,
             .cols = cols,
-            .data = try allocator.alloc(u8, rows*cols),
+            .data = try allocator.alloc(u8, @intCast(rows*cols)),
             .dataAllocator = allocator,
         };
 
@@ -32,7 +32,7 @@ pub const Area = struct {
         const area: Area = .{
             .rows = self.rows,
             .cols = self.cols,
-            .data = try allocator.alloc(u8, self.rows*self.cols),
+            .data = try allocator.alloc(u8, @intCast(self.rows*self.cols)),
             .dataAllocator = allocator,
         };
         std.mem.copyForwards(u8, area.data, self.data);
@@ -49,17 +49,19 @@ pub const Area = struct {
         return freeSize;
     }
 
-    pub fn getCell(self: *const Area, row: u8, col: u8) u8 {
-        return self.data[row*self.cols + col];
+    pub fn getCell(self: *const Area, row: i32, col: i32) u8 {
+        return self.data[@intCast(row*self.cols + col)];
     }
 
-    pub fn setCell(self: *Area, row: u8, col: u8, cell: u8) void {
-        self.data[row*self.cols + col] = cell;
+    pub fn setCell(self: *Area, row: i32, col: i32, cell: u8) void {
+        self.data[@intCast(row*self.cols + col)] = cell;
     }
 
     pub fn print(self: *const Area, allocator: std.mem.Allocator) !void {
-        for (0..self.rows) |row| {
-            for (0..self.cols) |col| {
+        const rows:usize = @intCast(self.rows);
+        const cols:usize = @intCast(self.cols);
+        for (0..rows) |row| {
+            for (0..cols) |col| {
                 const cell = self.getCell(@intCast(row), @intCast(col));
                 try std.fs.File.stdout().writeAll(
                   try std.fmt.allocPrint(allocator, "{c}", .{cell}));
@@ -70,7 +72,7 @@ pub const Area = struct {
     }
 
     pub fn appendShape(self: * const Area, allocator: std.mem.Allocator,
-                       shape: *const Shape, row: u8, col: u8, shapeSymbol: u8) !?Area {
+                       shape: *const Shape, row: i32, col: i32, shapeSymbol: u8) !?Area {
         var area = try self.copy(allocator);
         for (0..kShapeSize) |shapeRow| {
             for (0..kShapeSize) |shapeCol| {
@@ -78,13 +80,15 @@ pub const Area = struct {
                     continue;
                 }
 
-                const areaRow:u8 = @intCast(shapeRow + row);
+                var areaRow:i32 = @intCast(shapeRow);
+                areaRow += row;
                 if (areaRow >= area.rows) {
                     area.deinit();
                     return null;
                 }
 
-                const areaCol:u8 = @intCast(shapeCol + col);
+                var areaCol:i32 = @intCast(shapeCol);
+                areaCol += col;
                 if (areaCol >= area.cols) {
                     area.deinit();
                     return null;

+ 9 - 9
day12/day12a.zig

@@ -11,8 +11,8 @@ const readInputIntoString = inputModule.readInputIntoString;
 const parseInput = inputModule.parseInput;
 
 const Coord = struct {
-    row: u8,
-    col: u8,
+    row: i32,
+    col: i32,
 };
 
 pub fn getEligibleCoords(allocator: std.mem.Allocator,
@@ -20,8 +20,8 @@ pub fn getEligibleCoords(allocator: std.mem.Allocator,
                          eligibleCoords: *std.ArrayList(Coord)) !void {
     eligibleCoords.clearRetainingCapacity();
 
-    for (0 .. area.rows) |row| {
-        for (0 .. area.cols) |col|  {
+    for (0 .. @intCast(area.rows)) |row| {
+        for (0 .. @intCast(area.cols)) |col|  {
             if (area.getCell(@intCast(row), @intCast(col)) == '.') {
                 var shouldAdd = false;
                 if (row > 0 and area.getCell(@intCast(row-1), @intCast(col)) != '.') {
@@ -63,18 +63,18 @@ pub fn calculateShapeSizes(allocator: std.mem.Allocator, shapeTransforms: []cons
     return shapeSizes;
 }
 
-pub fn calcShapesTotalSize(shapeCounts: []u8, shapeSizes: []usize) usize {
+pub fn calcShapesTotalSize(shapeCounts: []i32, shapeSizes: []usize) usize {
     var totalSize: usize = 0;
 
     for (0..shapeCounts.len) |i| {
-        totalSize += shapeSizes[i] * shapeCounts[i];
+        totalSize += shapeSizes[i] * @as(usize, @abs(shapeCounts[i]));
     }
     return totalSize;
 }
 
 
 pub fn canContainShapes(allocator: std.mem.Allocator, area: Area,
-                        shapeCounts: []u8, shapeTransforms: []const []const Shape,
+                        shapeCounts: []i32, shapeTransforms: []const []const Shape,
                         shapeSizes: []usize) !bool {
     // try std.fs.File.stdout().writeAll("canContainShapes: [");
     // for (shapeCounts) |shapeCount| {
@@ -124,9 +124,9 @@ pub fn canContainShapes(allocator: std.mem.Allocator, area: Area,
                     // try newArea.print(allocator);
                     // try std.fs.File.stdout().writeAll("\n");
 
-                    var newShapeCounts = try allocator.alloc(u8, shapeCounts.len);
+                    var newShapeCounts = try allocator.alloc(i32, shapeCounts.len);
                     defer allocator.free(newShapeCounts);
-                    std.mem.copyForwards(u8, newShapeCounts, shapeCounts);
+                    std.mem.copyForwards(i32, newShapeCounts, shapeCounts);
                     newShapeCounts[shapeIndex] -= 1;
 
                     if (try canContainShapes(allocator, newArea, newShapeCounts,

+ 7 - 7
day12/input.zig

@@ -3,9 +3,9 @@ const shapeModule = @import("shape.zig");
 const Shape = shapeModule.Shape;
 
 pub const AreaRequirements = struct {
-    rows: u8,
-    cols: u8,
-    shapesCount: []u8,
+    rows: i32,
+    cols: i32,
+    shapesCount: []i32,
 };
 
 pub const Input = struct {
@@ -40,14 +40,14 @@ fn parseAreaRequirements(allocator: std.mem.Allocator, line: []const u8) !AreaRe
     const rowsText = sizeTextIt.next().?;
     const colsText = sizeTextIt.next().?;
 
-    const rows = try std.fmt.parseInt(u8, rowsText, 10);
-    const cols = try std.fmt.parseInt(u8, colsText, 10);
+    const rows = try std.fmt.parseInt(i32, rowsText, 10);
+    const cols = try std.fmt.parseInt(i32, colsText, 10);
 
     var shapesCountTextIt = std.mem.tokenizeScalar(u8, shapesCountText, ' ');
-    var shapesCount = std.ArrayList(u8).empty;
+    var shapesCount = std.ArrayList(i32).empty;
     while (shapesCountTextIt.next()) |shapeCountText| {
       const shapeCount = try shapesCount.addOne(allocator);
-      shapeCount.* = try std.fmt.parseInt(u8, shapeCountText, 10);
+      shapeCount.* = try std.fmt.parseInt(i32, shapeCountText, 10);
     }
     
     const area: AreaRequirements = .{

+ 5 - 5
day12/shape.zig

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