Skip to content

Commit

Permalink
Refactor integer types for improved capacity
Browse files Browse the repository at this point in the history
- Refactor integer data types across various structs and functions for improved type safety and to accommodate larger values.
- Update arithmetic operations to use appropriate casting, ensuring accuracy with larger integer calculations.
- Adjust related unit tests to align with the updated data types and expected results.
  • Loading branch information
thekorn committed Dec 2, 2024
1 parent 743d9a0 commit e8da024
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
39 changes: 20 additions & 19 deletions src/day01.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ const std = @import("std");
const utils = @import("./utils.zig");

const Values = struct {
left: []i32,
right: []i32,
counter: utils.Counter(i32),
left: []u32,
right: []u32,
counter: utils.Counter(u32),

fn init(alloc: utils.Allocator, content: []const u8) !Values {
var readIter = std.mem.tokenizeSequence(u8, content, "\n");

var left = utils.List(i32).init(alloc);
var right = utils.List(i32).init(alloc);
var left = utils.List(u32).init(alloc);
var right = utils.List(u32).init(alloc);

var counter = try utils.Counter(i32).init(alloc);
var counter = try utils.Counter(u32).init(alloc);

while (readIter.next()) |line| {
var lineIter = std.mem.tokenizeSequence(u8, line, " ");
try left.append(try utils.parseInt(i32, lineIter.next().?, 10));
try left.append(try utils.parseInt(u32, lineIter.next().?, 10));

const r = try utils.parseInt(i32, lineIter.next().?, 10);
const r = try utils.parseInt(u32, lineIter.next().?, 10);
try counter.add(r);
try right.append(r);
}
const l = try left.toOwnedSlice();
std.mem.sort(i32, l, {}, comptime std.sort.asc(i32));
std.mem.sort(u32, l, {}, comptime std.sort.asc(u32));
const r = try right.toOwnedSlice();
std.mem.sort(i32, r, {}, comptime std.sort.asc(i32));
std.mem.sort(u32, r, {}, comptime std.sort.asc(u32));

return .{
.left = l,
Expand All @@ -35,8 +35,8 @@ const Values = struct {
}
};

fn part1(content: []const u8) !i32 {
var result: i32 = 0;
fn part1(content: []const u8) !u32 {
var result: u32 = 0;
const values = try Values.init(utils.gpa, content);

var i: usize = 0;
Expand All @@ -47,14 +47,15 @@ fn part1(content: []const u8) !i32 {
const x = values.left[i];
const y = values.right[i];

result += @intCast(@abs(y - x));
const diff: i64 = @as(i64, y) - @as(i64, x);
result += @intCast(@abs(diff));
}

return result;
}

fn part2(content: []const u8) !i32 {
var result: i32 = 0;
fn part2(content: []const u8) !u32 {
var result: u32 = 0;
var values = try Values.init(utils.gpa, content);

var i: usize = 0;
Expand All @@ -64,8 +65,8 @@ fn part2(content: []const u8) !i32 {
}
const x = values.left[i];

const factor: i32 = values.counter.get(x) orelse 0;
result += @intCast(@abs(factor * x));
const factor: u32 = @intCast(values.counter.get(x) orelse 0);
result += factor * x;
}

return result;
Expand All @@ -89,7 +90,7 @@ test "day01 -> part1" {
\\3 3
;
const result = try part1(content);
try std.testing.expectEqual(@as(i32, 11), result);
try std.testing.expectEqual(@as(u32, 11), result);
}

test "day01 -> part2" {
Expand All @@ -102,5 +103,5 @@ test "day01 -> part2" {
\\3 3
;
const result = try part2(content);
try std.testing.expectEqual(@as(i32, 31), result);
try std.testing.expectEqual(@as(u32, 31), result);
}
8 changes: 4 additions & 4 deletions src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ pub fn codeToChar(code: usize) u8 {
pub fn Counter(comptime K: type) type {
return struct {
const Self = @This();
items: Map(K, i32),
items: Map(K, usize),

pub fn init(alloc: Allocator) !Self {
return .{
.items = Map(K, i32).init(alloc),
.items = Map(K, usize).init(alloc),
};
}

Expand All @@ -74,14 +74,14 @@ pub fn Counter(comptime K: type) type {
}
}

pub fn get(self: *Self, key: K) ?i32 {
pub fn get(self: *Self, key: K) ?usize {
return self.items.get(key);
}
};
}

test "utils -> Counter" {
const CounterStr = Counter(i32);
const CounterStr = Counter(u32);
var counter = try CounterStr.init(gpa);
try counter.add(1);
try counter.add(1);
Expand Down

0 comments on commit e8da024

Please sign in to comment.