Day 6. Cheating a bit. Not bothering to try to parse a file with the data, just putting it in variables. And there's an off-by-one error at the end Im just going to ignore

master
Dustin Swan 1 year ago
parent 5ba7e6e4e1
commit c40831a68b
Signed by: dustinswan
GPG Key ID: AB49BD6B2B3A6377

@ -0,0 +1,2 @@
Time: 50 74 86 85
Distance: 242 1017 1691 1252

@ -0,0 +1,64 @@
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
}

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200
Loading…
Cancel
Save