day01part2 ok

This commit is contained in:
Pascal Phelipot 2024-12-04 22:59:18 +01:00
parent e1f3ef6fc6
commit 4e13fe6e80
1 changed files with 20 additions and 0 deletions

View File

@ -31,9 +31,27 @@ fn solve(str_input: []const u8, allocator: std.mem.Allocator) !u128 {
total += (@max(right_item, left_item) - @min(right_item, left_item)); total += (@max(right_item, left_item) - @min(right_item, left_item));
} }
const res2 = solve_part2(left, right);
std.debug.print("Result part 2: {d}\n", .{res2});
return total; return total;
} }
fn solve_part2(first: ArrayList(u32), second: ArrayList(u32)) u128 {
var result: u128 = 0;
for (first.items) |n| {
var count: u128 = 0;
for (second.items) |r| {
if (n == r) {
count += 1;
}
}
result += n * count;
}
return result;
}
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, ' ');
if (parts.next()) |part| { if (parts.next()) |part| {
@ -86,3 +104,5 @@ test "day01_parse_list" {
try expect(std.mem.eql(u32, left.items, &[_]u32{ 3, 4, 2, 1, 3, 3 })); 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 })); try expect(std.mem.eql(u32, right.items, &[_]u32{ 4, 3, 5, 3, 9, 3 }));
} }
test "day01_part2" {}