-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.zig
61 lines (51 loc) · 1.58 KB
/
day02.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const std = @import("std");
const assert = std.debug.assert;
const print = std.debug.print;
const eql = std.mem.eql;
const indexOf = std.mem.indexOf;
const parseInt = std.fmt.parseInt;
const REAL_INPUT = @embedFile("inputs/day02.txt");
pub fn main() !void {
try part1(REAL_INPUT);
try part2(REAL_INPUT);
}
fn part1(input: []const u8) !void {
var lines = std.mem.split(u8, input, "\n");
var depth: i64 = 0;
var pos: i64 = 0;
while (lines.next()) |line| {
const space = indexOf(u8, line, " ").?;
const dir = line[0..space];
const delta = try parseInt(i64, line[space + 1..line.len], 10);
if (eql(u8, dir, "up")) {
depth -= delta;
} else if (eql(u8, dir, "down")) {
depth += delta;
} else {
assert(eql(u8, dir, "forward"));
pos += delta;
}
}
print("Part 1: {} x {} = {}\n", .{depth, pos, depth * pos});
}
fn part2(input: []const u8) !void {
var lines = std.mem.split(u8, input, "\n");
var aim: i64 = 0;
var depth: i64 = 0;
var pos: i64 = 0;
while (lines.next()) |line| {
const space = indexOf(u8, line, " ").?;
const dir = line[0..space];
const delta = try parseInt(i64, line[space + 1..line.len], 10);
if (eql(u8, dir, "up")) {
aim -= delta;
} else if (eql(u8, dir, "down")) {
aim += delta;
} else {
assert(eql(u8, dir, "forward"));
pos += delta;
depth += aim * delta;
}
}
print("Part 2: {} x {} = {}\n", .{depth, pos, depth * pos});
}