Solve day 4

This commit is contained in:
Tyler Hallada 2022-12-04 00:53:25 -05:00
parent a1c8610cc3
commit b3eb61cc5f
4 changed files with 1064 additions and 0 deletions

55
src/day04.zig Normal file
View File

@ -0,0 +1,55 @@
const std = @import("std");
pub const input = @embedFile("input/day04.txt");
const test_input = @embedFile("input/day04_test1.txt");
const Range = struct {
start: usize,
end: usize,
fn parse(str: []const u8) !Range {
var range_parts = std.mem.tokenize(u8, str, "-");
const range = Range{
.start = try std.fmt.parseInt(usize, range_parts.next().?, 10),
.end = try std.fmt.parseInt(usize, range_parts.next().?, 10),
};
return range;
}
fn contains(self: Range, other: Range) bool {
return (other.start >= self.start and other.end <= self.end);
}
fn overlaps(self: Range, other: Range) bool {
return (self.start <= other.end and self.end >= other.start);
}
};
pub fn solve_part1(data: []const u8) !usize {
var lines = std.mem.tokenize(u8, data, "\n");
var count: usize = 0;
while (lines.next()) |line| {
var elves = std.mem.tokenize(u8, line, ",");
const range1 = try Range.parse(elves.next().?);
const range2 = try Range.parse(elves.next().?);
if (range1.contains(range2) or range2.contains(range1)) count += 1;
}
return count;
}
pub fn solve_part2(data: []const u8) !usize {
var lines = std.mem.tokenize(u8, data, "\n");
var count: usize = 0;
while (lines.next()) |line| {
var elves = std.mem.tokenize(u8, line, ",");
const range1 = try Range.parse(elves.next().?);
const range2 = try Range.parse(elves.next().?);
if (range1.overlaps(range2)) count += 1;
}
return count;
}
test "solves part1" {
try std.testing.expectEqual(solve_part1(test_input), 2);
}
test "solves part2" {
try std.testing.expectEqual(solve_part2(test_input), 4);
}

1000
src/input/day04.txt Executable file

File diff suppressed because it is too large Load Diff

View 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

View File

@ -3,6 +3,7 @@ const std = @import("std");
const day01 = @import("day01.zig"); const day01 = @import("day01.zig");
const day02 = @import("day02.zig"); const day02 = @import("day02.zig");
const day03 = @import("day03.zig"); const day03 = @import("day03.zig");
const day04 = @import("day04.zig");
const utils = @import("utils.zig"); const utils = @import("utils.zig");
fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void { fn solve_day(comptime day_num: u8, day: anytype, stdout: anytype, timer: *std.time.Timer) !void {
@ -30,6 +31,7 @@ pub fn main() !void {
try solve_day(1, day01, &stdout, &timer); try solve_day(1, day01, &stdout, &timer);
try solve_day(2, day02, &stdout, &timer); try solve_day(2, day02, &stdout, &timer);
try solve_day(3, day03, &stdout, &timer); try solve_day(3, day03, &stdout, &timer);
try solve_day(4, day04, &stdout, &timer);
try bw.flush(); try bw.flush();
} }
@ -40,5 +42,6 @@ test {
_ = day01; _ = day01;
_ = day02; _ = day02;
_ = day03; _ = day03;
_ = day04;
_ = utils; _ = utils;
} }