Basic stuff

This commit is contained in:
Pascal Phelipot 2024-12-02 01:37:00 +01:00
commit 0547fa5a1a
3 changed files with 1061 additions and 0 deletions

1000
datasets/day01.txt Normal file

File diff suppressed because it is too large Load Diff

61
src/day01.zig Normal file
View File

@ -0,0 +1,61 @@
const std = @import("std");
const testing = std.testing;
const expect = testing.expect;
const ArrayList = std.ArrayList;
pub fn main() !void {
//const allocator = std.heap.page_allocator;
std.debug.print("Hello, {s}!\n", .{"World"});
}
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);
return .{ left, right };
}
fn get_list(str_input: []const u8, allocator: std.mem.Allocator) !struct { ArrayList(u32), ArrayList(u32) } {
var left = ArrayList(u32).init(allocator);
var right = ArrayList(u32).init(allocator);
var lines = std.mem.splitSequence(u8, str_input, "\n");
while (lines.next()) |line| {
const a, const b = try parse_line(line);
try left.append(a);
try right.append(b);
}
return .{ left, right };
}
const test_allocator = std.testing.allocator;
const input = "3 4\n4 3\n2 5\n1 3\n3 9\n3 3";
test "day01_given_example" {
const left, const right = try get_list(input, test_allocator);
defer {
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" {
const left, const right = try parse_line("3 4");
try expect(left == 3);
try expect(right == 4);
}
test "day01_parse_list" {
const left, const right = try get_list(input, test_allocator);
defer {
left.deinit();
right.deinit();
}
try expect(std.mem.eql(u32, left.items, &[_]u32{ 3, 4, 2, 1, 3, 3 }));
try expect(std.mem.eql(u32, right.items, &[_]u32{ 4, 3, 5, 3, 9, 3 }));
}

0
src/utils.zig Normal file
View File