Added second day in zig
This commit is contained in:
parent
3587c73fea
commit
2d8480491e
3 changed files with 72 additions and 0 deletions
31
2021/02/first.zig
Normal file
31
2021/02/first.zig
Normal file
|
@ -0,0 +1,31 @@
|
|||
const std = @import("std");
|
||||
|
||||
const example = @embedFile("example");
|
||||
const input = @embedFile("input");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
const result = try solve(input);
|
||||
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||
}
|
||||
|
||||
fn solve(puzzle: []const u8) !u64 {
|
||||
var it = std.mem.tokenize(u8, puzzle, "\n");
|
||||
var pos: u64 = 0;
|
||||
var depth: u64 = 0;
|
||||
while (it.next()) |line| {
|
||||
var it2 = std.mem.tokenize(u8, line, " ");
|
||||
const step = it2.next() orelse unreachable;
|
||||
const n: u64 = std.fmt.parseInt(u64, it2.next().?, 10) catch unreachable;
|
||||
switch (step[0]) {
|
||||
'f' => pos += n,
|
||||
'd' => depth += n,
|
||||
'u' => depth -= n,
|
||||
else => unreachable,
|
||||
}
|
||||
}
|
||||
return pos * depth;
|
||||
}
|
||||
|
||||
test "solve" {
|
||||
try std.testing.expectEqual(solve(example), 150);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue