From 0fe0f4fc9a8e00d54b668fa3c0dddb8b9219ec78 Mon Sep 17 00:00:00 2001 From: Tarik Hijstek Date: Thu, 28 Sep 2023 13:33:13 +0200 Subject: [PATCH] day13 part 2 brute force --- 2020/day13/inputPart2.txt | 68 ++++++++++++++++++++++++++++++++++ 2020/day13/inputPart2Test1.txt | 4 ++ 2020/day13/inputPart2Test2.txt | 5 +++ 2020/day13/inputPart2Test3.txt | 5 +++ 2020/day13/mainPart2.zig | 62 +++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 2020/day13/inputPart2.txt create mode 100644 2020/day13/inputPart2Test1.txt create mode 100644 2020/day13/inputPart2Test2.txt create mode 100644 2020/day13/inputPart2Test3.txt create mode 100644 2020/day13/mainPart2.zig diff --git a/2020/day13/inputPart2.txt b/2020/day13/inputPart2.txt new file mode 100644 index 0000000..bc38b2e --- /dev/null +++ b/2020/day13/inputPart2.txt @@ -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 \ No newline at end of file diff --git a/2020/day13/inputPart2Test1.txt b/2020/day13/inputPart2Test1.txt new file mode 100644 index 0000000..7c7cd06 --- /dev/null +++ b/2020/day13/inputPart2Test1.txt @@ -0,0 +1,4 @@ +17 +0 +13 +19 \ No newline at end of file diff --git a/2020/day13/inputPart2Test2.txt b/2020/day13/inputPart2Test2.txt new file mode 100644 index 0000000..af1f98a --- /dev/null +++ b/2020/day13/inputPart2Test2.txt @@ -0,0 +1,5 @@ +67 +7 +0 +59 +61 \ No newline at end of file diff --git a/2020/day13/inputPart2Test3.txt b/2020/day13/inputPart2Test3.txt new file mode 100644 index 0000000..af1f98a --- /dev/null +++ b/2020/day13/inputPart2Test3.txt @@ -0,0 +1,5 @@ +67 +7 +0 +59 +61 \ No newline at end of file diff --git a/2020/day13/mainPart2.zig b/2020/day13/mainPart2.zig new file mode 100644 index 0000000..6c38119 --- /dev/null +++ b/2020/day13/mainPart2.zig @@ -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; + }; + +} \ No newline at end of file