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.

62 lines
1.2 KiB
Zig

const input = @embedFile("inputPart2.txt");
const std = @import("std");
pub fn main() !void {
//Data wrangling
var it = std.mem.tokenize(u8, input, "\n");
var busses:[68]u32 = std.mem.zeroes([68:0]u32);
var i: u32 = 0;
while(it.next())
|line|
:(i+=1)
{
busses[i] = readNum(line);
}
//count total active buslines
var busTotal: u8 = 0;
for(busses)|bus|{
if(bus > 0)
busTotal += 1;
}
const stepSize: u32 = 1;
var n: u64 = 1;
var found: u8 = 0;
//Algorithm
while(found != busTotal):(n+=stepSize){
for(busses, 0..)|bus, offset|{
//skip
if(bus == 0){
continue;
}
if(((n + offset) % bus) == 0){
found +=1;
}
}
if(found >= 7){
std.debug.print("tick {d} has {d} aligned!\n",.{n, found});
}
if(found == busTotal){
std.debug.print("Final tick {d}\n",.{n});
break;
}else{
found = 0;
}
}
}
fn readNum(line: []const u8) u32 {
return std.fmt.parseInt(u32, line, 10) catch {
return 0;
};
}