2022-01 in zig
This commit is contained in:
parent
abb58800e2
commit
c0877054bd
4 changed files with 2342 additions and 0 deletions
14
2022/01/example
Normal file
14
2022/01/example
Normal file
|
@ -0,0 +1,14 @@
|
|||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
28
2022/01/first.zig
Normal file
28
2022/01/first.zig
Normal file
|
@ -0,0 +1,28 @@
|
|||
const std = @import("std");
|
||||
|
||||
const example = @embedFile("example");
|
||||
const input = @embedFile("input");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
try std.testing.expectEqual(solve(example), 24000);
|
||||
const result = try solve(input);
|
||||
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||
}
|
||||
|
||||
fn solve(puzzle: []const u8) !u64 {
|
||||
var it = std.mem.split(u8, puzzle, "\n");
|
||||
var max: u64 = 0;
|
||||
var tot: u64 = 0;
|
||||
while (it.next()) |value| {
|
||||
const n = std.fmt.parseInt(u64, value, 10) catch 0;
|
||||
if (n == 0) {
|
||||
if (tot > max) {
|
||||
max = tot;
|
||||
}
|
||||
tot = 0;
|
||||
} else {
|
||||
tot += n;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
2267
2022/01/input
Normal file
2267
2022/01/input
Normal file
File diff suppressed because it is too large
Load diff
33
2022/01/second.zig
Normal file
33
2022/01/second.zig
Normal file
|
@ -0,0 +1,33 @@
|
|||
const std = @import("std");
|
||||
|
||||
const example = @embedFile("example");
|
||||
const input = @embedFile("input");
|
||||
|
||||
pub fn main() anyerror!void {
|
||||
try std.testing.expectEqual(solve(example), 45000);
|
||||
const result = try solve(input);
|
||||
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||
}
|
||||
|
||||
fn solve(puzzle: []const u8) !u64 {
|
||||
var it = std.mem.split(u8, puzzle, "\n");
|
||||
var top3 = [_]u64{ 0, 0, 0 };
|
||||
var tot: u64 = 0;
|
||||
while (it.next()) |value| {
|
||||
const n = std.fmt.parseInt(u64, value, 10) catch 0;
|
||||
if (n == 0) {
|
||||
for (top3) |*max| {
|
||||
if (tot > max.*) {
|
||||
// this swapping will keep the array sorted
|
||||
const prev = max.*;
|
||||
max.* = tot;
|
||||
tot = prev;
|
||||
}
|
||||
}
|
||||
tot = 0;
|
||||
} else {
|
||||
tot += n;
|
||||
}
|
||||
}
|
||||
return top3[0] + top3[1] + top3[2];
|
||||
}
|
Loading…
Add table
Reference in a new issue