Day 1 part 1!
This commit is contained in:
parent
46582ed853
commit
9c22642be4
2 changed files with 23 additions and 0 deletions
1
2020/zig/.gitignore
vendored
1
2020/zig/.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
zig-cache/
|
zig-cache/
|
||||||
zig-out/
|
zig-out/
|
||||||
|
data/
|
||||||
build_runner.zig
|
build_runner.zig
|
||||||
|
|
|
@ -12,7 +12,29 @@ const gpa = util.gpa;
|
||||||
const data = @embedFile("../data/day01.txt");
|
const data = @embedFile("../data/day01.txt");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
|
var it = tokenize(u8, data, "\n");
|
||||||
|
|
||||||
|
// Not sure if allocating a massive buffer and tracking the size is the right approach?
|
||||||
|
var buffer: [1000]u16 = undefined;
|
||||||
|
var i: usize = 0;
|
||||||
|
while (it.next()) |line| {
|
||||||
|
const int = try parseInt(u16, line, 10);
|
||||||
|
buffer[i] = int;
|
||||||
|
// Can we use continue expression?
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
var answer: u32 = 0;
|
||||||
|
outer: for (buffer[0..i]) |a, j| {
|
||||||
|
for (buffer[(j+1)..i]) |b| {
|
||||||
|
if (a + b == 2020) {
|
||||||
|
answer = @as(u32, a) * b;
|
||||||
|
break :outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
print("{d}\n", .{answer});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Useful stdlib functions
|
// Useful stdlib functions
|
||||||
|
|
Loading…
Reference in a new issue