diff --git a/day-2/main.zig b/day-2/main.zig index b6a8396..35c0e54 100644 --- a/day-2/main.zig +++ b/day-2/main.zig @@ -10,7 +10,7 @@ pub fn define_direction(num1: i32, num2: i32) i8 { } } -pub fn main() !void { +pub fn part_1() !void { var file = try std.fs.cwd().openFile("input.txt", .{}); defer file.close(); @@ -28,7 +28,6 @@ pub fn main() !void { var direction: i8 = 0; var tokAny = std.mem.tokenizeAny(u8, line, " "); while (tokAny.next()) |num| { - std.debug.print("{s} ", .{num}); const n = try std.fmt.parseInt(i32, num, 10); if (first_round) { last_num = n; @@ -52,11 +51,84 @@ pub fn main() !void { } last_num = n; } - std.debug.print("\n", .{}); if (is_safe) { safe_reports += 1; } } - std.debug.print("{}", .{safe_reports}); + std.debug.print("Part 1: {}\n", .{safe_reports}); +} + +pub fn part_2() !void { + var file = try std.fs.cwd().openFile("input.txt", .{}); + defer file.close(); + + var buf_reader = std.io.bufferedReader(file.reader()); + var in_stream = buf_reader.reader(); + + var safe_reports: u32 = 0; + + var buf: [2048]u8 = undefined; + while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| { + var is_safe = true; + var round: i32 = -1; + var last_num: i32 = 0; + var defined_direction = false; + var direction: i8 = 0; + var shield = true; // very bad name lol + var tokAny = std.mem.tokenizeAny(u8, line, " "); + while (tokAny.next()) |num| { + round += 1; + const n = try std.fmt.parseInt(i32, num, 10); + if (round == 0) { + defined_direction = false; + last_num = n; + continue; + } + if (@abs(last_num - n) > 3) { + if (shield) { + if (round == 1) { + defined_direction = false; + last_num = n; + } + shield = false; + continue; + } + is_safe = false; + break; + } + + const current_direction = define_direction(last_num, n); + + if (!defined_direction) { + direction = current_direction; + if (current_direction != 0) { + defined_direction = true; + } + } + if (current_direction != direction or current_direction == 0) { + if (shield) { + if (round == 2) { + defined_direction = false; + last_num = n; + } + shield = false; + continue; + } + is_safe = false; + break; + } + last_num = n; + } + + if (is_safe) { + safe_reports += 1; + } + } + std.debug.print("Part 2: {}\n", .{safe_reports}); +} + +pub fn main() !void { + try part_1(); + try part_2(); }