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 expect = testing.expect;
const ArrayList = std.ArrayList;
const utils = @import("utils.zig");
pub fn main() !void {
//const allocator = std.heap.page_allocator;
std.debug.print("Hello, {s}!\n", .{"World"});
const allocator = std.heap.page_allocator;
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);
defer {
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, 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;
var total: u128 = 0;
for (0..left.items.len) |index| {
const left_item = left.items[index];
const right_item = right.items[index];
total += (right_item - left_item);
const left_item: u64 = left.items[index];
const right_item: u64 = right.items[index];
total += (@max(right_item, left_item) - @min(right_item, left_item));
}
return total;
@ -34,10 +36,15 @@ fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u32 {
fn parse_line(line: []const u8) !struct { u32, u32 } {
var parts = std.mem.tokenizeScalar(u8, line, ' ');
const left = try std.fmt.parseInt(u32, parts.next().?, 10);
const right = try std.fmt.parseInt(u32, parts.next().?, 10);
if (parts.next()) |part| {
const left = try std.fmt.parseInt(u32, part, 10);
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) } {
var left = ArrayList(u32).init(allocator);
@ -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");
while (lines.next()) |line| {
const a, const b = try parse_line(line);
if (parse_line(line)) |ret| {
const a, const b = ret;
try left.append(a);
try right.append(b);
} else |_| {}
}
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;
}