Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** | | | | | | | | | | ||||||||||||||||
Expand Down
6 changes: 3 additions & 3 deletions days/day-02/solutions/day02.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
66 changes: 66 additions & 0 deletions days/day-03/solutions/day03.zig
Original file line number Diff line number Diff line change
@@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Behold, the most boring collection of words in the universe: "arbitrary line ending format". 😆 Do not mention this on a date 💯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't promise anything 😁

.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] == '#');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the @ signifying compile-time in this language?

Copy link
Contributor Author

@Avokadoen Avokadoen Dec 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arxcis Comptime has no extra syntax in Zig other than "comptime". @ means compiler builtin function. It's in other words language features that does a given operation, like casting or adding with overflow (will panic with safety on otherwise)

To run an expression on compile time you simply say comptime *expression*. See:

Copy link
Owner

@Arxcis Arxcis Dec 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 Aha! Very helpful 👍


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)});
}
1 change: 1 addition & 0 deletions days/day-03/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down