d01part01 ok

This commit is contained in:
Pascal Phelipot 2024-12-04 22:45:20 +01:00
parent ac8752045c
commit e1f3ef6fc6
2 changed files with 38 additions and 16 deletions

View File

@ -2,13 +2,18 @@ const std = @import("std");
const testing = std.testing; const testing = std.testing;
const expect = testing.expect; const expect = testing.expect;
const ArrayList = std.ArrayList; const ArrayList = std.ArrayList;
const utils = @import("utils.zig");
pub fn main() !void { pub fn main() !void {
//const allocator = std.heap.page_allocator; const allocator = std.heap.page_allocator;
std.debug.print("Hello, {s}!\n", .{"World"}); const input_txt = try utils.load_dataset("./datasets/day01.txt", allocator);
defer allocator.free(input_txt);
const result = try solve(input_txt, allocator);
std.debug.print("Result: {d}", .{result});
} }
fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u32 { fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u128 {
const left, const right = try get_list(str_input, allocator); const left, const right = try get_list(str_input, allocator);
defer { defer {
left.deinit(); left.deinit();
@ -18,15 +23,12 @@ fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u32 {
std.mem.sort(u32, left.items, {}, comptime std.sort.asc(u32)); std.mem.sort(u32, left.items, {}, comptime std.sort.asc(u32));
std.mem.sort(u32, right.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); try expect(left.items.len == right.items.len);
var total: u32 = 0; var total: u128 = 0;
for (0..left.items.len) |index| { for (0..left.items.len) |index| {
const left_item = left.items[index]; const left_item: u64 = left.items[index];
const right_item = right.items[index]; const right_item: u64 = right.items[index];
total += (right_item - left_item); total += (@max(right_item, left_item) - @min(right_item, left_item));
} }
return total; return total;
@ -34,9 +36,14 @@ fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u32 {
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); if (parts.next()) |part| {
const right = try std.fmt.parseInt(u32, parts.next().?, 10); const left = try std.fmt.parseInt(u32, part, 10);
return .{ left, right }; if (parts.next()) |part2| {
const right = try std.fmt.parseInt(u32, part2, 10);
return .{ left, right };
}
}
return error.LineIncomplete;
} }
fn get_list(str_input: []const u8, allocator: std.mem.Allocator) !struct { ArrayList(u32), ArrayList(u32) } { fn get_list(str_input: []const u8, allocator: std.mem.Allocator) !struct { ArrayList(u32), ArrayList(u32) } {
@ -45,9 +52,11 @@ fn get_list(str_input: []const u8, allocator: std.mem.Allocator) !struct { Array
var lines = std.mem.splitSequence(u8, str_input, "\n"); var lines = std.mem.splitSequence(u8, str_input, "\n");
while (lines.next()) |line| { while (lines.next()) |line| {
const a, const b = try parse_line(line); if (parse_line(line)) |ret| {
try left.append(a); const a, const b = ret;
try right.append(b); try left.append(a);
try right.append(b);
} else |_| {}
} }
return .{ left, right }; return .{ left, right };

View File

@ -0,0 +1,13 @@
const std = @import("std");
pub fn load_dataset(dataset_path: []const u8, allocator: std.mem.Allocator) ![]u8 {
var file = try std.fs.cwd().openFile(dataset_path, .{});
defer file.close();
const file_size = try file.getEndPos();
const buffer = try allocator.alloc(u8, file_size);
_ = try file.readAll(buffer);
return buffer;
}