-
Notifications
You must be signed in to change notification settings - Fork 8
Zig day 3 #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zig day 3 #69
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 💯
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] == '#'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)}); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.