This commit is contained in:
Pascal Phelipot 2024-12-03 20:43:12 +01:00
parent 0547fa5a1a
commit ac8752045c
1 changed files with 26 additions and 8 deletions

View File

@ -8,6 +8,30 @@ pub fn main() !void {
std.debug.print("Hello, {s}!\n", .{"World"}); std.debug.print("Hello, {s}!\n", .{"World"});
} }
fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u32 {
const left, const right = try get_list(str_input, allocator);
defer {
left.deinit();
right.deinit();
}
std.mem.sort(u32, left.items, {}, comptime std.sort.asc(u32));
std.mem.sort(u32, right.items, {}, comptime std.sort.asc(u32));
try expect(std.mem.eql(u32, left.items, &[_]u32{ 1, 2, 3, 3, 3, 4 }));
try expect(std.mem.eql(u32, right.items, &[_]u32{ 3, 3, 3, 4, 5, 9 }));
try expect(left.items.len == right.items.len);
var total: u32 = 0;
for (0..left.items.len) |index| {
const left_item = left.items[index];
const right_item = right.items[index];
total += (right_item - left_item);
}
return total;
}
fn parse_line(line: []const u8) !struct { u32, u32 } { fn parse_line(line: []const u8) !struct { u32, u32 } {
var parts = std.mem.tokenizeScalar(u8, line, ' '); var parts = std.mem.tokenizeScalar(u8, line, ' ');
const left = try std.fmt.parseInt(u32, parts.next().?, 10); const left = try std.fmt.parseInt(u32, parts.next().?, 10);
@ -33,14 +57,8 @@ const test_allocator = std.testing.allocator;
const input = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3"; const input = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3";
test "day01_given_example" { test "day01_given_example" {
const left, const right = try get_list(input, test_allocator); const total = try solve(input, test_allocator);
defer { try expect(total == 11);
left.deinit();
right.deinit();
}
// const expected_distances = [_]u32{ 2, 1, 0, 1, 2, 5 };
// const expected_distance_total: u32 = 11;
try expect(false);
} }
test "day01_parse_line" { test "day01_parse_line" {