diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2024-12-02 15:20:35 +0100 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2024-12-02 15:20:38 +0100 |
commit | 0c01b3924d55e0923cf895254b9412c9cedc9047 (patch) | |
tree | 9e5cf32ab38f6ea9b1aa36127d097aaa57d5e75f /aoc24/src | |
parent | ee7ee7ff1f136eceb739820d82f131ece3dd2cd2 (diff) | |
download | programming-problem-solving-0c01b3924d55e0923cf895254b9412c9cedc9047.tar.gz |
aoc2024: day 2
Diffstat (limited to 'aoc24/src')
-rw-r--r-- | aoc24/src/day2p1.zig | 54 | ||||
-rw-r--r-- | aoc24/src/day2p2.zig | 63 |
2 files changed, 117 insertions, 0 deletions
diff --git a/aoc24/src/day2p1.zig b/aoc24/src/day2p1.zig new file mode 100644 index 0000000..f5263be --- /dev/null +++ b/aoc24/src/day2p1.zig @@ -0,0 +1,54 @@ +const std = @import("std"); + +pub fn main() !void { + const input = @embedFile("2.txt"); + try std.fmt.format(std.io.getStdOut().writer(), "{}\n", .{try run(input)}); +} + +test { + const value = try run( + \\7 6 4 2 1 + \\1 2 7 8 9 + \\9 7 6 2 1 + \\1 3 2 4 5 + \\8 6 4 4 1 + \\1 3 6 7 9 + ); + std.debug.print("got {}\n", .{value}); + std.debug.assert(value == 2); +} + +pub fn run(input: []const u8) !u32 { + var lines = std.mem.split(u8, input, "\n"); + + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var reports = std.ArrayList(std.ArrayList(u32)).init(allocator); + defer reports.deinit(); + + while (lines.next()) |line| { + if (line.len == 0) continue; + var report = std.ArrayList(u32).init(allocator); + var words = std.mem.split(u8, line, " "); + while (words.next()) |word| { + try report.append(try std.fmt.parseInt(u32, word, 10)); + } + try reports.append(report); + } + + var safe: u32 = 0; + for (reports.items) |report| { + const increasing = report.items[0] < report.items[1]; + for (report.items[0 .. report.items.len - 1], report.items[1..]) |prev, curr| { + if ((prev < curr) != increasing) break; + if (prev == curr) break; + if (@max(prev, curr) - @min(prev, curr) > 3) break; + } else { + safe += 1; + } + } + + return safe; +} diff --git a/aoc24/src/day2p2.zig b/aoc24/src/day2p2.zig new file mode 100644 index 0000000..fd2f7a3 --- /dev/null +++ b/aoc24/src/day2p2.zig @@ -0,0 +1,63 @@ +const std = @import("std"); + +pub fn main() !void { + const input = @embedFile("2.txt"); + try std.fmt.format(std.io.getStdOut().writer(), "{}\n", .{try run(input)}); +} + +test { + const value = try run( + \\7 6 4 2 1 + \\1 2 7 8 9 + \\9 7 6 2 1 + \\1 3 2 4 5 + \\8 6 4 4 1 + \\1 3 6 7 9 + ); + std.debug.print("got {}\n", .{value}); + std.debug.assert(value == 4); +} + +pub fn run(input: []const u8) !u32 { + var lines = std.mem.split(u8, input, "\n"); + + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var reports = std.ArrayList(std.ArrayList(u32)).init(allocator); + defer reports.deinit(); + + while (lines.next()) |line| { + if (line.len == 0) continue; + var report = std.ArrayList(u32).init(allocator); + var words = std.mem.split(u8, line, " "); + while (words.next()) |word| { + try report.append(try std.fmt.parseInt(u32, word, 10)); + } + try reports.append(report); + } + + var safe: u32 = 0; + for (reports.items) |report| { + var is_safe = false; + for ([_]bool{ false, true }) |increasing| for (0..report.items.len) |skipped| { + const first: u32 = if (skipped == 0) 1 else 0; + var prev = report.items[first]; + for (first + 1..report.items.len) |i| { + if (i == skipped) continue; + const curr = report.items[i]; + if ((prev < curr) != increasing or prev == curr or @max(prev, curr) - @min(prev, curr) > 3) { + break; + } + prev = curr; + } else { + is_safe = true; + } + }; + if (is_safe) safe += 1; + // std.debug.print("{any} is {s}safe\n", .{ report.items, if (is_safe) "" else "un" }); + } + + return safe; +} |