diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/2015/day1/aoc.cpp | 13 | ||||
-rw-r--r-- | src/2015/day1/aoc.h | 8 | ||||
-rw-r--r-- | src/common.cpp | 25 | ||||
-rw-r--r-- | src/common.h | 4 |
4 files changed, 45 insertions, 5 deletions
diff --git a/src/2015/day1/aoc.cpp b/src/2015/day1/aoc.cpp index e69de29..2263558 100644 --- a/src/2015/day1/aoc.cpp +++ b/src/2015/day1/aoc.cpp @@ -0,0 +1,13 @@ +#include "aoc.h" + +namespace aoc2015 { + +int day1(line_view lv) { + int level = 0; + for (size_t i = 0; i < lv.length; ++i) { + level += lv.line[i] == '(' ? 1 : -1; + } + return level; +} + +} // namespace aoc2015 diff --git a/src/2015/day1/aoc.h b/src/2015/day1/aoc.h index 6f70f09..b83b525 100644 --- a/src/2015/day1/aoc.h +++ b/src/2015/day1/aoc.h @@ -1 +1,9 @@ #pragma once + +#include "common.h" + +namespace aoc2015 { + +int day1(line_view lv); + +} diff --git a/src/common.cpp b/src/common.cpp index 8ac3a96..d4457c6 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -5,9 +5,28 @@ #include <sys/stat.h> line_view load_file(const char* path) { - return {nullptr, 0}; + int fd; + struct stat fs; + fd = open(path, O_RDONLY); + if (fd == -1) + return {nullptr, 0}; + if (fstat(fd, &fs) == -1) + return {nullptr, 0}; + + line_view lv; + lv.length = fs.st_size; + lv.line = static_cast<const char*>(mmap(NULL, lv.length, PROT_READ, MAP_PRIVATE, fd, 0)); + return lv; } -line_view next_line(const char* file, size_t offset) { - return {nullptr, 0}; +line_view next_line(line_view file) { + static size_t offset = 0; + line_view lv; + lv.line = file.line + offset; + lv.length = 0; + while (offset < file.length && file.line[offset] != '\n') { + lv.length += 1; + offset++; + } + return lv; } diff --git a/src/common.h b/src/common.h index f242f48..e320b66 100644 --- a/src/common.h +++ b/src/common.h @@ -3,9 +3,9 @@ #include <stdlib.h> struct line_view { - char* line; + const char* line; size_t length; }; line_view load_file(const char*); -line_view next_line(const char*, size_t); +line_view next_line(line_view); |