This commit is contained in:
Dendy 2022-12-04 23:44:05 +01:00
parent d24e400dc4
commit 02cebba0da
3 changed files with 1102 additions and 0 deletions

51
4/1-2.zig Normal file
View File

@ -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];
}

51
4/1.zig Normal file
View File

@ -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];
}

1000
4/input.asc Normal file

File diff suppressed because it is too large Load Diff