|
| 1 | +const std = @import("std"); |
| 2 | +const mem = std.mem; |
| 3 | +const io = std.io; |
| 4 | +const ArrayList = std.ArrayList; |
| 5 | + |
| 6 | +const MAX_FILE_SIZE = 20_000; |
| 7 | + |
| 8 | +const Map = struct { |
| 9 | + bytes: []u8, |
| 10 | + line_width: usize, |
| 11 | + map_height: usize, |
| 12 | + |
| 13 | + pub inline fn init(bytes: []u8) Map { |
| 14 | + var line_width: usize = 0; |
| 15 | + while (line_width < bytes.len) : (line_width += 1) { |
| 16 | + switch(bytes[line_width]) { |
| 17 | + '\n', '\r' => break, |
| 18 | + else => continue |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + line_width += 1; |
| 23 | + |
| 24 | + return Map { |
| 25 | + .bytes = bytes, |
| 26 | + .line_width = line_width, // TODO: account for arbitrary line ending format |
| 27 | + .map_height = @divFloor(bytes.len, line_width) |
| 28 | + }; |
| 29 | + } |
| 30 | + |
| 31 | + pub inline fn inspectPath(self: Map, rigth: usize, down: usize) usize { |
| 32 | + var x_pos: usize = 0; |
| 33 | + var y_pos: usize = 0; |
| 34 | + var closed: u32 = 0; |
| 35 | + |
| 36 | + while (y_pos < self.map_height) { |
| 37 | + var n_skipped = @divFloor(x_pos, self.line_width - 1); |
| 38 | + var real_x = (x_pos + n_skipped) % self.line_width; |
| 39 | + var real_y = y_pos * self.line_width; |
| 40 | + |
| 41 | + closed += @boolToInt(self.bytes[real_x + real_y] == '#'); |
| 42 | + |
| 43 | + x_pos += rigth; |
| 44 | + y_pos += down; |
| 45 | + } |
| 46 | + |
| 47 | + return closed; |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +pub fn main() anyerror!void { |
| 54 | + const in = std.io.getStdIn().reader(); |
| 55 | + const stdout = std.io.getStdOut().writer(); |
| 56 | + |
| 57 | + var buf: [MAX_FILE_SIZE]u8 = undefined; |
| 58 | + const length = try in.readAll(&buf); |
| 59 | + |
| 60 | + var map = Map.init(buf[0..length]); |
| 61 | + |
| 62 | + var path_3_1 = map.inspectPath(3, 1); |
| 63 | + try stdout.print("{}\n", .{path_3_1}); |
| 64 | + |
| 65 | + try stdout.print("{}\n", .{map.inspectPath(1, 1) * path_3_1 * map.inspectPath(5, 1) * map.inspectPath(7, 1) * map.inspectPath(1, 2)}); |
| 66 | +} |
0 commit comments