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/4/1-2.zig

52 lines
1.4 KiB
Zig
Raw 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;
const input = @embedFile("input.asc");
var iter = std.mem.split(u8, input, "\n");
while(iter.next()) |line| {
if (line.len <= 0) continue;
//////////////////
// Split the nums into an array
////////////////
const delim = "-,-";
if ((delim.len+1)&1 != 0) @panic("Amount of nums should always be even");
var nums = [_]u8{undefined} ** (delim.len+1);
var startIdx: usize = 0;
inline for(nums) |_,i| {
// 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];
}