return n;
}
-read_line :: (use reader: ^Reader, consume_newline := true, allocator := context.allocator) -> str {
+read_line :: (use reader: ^Reader, consume_newline := true, allocator := context.allocator, inplace := false) -> str {
reader_read_next_chunk(reader);
count := start;
+ defer start = count;
+
while count < end && buffer[count] != #char "\n" {
count += 1;
}
count += 1;
}
+ if inplace do return buffer[start .. count];
+
out := str.{
data = raw_alloc(allocator, count * sizeof(u8)),
count = count - start,
};
memory.copy(out.data, buffer.data + start, count - start);
- start = count;
return out;
}
-read_word :: (use reader: ^Reader, numeric_allowed := false, allocator := context.allocator) -> str {
+read_word :: (use reader: ^Reader, numeric_allowed := false, allocator := context.allocator, inplace := false) -> str {
skip_whitespace(reader);
reader_read_next_chunk(reader);
count := start;
+ defer start = count;
while count < end {
curr := buffer[count];
if (curr >= #char "a" && curr <= #char "z")
}
}
+ if inplace do return buffer[start .. count];
+
out := str.{
data = raw_alloc(allocator, count * sizeof(u8)),
count = count - start,
};
memory.copy(out.data, buffer.data + start, count - start);
- start = count;
return out;
}
-read_until :: (use reader: ^Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false) -> str {
+read_until :: (use reader: ^Reader, until: u8, skip: u32 = 0, allocator := context.allocator, consume_end := false, inplace := false) -> str {
reader_read_next_chunk(reader);
count := start;
+ defer start = count;
while count < end {
curr := buffer[count];
if curr != until {
if consume_end && count < end do count += 1;
+ if inplace do return buffer[start .. count];
+
out := str.{
data = raw_alloc(allocator, count * sizeof(u8)),
count = count - start,
};
memory.copy(out.data, buffer.data + start, count - start);
- start = count;
return out;
}
use package core
-__output_string :: (s: str) -> u32 #foreign "host" "print_str" ---
-__exit :: (status: i32) -> void #foreign "host" "exit" ---
+__output_string :: (s: str) -> u32 #foreign "host" "print_str" ---
+__exit :: (status: i32) -> void #foreign "host" "exit" ---
+__read_from_input :: (buf: [] u8) -> u32 do return 0;
// The builtin _start proc.
// Sets up everything needed for execution.
__exit :: (status: i32) do proc_exit(status);
+__read_from_input :: (buffer: [] u8) -> i32 {
+ STDIN_FILENO :: 0
+
+ vec := IOVec.{ buf = cast(i32) buffer.data, len = buffer.count };
+ read: Size;
+ error := fd_read(STDIN_FILENO, .{ cast(i32) ^vec, 1 }, ^read);
+ if error != .Success do return -1;
+
+ return read;
+}
+
+
// The builtin _start proc.
// Sets up everything needed for execution.
#export "_start" () {
__stdio_init :: () {
stdio.print_stream = io.dynamic_string_stream_make(2048, context.allocator);
stdio.print_writer = io.writer_make(^stdio.print_stream);
+
+ // This shouldn't need to be here, but because ^stdin_vtable is not a compile-time
+ // known value (even through it should be).
+ stdin.vtable = ^stdin_vtable;
}
^stdio.print_stream |> io.stream_flush();
}
+#local stdin_vtable := io.Stream_Vtable.{
+ read = (_: ^io.Stream, buffer: [] u8) -> (io.Error, u32) {
+ bytes_read := runtime.__read_from_input(buffer);
+ if bytes_read <= 0 do return .EOF, 0;
+
+ return .None, bytes_read;
+ },
+
+ read_byte = (_: ^io.Stream) -> (io.Error, u8) {
+ buf: [1] u8;
+ bytes_read := runtime.__read_from_input(buf);
+ if bytes_read <= 0 do return .EOF, 0;
+
+ return .None, buf[0];
+ }
+}
+
+stdin: io.Stream;
\ No newline at end of file
+Part 1: 1564
Part 2: 1611
-PART :: 2
-
#load "core/std"
use package core
io.skip_whitespace(^reader);
}
- #if PART == 1 {
+ { // Part 1
increased_count := count_increasing(nums);
printf("Part 1: {}\n", increased_count);
}
- #if PART == 2 {
+ { // Part 2
windows := array.make(i32);
for i: range.{ 0, nums.count - 2 } {
sum := 0;
increased_count := count_increasing(windows);
printf("Part 2: {}\n", increased_count);
}
-}
\ No newline at end of file
+}