day13 part 2 brute force
parent
d1c4e55ac0
commit
0fe0f4fc9a
@ -0,0 +1,68 @@
|
|||||||
|
19
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
37
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
383
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
23
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
13
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
29
|
||||||
|
0
|
||||||
|
457
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
41
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0
|
||||||
|
17
|
||||||
@ -0,0 +1,4 @@
|
|||||||
|
17
|
||||||
|
0
|
||||||
|
13
|
||||||
|
19
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
67
|
||||||
|
7
|
||||||
|
0
|
||||||
|
59
|
||||||
|
61
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
67
|
||||||
|
7
|
||||||
|
0
|
||||||
|
59
|
||||||
|
61
|
||||||
@ -0,0 +1,62 @@
|
|||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue