1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
c737a84703
unwash day06 or something
Some checks failed
Build and test / test (push) Failing after 2m34s
2023-12-06 16:31:54 +11:00
d848f3951b
wtf happened in day06 2023-12-06 16:31:50 +11:00

78
src/day06.zig Normal file
View File

@ -0,0 +1,78 @@
const std = @import("std");
const util = @import("util.zig");
const mem = std.mem;
pub fn main() !void {
const input = @embedFile("data/day06.txt");
const sln = try solve(util.gpa, input);
std.debug.print("{d}\n", .{sln.a});
std.debug.print("{d}\n", .{sln.b});
}
const Solution = struct {
a: usize,
b: usize,
};
const Race = struct {
time: usize,
distance: usize,
};
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
var result: usize = 1;
{
var it = util.splitLines(input);
const times = try util.parseIntsScalar(usize, alloc, it.next().?[10..], .{});
const distances = try util.parseIntsScalar(usize, alloc, it.next().?[10..], .{});
defer alloc.free(times);
defer alloc.free(distances);
for (times, distances) |time, minDistance| {
var winning: usize = 0;
for (0..time - 1) |speed| {
const distance = speed * (time - speed);
if (distance > minDistance) winning += 1;
}
result *= winning;
}
}
var result2: usize = 1;
{
var it = util.splitLines(input);
var td: [2]usize = undefined;
for (0..td.len) |i| {
const line = try mem.replaceOwned(u8, alloc, it.next().?[10..], " ", "");
defer alloc.free(line);
td[i] = try std.fmt.parseInt(usize, line, 10);
}
var winning2: usize = 0;
for (0..td[0] - 1) |speed| {
const distance = speed * (td[0] - speed);
if (distance > td[1]) winning2 += 1;
}
result2 *= winning2;
}
return .{ .a = result, .b = result2 };
}
test "silver" {
const input =
\\Time: 7 15 30
\\Distance: 9 40 200
;
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 288), sln.a);
}
test "gold" {
const input =
\\Time: 7 15 30
\\Distance: 9 40 200
;
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 71503), sln.b);
}