You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.9 KiB
Zig
70 lines
1.9 KiB
Zig
const input = @embedFile("input2");
|
|
const std = @import("std");
|
|
|
|
pub fn main() !void{
|
|
|
|
var horizontal: usize = 0;
|
|
var depth: usize = 0;
|
|
var aim: usize = 0;
|
|
|
|
var iter = std.mem.tokenize(u8, input, "\n "); // You can split on 2 delimeters, by adding them to the string
|
|
|
|
while(iter.next())|direction|{
|
|
var value = try std.fmt.parseInt(usize, iter.next().?, 10);
|
|
|
|
if(std.mem.eql(u8, direction, "up")){ aim -= value; }
|
|
if(std.mem.eql(u8, direction, "down")){ aim += value; }
|
|
if(std.mem.eql(u8, direction, "forward")){
|
|
horizontal += value;
|
|
depth += (aim * value);
|
|
}
|
|
}
|
|
|
|
std.debug.print("result: {}", .{depth * horizontal});
|
|
}
|
|
|
|
test "Part 1"{
|
|
|
|
var horizontal: usize = 0;
|
|
var depth: usize = 0;
|
|
|
|
var iter = std.mem.tokenize(u8, input, "\n "); // You can split on 2 delimeters, by adding them to the string
|
|
|
|
while(iter.next())|direction|{
|
|
var value = try std.fmt.parseInt(usize, iter.next().?, 10);
|
|
|
|
if(std.mem.eql(u8, direction, "forward")){ horizontal += value; }
|
|
if(std.mem.eql(u8, direction, "up")){ depth -= value; }
|
|
if(std.mem.eql(u8, direction, "down")){ depth += value; }
|
|
|
|
}
|
|
|
|
|
|
std.testing.expect(1762050 == (depth * horizontal));
|
|
}
|
|
|
|
|
|
|
|
|
|
test "part 2"{
|
|
|
|
var horizontal: usize = 0;
|
|
var depth: usize = 0;
|
|
var aim: usize = 0;
|
|
|
|
var iter = std.mem.tokenize(u8, input, "\n "); // You can split on 2 delimeters, by adding them to the string
|
|
|
|
//First token is the direction
|
|
while(iter.next())|direction|{
|
|
var value = try std.fmt.parseInt(usize, iter.next().?, 10);
|
|
|
|
if(std.mem.eql(u8, direction, "up")){ aim -= value; }
|
|
if(std.mem.eql(u8, direction, "down")){ aim += value; }
|
|
if(std.mem.eql(u8, direction, "forward")){
|
|
horizontal += value;
|
|
depth += (aim * value);
|
|
}
|
|
}
|
|
|
|
std.testing.expect(1855892637 == (depth * horizontal));
|
|
} |