non brute force solution.

main
Tarik Hijstek 2 years ago
parent 0fe0f4fc9a
commit 8c8057fd3a

@ -1,7 +1,6 @@
const input = @embedFile("inputPart2.txt");
const std = @import("std");
pub fn main() !void {
//Data wrangling
@ -9,10 +8,7 @@ pub fn main() !void {
var busses: [68]u32 = std.mem.zeroes([68:0]u32);
var i: u32 = 0;
while(it.next())
|line|
:(i+=1)
{
while (it.next()) |line| : (i += 1) {
busses[i] = readNum(line);
}
@ -23,12 +19,8 @@ pub fn main() !void {
busTotal += 1;
}
const stepSize: u32 = 1;
var stepSize: u64 = 1;
var n: u64 = 1;
var found: u8 = 0;
//Algorithm
while(found != busTotal):(n+=stepSize){
for (busses, 0..) |bus, offset| {
//skip
@ -36,27 +28,15 @@ pub fn main() !void {
continue;
}
if(((n + offset) % bus) == 0){
found +=1;
}
}
if(found >= 7){
std.debug.print("tick {d} has {d} aligned!\n",.{n, found});
}
while (((n + offset) % bus) != 0) : (n += stepSize) {}
if(found == busTotal){
std.debug.print("Final tick {d}\n",.{n});
break;
}else{
found = 0;
}
stepSize = stepSize * bus;
}
std.debug.print("tick: {d}", .{n});
}
fn readNum(line: []const u8) u32 {
return std.fmt.parseInt(u32, line, 10) catch {
return 0;
};
}
Loading…
Cancel
Save