diff --git a/src/days/day07.zig b/src/days/day07.zig index 28eeee2..9319ce8 100644 --- a/src/days/day07.zig +++ b/src/days/day07.zig @@ -1,7 +1,86 @@ const std = @import("std"); -pub const title = "Day 07"; +pub const title = "Day 07: Laboratories"; -pub fn run(_: std.mem.Allocator) !void { +pub fn run(allocator: std.mem.Allocator) !void { + const input = @embedFile("./input/day07.txt"); + //const input = + // \\.......S....... + // \\............... + // \\.......^....... + // \\............... + // \\......^.^...... + // \\............... + // \\.....^.^.^..... + // \\............... + // \\....^.^...^.... + // \\............... + // \\...^.^...^.^... + // \\............... + // \\..^...^.....^.. + // \\............... + // \\.^.^.^.^.^...^. + // \\............... + // ; + + var lines = std.mem.tokenizeScalar(u8, input, '\n'); + + const first_line = lines.next() orelse unreachable; + const starting_position = for (first_line, 0..) |c, i| { + if (c == 'S') break i; + } else unreachable; + const line_len = first_line.len; + + std.debug.print("starting_position = {d}\n", .{starting_position}); + + std.debug.print("{s}\n", .{first_line}); + + const previous_line = try allocator.alloc(u8, line_len); + defer allocator.free(previous_line); + + var accumulator: usize = 0; + + var i: usize = 0; + while (lines.next()) |line| { + defer i += 1; + + const current_line = try allocator.alloc(u8, line_len); + defer allocator.free(current_line); + @memcpy(current_line, line); + + defer @memcpy(previous_line, current_line); + + defer std.debug.print("{s}\n", .{current_line}); + + if (i == 0) { + std.debug.assert(current_line[starting_position] == '.'); + current_line[starting_position] = '|'; + continue; + } + + for (line, 0..) |c, ci| { + if (previous_line[ci] == '|') { + if (c == '.') { + current_line[ci] = '|'; + } else if (c == '^') { + accumulator += 1; + if (ci != 0 and current_line[ci - 1] == '.') { + current_line[ci - 1] = '|'; + } + if (ci != line_len - 1 and current_line[ci + 1] == '.') { + current_line[ci + 1] = '|'; + } + } + } + } + } + + var buffer: [8]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&buffer); + const stdout = &stdout_writer.interface; + + try stdout.print("{d}\n", .{accumulator}); + + try stdout.flush(); } diff --git a/src/days/input/day07.txt b/src/days/input/day07.txt new file mode 100755 index 0000000..9b6063c --- /dev/null +++ b/src/days/input/day07.txt @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^...^.^................................................................... +............................................................................................................................................. +..................................................................^.^...^.^.................................................................. +............................................................................................................................................. +.................................................................^...^.^.^.^................................................................. +............................................................................................................................................. +................................................................^.^.........^................................................................ +............................................................................................................................................. +...............................................................^.^...^.^.^.^.^............................................................... +............................................................................................................................................. +..............................................................^.^.^.^.^.^.....^.............................................................. +............................................................................................................................................. +.............................................................^.^.....^...^.^.^.^............................................................. +............................................................................................................................................. +............................................................^...^.....^.^...^.^.^............................................................ +............................................................................................................................................. +...........................................................^.....^.^.^.^.^.^.^.^.^........................................................... +............................................................................................................................................. +..........................................................^...^...^.....^.^...^.^.^.......................................................... +............................................................................................................................................. +.........................................................^.^.....^.^.....^.^.^.^.^.^......................................................... +............................................................................................................................................. +........................................................^...^.^.^.^...^.^.....^.....^........................................................ +............................................................................................................................................. +.......................................................^.^.^.^.^.^.....^.....^.^.^.^.^....................................................... +............................................................................................................................................. +......................................................^.^.....^.^.^.....^.^...^...^.^.^...................................................... +............................................................................................................................................. +.....................................................^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^..................................................... +............................................................................................................................................. +....................................................^.^.^...^.^.^.^.^.^.^.^...^...^.^.^.^.................................................... +............................................................................................................................................. +...................................................^.^...^.^.^.^.^.^...^.^.^.^.^...^...^.^................................................... +............................................................................................................................................. +..................................................^.^.^.^...^.^.^.^.^.^.^...^.^...^.^...^.^.................................................. +............................................................................................................................................. +.................................................^.^.^.^...^.^.......^.^.^.^.....^.^.^.^.^.^................................................. +............................................................................................................................................. +................................................^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^...^................................................ +............................................................................................................................................. +...............................................^.....^...^.^.^.^.^...^...^.^.^.^.^.^.^.^...^.^............................................... +............................................................................................................................................. +..............................................^...^.^...^.^...^.....^.^.^.^.^.^...^.^.^.......^.............................................. +............................................................................................................................................. +.............................................^.^.^.^.^...^.^.^.^.^.^...^.....^.^.....^.^.^...^.^............................................. +............................................................................................................................................. +............................................^.^.^.^.^.^.^.^.^.^.^...^.^.....^...^.^...^.^.^.^...^............................................ +............................................................................................................................................. +...........................................^.^.....^.^.....^.^.^.^.^.^.^.^.^.^...^.^...^.^.^.....^........................................... +............................................................................................................................................. +..........................................^.^.^...^.....^.^.^.^.^...^.^.....^.^.^.^...^...^.^...^.^.......................................... +............................................................................................................................................. +.........................................^.^...^.^.^.^.^.^.^.^.^.^.^.....^.....^.^.^.^.^.^.^.^.^.^.^......................................... +............................................................................................................................................. +........................................^.....^.^...^.^.^.^.....^.^.^.^...^...^.^.^.^.^...^.^...^.^.^........................................ +............................................................................................................................................. +.......................................^.^.^.^.....^.^.^.......^.^.........^...^.^.^.^.....^.^.....^.^....................................... +............................................................................................................................................. +......................................^.^.....^.^.^...^.^...^.^.......^.^...^.^.^.^.^.^...^.....^.^...^...................................... +............................................................................................................................................. +.....................................^.^.^...^.^.^.^.^.....^.^...^.....^.....^...^.^...^.......^.^.^.^.^..................................... +............................................................................................................................................. +....................................^.^.^...^...^.^.^...^.^...^.^.^.^.......^.....^.....^.^.......^.^.^.^.................................... +............................................................................................................................................. +...................................^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^...^.^...^.^.^.^.^...^.^.^................................... +............................................................................................................................................. +..................................^...^.^.^.^...^.......^.^.^...^.^.......^.^.^.....^...^...^.^.^...^.^...^.................................. +............................................................................................................................................. +.................................^...^.......^.^.^.....^.^...^.^...^.^.^.^.^.^.....^.^...^...^.^.^...^...^.^................................. +............................................................................................................................................. +................................^.^.^...^.^.^.^.^.^.^...^...^.^.......^.^...^.^.^...^.^...^.^.^.^.^...^...^.^................................ +............................................................................................................................................. +...............................^.^.....^.^.^...^.^.^.^...^.........^.^.^.^.........^.^.^...........^.^.^.^...^............................... +............................................................................................................................................. +..............................^.^...^.^...^...^...^...^.^.^.^.^.^...^.^.^...^.^.^...^.....^.^.^...^...^.^.^...^.............................. +............................................................................................................................................. +.............................^.^.^.^.^.^.^.....^.^.^.^.^.^.....^...^.^.^.......^.^.^.^.....^.........^.^.^.....^............................. +............................................................................................................................................. +............................^...^...^.^.^...^...^.^.^.^.^.^.^.^.^.^.......^.^.......^.^.....^.^.^.^...^.^...^...^............................ +............................................................................................................................................. +...........................^.^.^...^.^.^.....^.....^.^.......^.^.^.....^.^.^...^.....^...^.^.^.^.....^.^.^.^.^.^.^........................... +............................................................................................................................................. +..........................^.....^.....^.^...^.^.^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.^.^.....^.^.^...^.^...^...^.......................... +............................................................................................................................................. +.........................^.^...^.^.^.^...^.^...^.^.........^.......^.^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^...^......................... +............................................................................................................................................. +........................^...^.^.^.^...^.^...^...^.^...^.^.^...^.^.^.^.^.^...^.^.^...^.......^.^.^.^.^...^.....^...^.^........................ +............................................................................................................................................. +.......................^.^.^...^.^.^.....^.^...^.^.^...^...^.^.^...^.^.^...^.^.....^.^.^.^.^...^.^.........^.^.^.^.^.^....................... +............................................................................................................................................. +......................^.^.^.^...^.^.^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.....^.^.....^...^.^.^.^.....^...........^.^.^...................... +............................................................................................................................................. +.....................^.^.^.^.^.^...^.^.^.^.^.....^.^...^...^...^.^.^.^.^.....^.^.^.....^.^...^.......^.^.^.....^.^.^.^.^..................... +............................................................................................................................................. +....................^...^.^.......^.^.^.^.^.^.........^.^.^.^.......^.^...^.^.^.^.^.^.^.^.^...^.^.^.....^.^.^.^.^.^.^.^.^.................... +............................................................................................................................................. +...................^.^.......^.^...^.^.^.^.^.^...^.^...^.^...^.....^.^.^...^.^...^.^.^.^.^...^...^.^.....^.^...^.^.^.^.^.^................... +............................................................................................................................................. +..................^.^.........^.^.^...^.^...^...^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^...^.....^...^.^.................. +............................................................................................................................................. +.................^.^.^...^.^.^.^.^.......^.^.....^.^...^.^.^...^.^.^...^.^.^.^.^...^.......^.^...^.^.^.........^.^...^.^.^.^................. +............................................................................................................................................. +................^.....^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^.^.^.^...^...^.^...^.^.^.^...^.^.^.^.^.^.....^...^.....^.^................ +............................................................................................................................................. +...............^.....^.^.^.^.^.^.^.^.^.^...^.^...^...^...^...^...^.^.^.^.........^.^.^.^.^...^.....^.^.......^...^...^.^.^.^.^............... +............................................................................................................................................. +..............^.^.^.^.^.^.^...^.^.^.^.^.^.^.^...^.^.^.^...^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.....^.^...^.^.^.^.^...^.....^.....^.............. +............................................................................................................................................. +.............^.^...^.^.....^.......^.....^.....^.^...^.^.^.^.....^.^.....^.....^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^.^............. +............................................................................................................................................. +............^.^...^.^.....^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.^...^.....^.^...^...^.........^.^...^.^...^.^.^.^.^.^.^.^...^............ +............................................................................................................................................. +...........^...^.^.^...^.^...^...^.^...^...^.^.^.^...^.^.^...^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.....^.^.^.^.^...^.^.^.....^.^.^.^........... +............................................................................................................................................. +..........^.^.........^.^...^.....^.^.^.^.^.^.^.^...^...^...^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^.^...^.^.^.^...^.....^...^...^.......... +............................................................................................................................................. +.........^.^.^.^...^...^.^.^.^...^.^.^.^.^.^.....^...^.^.....^.^...^...^.^...^.^.^.^...^.^...^...^.^.^.^...^...^.....^.^.^...^...^.^......... +............................................................................................................................................. +........^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.........^...^.^.^.^.....^.^...^.^.^.^.^.....^.^.^.^.^.^.^.^.^.^...^.^.^.....^.^.....^.^.^........ +............................................................................................................................................. +.......^.......^.^.^.^...^.^...^.^...^.^.^.^.^.^.^.^.^...^.^.^...^...^...^.^.^...^.^.^...^.^.^.^.^.^.^...^.^.....^.^.^...^.^.^...^.^.^....... +............................................................................................................................................. +......^...^...^.^.....^.^.^.^.^.^.^...^.^.^.^...^.^.^...^.^.^.....^.^.^.....^.^.^.^.^...^.^.^.^.....^.^.^...^.^.^.....^.^.^.^.^.^...^.^...... +............................................................................................................................................. +.....^.....^.^.^.^.^...^.........^.....^.......^.^.^...^.^.......^.^.^.^...^.^.^.^.^...^.^.^.^.^.^.^...^...^.^.^...^...^.^...^.^.....^.^..... +............................................................................................................................................. +....^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^.^.^.^...^.....^.^.....^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^...^.^.^...^...^.... +............................................................................................................................................. +...^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.....^.^.^.^.^.^...^.......^.^...^...^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^.^...^... +............................................................................................................................................. +..^.^.^.^...^.^.^.^.^...^.....^...^.^.^...^...^.^.^.^...^...^.^.^...^...........^.^.^.^.......^...^...^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.^.. +............................................................................................................................................. +.^.^.^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.^...^.^...^.^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^.....^.^.......^.^.^.^.^...^...^.^.^.^.......^.....^. +.............................................................................................................................................