You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.5 KiB
Zig

const std = @import("std");
pub fn main() !void {
// const times = [_]u8{ 7, 15, 30 };
// const distances = [_]u8{ 9, 40, 200 };
const times = [_]u8{ 50, 74, 86, 85 };
const distances = [_]u16{ 242, 1017, 1691, 1252 };
// Part 1
var totals: u64 = 1;
for (times, 0..) |time, idx| {
var waysToWin: u8 = 0;
for (0..time) |ms| {
var dist: u16 = 0;
for (0..time - ms) |_| {
dist += @intCast(ms);
}
if (dist > distances[idx]) {
waysToWin += 1;
}
}
totals *= waysToWin;
}
std.debug.print("Part 1: {d}\n", .{totals});
// Part 2
// const time = 71530;
// const distance = 940200;
const time = 50748685;
const distance = 242101716911252;
var pivot: u32 = time / 2;
var max: u32 = pivot;
var min: u32 = 0;
var prev: u32 = pivot;
var i: u8 = 0;
while (true) : (i += 1) {
var dist: u64 = 0;
for (0..(time - pivot)) |_| {
dist += pivot;
}
if (dist > distance) {
max = pivot;
pivot -= (max - min) / 2;
} else {
min = pivot;
pivot += (max - min) / 2;
}
if (prev == pivot) {
break;
}
prev = pivot;
}
const num = time - (pivot * 2);
std.debug.print("Part 2: {}\n", .{num});
// TODO: potentially off by one. Could actually figure it out or just try num + 1 in AoC
}