-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03_mull-it-over.zig
76 lines (71 loc) · 2.42 KB
/
day03_mull-it-over.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const std = @import("std");
pub fn main() !void {
const file = try std.fs.cwd().openFile("data/day03.txt", .{ .mode = .read_only });
defer file.close();
var buffered_reader = std.io.bufferedReader(file.reader());
const reader = buffered_reader.reader();
var sum: i32 = 0;
var is_enabled = true;
while (true) {
if (!is_enabled) {
_ = skip_until(reader, &[_][]const u8{"do()"}) catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};
is_enabled = true;
}
const token = skip_until(reader, &[_][]const u8{ "mul(", "don't()" }) catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
};
if (std.mem.eql(u8, token, "don't()")) {
is_enabled = false;
continue;
}
const number1 = parse_number(reader, ',') catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
} orelse continue;
const number2 = parse_number(reader, ')') catch |err| switch (err) {
error.EndOfStream => break,
else => return err,
} orelse continue;
sum += number1 * number2;
}
std.debug.print("The total is: {}", .{sum});
}
pub fn skip_until(reader: anytype, tokens: []const []const u8) ![]const u8 {
var index: u8 = 0;
var found: ?[]const u8 = null;
while (true) {
const byte = reader.readByte() catch |err| return err;
var is_active = false;
for (tokens) |token| {
if (index >= token.len) continue;
if (byte != token[index]) continue;
if (index == token.len - 1) {
found = token;
break;
}
is_active = true;
}
if (found != null) return found.?;
if (is_active) {
is_active = false;
index += 1;
} else index = 0;
}
}
pub fn parse_number(reader: anytype, end: u8) !?i32 {
const allocator = std.heap.page_allocator;
var number = std.ArrayList(u8).init(allocator);
defer number.deinit();
while (true) {
const byte = reader.readByte() catch |err| return err;
if (!std.ascii.isDigit(byte)) {
if (byte != end) return null;
return try std.fmt.parseInt(i32, number.items, 10);
}
try number.append(byte);
}
}