2022-04 in zig
This commit is contained in:
parent
02a8114847
commit
c912b3347d
4 changed files with 1058 additions and 0 deletions
6
2022/04-camp-cleanup/example
Normal file
6
2022/04-camp-cleanup/example
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8
|
26
2022/04-camp-cleanup/first.zig
Normal file
26
2022/04-camp-cleanup/first.zig
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const example = @embedFile("example");
|
||||||
|
const input = @embedFile("input");
|
||||||
|
|
||||||
|
pub fn main() anyerror!void {
|
||||||
|
try std.testing.expectEqual(solve(example), 2);
|
||||||
|
const result = try solve(input);
|
||||||
|
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve(puzzle: []const u8) !u64 {
|
||||||
|
var it = std.mem.tokenize(u8, puzzle, "\n");
|
||||||
|
var tot: u64 = 0;
|
||||||
|
while (it.next()) |line| {
|
||||||
|
var it2 = std.mem.tokenize(u8, line, "-,");
|
||||||
|
const a = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
const b = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
const c = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
const d = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
if (a <= c and b >= d or a >= c and b <= d) {
|
||||||
|
tot += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tot;
|
||||||
|
}
|
1000
2022/04-camp-cleanup/input
Normal file
1000
2022/04-camp-cleanup/input
Normal file
File diff suppressed because it is too large
Load diff
26
2022/04-camp-cleanup/second.zig
Normal file
26
2022/04-camp-cleanup/second.zig
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const example = @embedFile("example");
|
||||||
|
const input = @embedFile("input");
|
||||||
|
|
||||||
|
pub fn main() anyerror!void {
|
||||||
|
try std.testing.expectEqual(solve(example), 4);
|
||||||
|
const result = try solve(input);
|
||||||
|
try std.io.getStdOut().writer().print("{}\n", .{result});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve(puzzle: []const u8) !u64 {
|
||||||
|
var it = std.mem.tokenize(u8, puzzle, "\n");
|
||||||
|
var tot: u64 = 0;
|
||||||
|
while (it.next()) |line| {
|
||||||
|
var it2 = std.mem.tokenize(u8, line, "-,");
|
||||||
|
const a = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
const b = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
const c = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
const d = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10);
|
||||||
|
if (a <= c and b >= d or a >= c and b <= d or a <= c and b >= c or a <= d and b >= d) {
|
||||||
|
tot += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tot;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue