Skip to content

Commit

Permalink
Refactor solve functions to utilize Parts enum
Browse files Browse the repository at this point in the history
- Introduce `Parts` enum in `utils.zig` to standardize mode handling across different modules
- Refactor `solve` functions in `day01.zig` and `day02.zig` to utilize the `Parts` enum, improving modularity and readability
- Update main and test functions in `day01.zig` and `day02.zig` to integrate changes with the new `Parts` enum usage
  • Loading branch information
thekorn committed Dec 3, 2024
1 parent 9492f28 commit 97fbac4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
41 changes: 16 additions & 25 deletions src/day01.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const utils = @import("./utils.zig");
const Parts = utils.Parts;

const Values = struct {
left: []u32,
Expand Down Expand Up @@ -35,9 +36,9 @@ const Values = struct {
}
};

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

var i: usize = 0;
while (true) : (i += 1) {
Expand All @@ -47,36 +48,26 @@ fn part1(content: []const u8) !u32 {
const x = values.left[i];
const y = values.right[i];

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

return result;
}

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

var i: usize = 0;
while (true) : (i += 1) {
if (i >= values.left.len or i >= values.right.len) {
break;
switch (part) {
Parts.one => {
const diff: i64 = @as(i64, y) - @as(i64, x);
result += @intCast(@abs(diff));
},
Parts.two => {
const factor: u32 = @intCast(values.counter.get(x) orelse 0);
result += factor * x;
},
}
const x = values.left[i];

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

return result;
}

pub fn main() !void {
const content = @embedFile("./data/day01.txt");
const result1 = try part1(content);
const result1 = try solve(content, Parts.one);
utils.print("Result day 1 - part 1: {any}\n", .{result1});
const result2 = try part2(content);
const result2 = try solve(content, Parts.two);
utils.print("Result day 1 - part 2: {any}\n", .{result2});
}

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

Expand All @@ -102,6 +93,6 @@ test "day01 -> part2" {
\\3 9
\\3 3
;
const result = try part2(content);
const result = try solve(content, Parts.two);
try std.testing.expectEqual(@as(u32, 31), result);
}
22 changes: 12 additions & 10 deletions src/day02.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const std = @import("std");

const utils = @import("./utils.zig");
const Parts = utils.Parts;

const Line = struct {
items: []u32,
Expand Down Expand Up @@ -60,7 +62,7 @@ const Line = struct {
}
};

fn solve(content: []const u8, allow_skip: bool) !u32 {
fn solve(content: []const u8, part: Parts) !u32 {
var safeCount: u32 = 0;
var lineIter = std.mem.tokenizeSequence(u8, content, "\n");

Expand All @@ -71,16 +73,16 @@ fn solve(content: []const u8, allow_skip: bool) !u32 {
try items.append(try utils.parseInt(u32, cell, 10));
}
var ln = Line{ .items = try items.toOwnedSlice() };
if (try ln.is_valid(allow_skip)) safeCount += 1;
if (try ln.is_valid(part == Parts.two)) safeCount += 1;
}
return safeCount;
}

pub fn main() !void {
const content = @embedFile("./data/day02.txt");
const result1 = try solve(content, false);
const result1 = try solve(content, Parts.one);
utils.print("Result day 2 - part 2: {any}\n", .{result1});
const result2 = try solve(content, true);
const result2 = try solve(content, Parts.two);
utils.print("Result day 2 - part 2: {any}\n", .{result2});
}

Expand All @@ -93,7 +95,7 @@ test "day02 -> part1" {
\\8 6 4 4 1
\\1 3 6 7 9
;
const result = try solve(content, false);
const result = try solve(content, Parts.one);
try std.testing.expectEqual(@as(u32, 2), result);
}

Expand All @@ -114,7 +116,7 @@ test "day02 -> part2" {
\\8 6 4 4 1
\\1 3 6 7 9
;
const result = try solve(content, true);
const result = try solve(content, Parts.two);
try std.testing.expectEqual(@as(u32, 4), result);
}

Expand All @@ -125,7 +127,7 @@ test "day02 -> part2.1" {
\\1 20 3 4 5
\\1 20 19 20 17
;
const result = try solve(content, true);
const result = try solve(content, Parts.two);
try std.testing.expectEqual(@as(u32, 3), result);
}

Expand All @@ -134,7 +136,7 @@ test "day02 -> part2.2" {
\\1 2 3 4 5
\\20 20 19 18 17
;
const result = try solve(content, true);
const result = try solve(content, Parts.two);
try std.testing.expectEqual(@as(u32, 2), result);
}

Expand All @@ -143,14 +145,14 @@ test "day02 -> part2.3" {
\\1 2 3 4 5
\\1 5 6 7 8
;
const result = try solve(content, true);
const result = try solve(content, Parts.two);
try std.testing.expectEqual(@as(u32, 2), result);
}

test "day02 -> part2.4" {
const content =
\\24 25 24 23 21 19 18 17
;
const result = try solve(content, true);
const result = try solve(content, Parts.two);
try std.testing.expectEqual(@as(u32, 1), result);
}
2 changes: 2 additions & 0 deletions src/utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub const sort = std.sort.block;
pub const asc = std.sort.asc;
pub const desc = std.sort.desc;

pub const Parts = enum { one, two };

pub fn charCode(c: u8) usize {
if (c >= 97 and c <= 122) {
return c - 'a';
Expand Down

0 comments on commit 97fbac4

Please sign in to comment.