blob: 80bb649501694022ff23893e52b421cc80277c76 (
plain) (
blame)
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 math = std.math;
const Result = @import("util/aoc.zig").Result;
pub fn puzzle_1(input: []const u8) !Result {
var iter = std.mem.split(u8, input, "\n");
var count: i32 = 0;
var max: i32 = 0;
while (iter.next()) |line| {
if (line.len == 0) {
if (count > max) {
max = count;
}
count = 0;
} else {
count += try std.fmt.parseInt(i32, line, 0);
}
}
return .{ .int = max };
}
pub fn puzzle_2(input: []const u8) !Result {
var iter = std.mem.split(u8, input, "\n");
var count: i32 = 0;
var max: [3]i32 = std.mem.zeroes([3]i32);
while (iter.next()) |line| {
if (line.len == 0) {
const lowest_u = min_idx(i32, &max);
if (count > max[lowest_u]) {
max[lowest_u] = count;
}
count = 0;
} else {
count += try std.fmt.parseInt(i32, line, 0);
}
}
count = 0;
for (max) |v| {
count += v;
}
return .{ .int = count };
}
fn min_idx(comptime T: type, slice: []const T) usize {
var best = slice[0];
var idx: usize = 0;
for (slice[1..]) |item, i| {
const possible_best = math.min(best, item);
if (best > possible_best) {
best = possible_best;
idx = i + 1;
}
}
return idx;
}
|