This repository has been archived on 2022-12-11. You can view files and clone it, but cannot push or open issues or pull requests.
aoc2022/04/1-2.zig

51 lines
1.4 KiB
Zig
Raw Permalink Normal View History

2022-12-04 22:44:05 +00:00
const std = @import("std");
// I don't have much time for this, it ain't gonna
// be as pretty as the other ones
pub fn main() void {
var totalFit: usize = 0;
var totalAabb: usize = 0;
2022-12-08 16:22:18 +00:00
const input = @embedFile("input");
2022-12-04 22:44:05 +00:00
var iter = std.mem.split(u8, input, "\n");
2022-12-08 16:22:18 +00:00
while (iter.next()) |line| {
2022-12-04 22:44:05 +00:00
if (line.len <= 0) continue;
//////////////////
// Split the nums into an array
////////////////
const delim = "-,-";
2022-12-08 16:22:18 +00:00
if ((delim.len + 1) & 1 != 0) @panic("Amount of nums should always be even");
var nums = [_]u8{undefined} ** (delim.len + 1);
2022-12-04 22:44:05 +00:00
var startIdx: usize = 0;
2022-12-08 16:22:18 +00:00
inline for (nums) |_, i| {
2022-12-04 22:44:05 +00:00
// Get the number
const numstr = if (i < delim.len)
std.mem.sliceTo(line[startIdx..], delim[i])
else
line[startIdx..];
startIdx += numstr.len + 1;
nums[i] = std.fmt.parseInt(u8, numstr, 10) catch unreachable;
}
if (fitTest(nums[0..2], nums[2..4])) totalFit += 1;
if (aabbTest(nums[0..2], nums[2..4])) totalAabb += 1;
}
std.debug.print("Part 1: {}\n", .{totalFit});
std.debug.print("Part 2: {}\n", .{totalAabb});
}
fn fitTest(a: []const u8, b: []const u8) bool {
return (a[0] >= b[0] and a[1] <= b[1]) or (b[0] >= a[0] and b[1] <= a[1]);
}
fn aabbTest(a: []const u8, b: []const u8) bool {
return a[1] >= b[0] and a[0] <= b[1];
}