Added benchmark

This commit is contained in:
2024-12-05 01:59:32 +01:00
parent d153b44e1e
commit 62233df3e5
10 changed files with 335 additions and 62 deletions
+78
View File
@@ -0,0 +1,78 @@
const std = @import("std");
const print = std.debug.print;
const LookingFor = enum {
m,
u,
l,
l_brace,
X,
Y,
};
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
pub fn main() !void {
const file = try std.fs.cwd().openFile("day3/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
try std.testing.expectEqual(190604937, try parse(file_buf[0..len]));
}
fn parse(input: []const u8) !u32 {
var state: LookingFor = .m;
var total: u32 = 0;
var intX_len: u8 = 0;
var intY_len: u8 = 0;
for (input, 0..) |c, i| switch (state) {
.m => switch (c) {
'm' => state = .u,
else => continue,
},
.u => switch (c) {
'u' => state = .l,
else => state = .m,
},
.l => switch (c) {
'l' => state = .l_brace,
else => state = .m,
},
.l_brace => switch (c) {
'(' => {
state = .X;
intX_len = 0;
intY_len = 0;
},
else => state = .m,
},
.X => switch (c) {
',' => state = if (intX_len > 0) .Y else .m,
'0'...'9' => if (intX_len == 3) {
state = .m;
} else {
intX_len += 1;
},
else => state = .m,
},
.Y => switch (c) {
')' => {
state = .m;
if (intY_len > 0) {
const x = try std.fmt.parseInt(u32, input[i - (intX_len + intY_len + 1) .. i - intY_len - 1], 10);
const y = try std.fmt.parseInt(u32, input[i - intY_len .. i], 10);
total += x * y;
}
},
'0'...'9' => {
if (intY_len == 3) {
state = .m;
continue;
}
intY_len += 1;
},
else => state = .m,
},
};
return total;
}
+5 -12
View File
@@ -19,27 +19,20 @@ const LookingFor = enum {
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
pub fn main() !void {
const time_start = std.time.microTimestamp();
const file = try std.fs.cwd().openFile("day3/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
var total: u32 = 0;
var enable = true;
total += try parse(file_buf[0..len], &enable);
const time_end = std.time.microTimestamp();
print("Total time: {d}μs\n", .{time_end - time_start});
print("Total safe: {d}\n", .{total});
try std.testing.expectEqual(82857512, try parse(file_buf[0..len]));
}
fn parse(input: []const u8, enable: *bool) !u32 {
fn parse(input: []const u8) !u32 {
var state: LookingFor = .m_or_d;
var total: u32 = 0;
var intX_len: u8 = 0;
var intY_len: u8 = 0;
var enable = true;
var enable_buff: bool = true;
for (input, 0..) |c, i| switch (state) {
.m_or_d => switch (c) {
@@ -89,7 +82,7 @@ fn parse(input: []const u8, enable: *bool) !u32 {
},
.r_brace_do => switch (c) {
')' => {
enable.* = enable_buff;
enable = enable_buff;
enable_buff = true;
state = .m_or_d;
},
@@ -108,7 +101,7 @@ fn parse(input: []const u8, enable: *bool) !u32 {
')' => {
state = .m_or_d;
if (intY_len > 0) {
if (!enable.*) continue;
if (!enable) continue;
const x = try std.fmt.parseInt(u32, input[i - (intX_len + intY_len + 1) .. i - intY_len - 1], 10);
const y = try std.fmt.parseInt(u32, input[i - intY_len .. i], 10);
total += x * y;