Day 4
This commit is contained in:
parent
d24e400dc4
commit
b17f160cc2
|
@ -0,0 +1,51 @@
|
|||
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];
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue