diff --git a/README.md b/README.md index c062bc6f..11879b45 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,9 @@ Welcome to this community project, where we collaboratively solve the 2020 editi | deno.ts | **7** | 1 | 1 | 1 | 1 | 1 | 1 | 1 | | | |||||||||||||||| | c | **6** | 1 | 1 | 1 | 1 | 1 | 1 | | | | |||||||||||||||| | node.js | **4** | 1 | | | 1 | | 1 | 1 | | | |||||||||||||||| +| zig | **3** | 1 | 1 | 1 | | | | | | | |||||||||||||||| | c++ | **2** | 1 | 1 | | | | | | | | |||||||||||||||| | ruby | **2** | 1 | | | 1 | | | | | | |||||||||||||||| -| zig | **2** | 1 | 1 | | | | | | | | |||||||||||||||| | rust | **1** | 1 | | | | | | | | | |||||||||||||||| | bash | **0** | | | | | | | | | | |||||||||||||||| | java | **0** | | | | | | | | | | |||||||||||||||| diff --git a/days/day-02/solutions/day02.zig b/days/day-02/solutions/day02.zig index 5032f35d..0fb6bc69 100644 --- a/days/day-02/solutions/day02.zig +++ b/days/day-02/solutions/day02.zig @@ -65,14 +65,14 @@ const RequirementPassword = struct { return occurence >= self.min; } - pub inline fn newValid(self: RequirementPassword) bool { + pub inline fn newValid(self: RequirementPassword) bool { var min_occurence: usize = @boolToInt(self.password[self.min - 1] == self.char); var max_occurence: usize = @boolToInt(self.password[self.max - 1] == self.char); return @as(usize, min_occurence) + @as(usize, max_occurence) == 1; } }; -fn bytesToRPPArrayList(allocator: *Allocator, bytes: []u8) !ArrayList(RequirementPassword) { +fn bytesToParsedArrayList(allocator: *Allocator, bytes: []u8) !ArrayList(RequirementPassword) { var list = try ArrayList(RequirementPassword).initCapacity(allocator, 2000); var it = mem.split(bytes, "\n"); @@ -110,7 +110,7 @@ pub fn main() anyerror!void { const length = try in.readAll(&buf); - var parsed_input = try bytesToRPPArrayList(allocator, buf[0..length]); + var parsed_input = try bytesToParsedArrayList(allocator, buf[0..length]); var old_valid: u32 = 0; var new_valid: u32 = 0; diff --git a/days/day-03/solutions/day03.zig b/days/day-03/solutions/day03.zig new file mode 100644 index 00000000..b534ee45 --- /dev/null +++ b/days/day-03/solutions/day03.zig @@ -0,0 +1,66 @@ +const std = @import("std"); +const mem = std.mem; +const io = std.io; +const ArrayList = std.ArrayList; + +const MAX_FILE_SIZE = 20_000; + +const Map = struct { + bytes: []u8, + line_width: usize, + map_height: usize, + + pub inline fn init(bytes: []u8) Map { + var line_width: usize = 0; + while (line_width < bytes.len) : (line_width += 1) { + switch(bytes[line_width]) { + '\n', '\r' => break, + else => continue + } + } + + line_width += 1; + + return Map { + .bytes = bytes, + .line_width = line_width, // TODO: account for arbitrary line ending format + .map_height = @divFloor(bytes.len, line_width) + }; + } + + pub inline fn inspectPath(self: Map, rigth: usize, down: usize) usize { + var x_pos: usize = 0; + var y_pos: usize = 0; + var closed: u32 = 0; + + while (y_pos < self.map_height) { + var n_skipped = @divFloor(x_pos, self.line_width - 1); + var real_x = (x_pos + n_skipped) % self.line_width; + var real_y = y_pos * self.line_width; + + closed += @boolToInt(self.bytes[real_x + real_y] == '#'); + + x_pos += rigth; + y_pos += down; + } + + return closed; + } +}; + + + +pub fn main() anyerror!void { + const in = std.io.getStdIn().reader(); + const stdout = std.io.getStdOut().writer(); + + var buf: [MAX_FILE_SIZE]u8 = undefined; + const length = try in.readAll(&buf); + + var map = Map.init(buf[0..length]); + + var path_3_1 = map.inspectPath(3, 1); + try stdout.print("{}\n", .{path_3_1}); + + try stdout.print("{}\n", .{map.inspectPath(1, 1) * path_3_1 * map.inspectPath(5, 1) * map.inspectPath(7, 1) * map.inspectPath(1, 2)}); +} diff --git a/days/day-03/test.sh b/days/day-03/test.sh index cd45467f..8d0bdc68 100755 --- a/days/day-03/test.sh +++ b/days/day-03/test.sh @@ -6,6 +6,7 @@ D=$(dirname $(realpath $0)) echo "" echo "--- Day 3: Counting trees ---" $D/../../languages/c.sh $D/input.txt $D/output.txt $D/solutions/day03.c +$D/../../languages/zig.sh $D/input.txt $D/output.txt $D/solutions/day03.zig $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/day03.stektpotet.go $D/../../languages/go.sh $D/input.txt $D/output.txt $D/solutions/day03.tholok97.go $D/../../languages/sml.sh $D/input.txt $D/output.txt $D/solutions/day03.sml