const std = @import("std"); const path = "data/day07/input.txt"; const crabs = 1000; const CrabType = u11; pub fn parseInput() anyerror![crabs]CrabType { const in = @embedFile(path); const input = std.mem.trimRight(u8, in, "\r\n"); var crbs = std.mem.tokenize(u8, input, ","); var ret: [crabs]CrabType = undefined; var idx: usize = 0; while (crbs.next()) |crab| : (idx += 1) { const crab_pos = try std.fmt.parseUnsigned(CrabType, crab, 10); ret[idx] = crab_pos; } std.sort.sort(CrabType, &ret, {}, comptime std.sort.asc(CrabType)); return ret; } pub fn first(allocator: ?std.mem.Allocator) anyerror!usize { _ = allocator; const crbs = try parseInput(); const pivot = crbs[crabs / 2]; var fuel: usize = 0; for (crbs) |c| { if (c > pivot) { fuel += c - pivot; } else { fuel += pivot - c; } } return fuel; } pub fn main() anyerror!void { var timer = try std.time.Timer.start(); const ret = try first(null); const f = timer.lap() / 1000; try std.testing.expectEqual(ret, @as(usize, 335271)); std.debug.print("Day 7a result: {d} \t\ttime: {d}us\n", .{ ret, f }); } test "day07a" { try std.testing.expectEqual(@as(usize, 335271), try first(std.testing.allocator)); }