whoops, missing grid
This commit is contained in:
parent
590986cb5c
commit
be46fa62dd
16
aoc/aoc.c
16
aoc/aoc.c
|
|
@ -70,3 +70,19 @@ parse_i64(str s, Arena temp) {
|
|||
ASSERT(*endptr == '\0');
|
||||
return result;
|
||||
}
|
||||
|
||||
Grid parse_grid(str input, Arena *arena) {
|
||||
Tokens lines = str_tokenize(arena, input, STR("\n"));
|
||||
ASSERT(lines.len > 0);
|
||||
i32 width = lines.tokens[0].len;
|
||||
for (i32 i = 0; i < lines.len; i++) {
|
||||
ASSERT(lines.tokens[i].len == width);
|
||||
}
|
||||
|
||||
Grid grid = { .width = width, .height = lines.len };
|
||||
grid.grid = ARENA_ALLOC_ARRAY(arena, u8, grid.width * grid.height);
|
||||
for (i32 i = 0; i < lines.len; i++) {
|
||||
memcpy(grid.grid + i * grid.width, lines.tokens[i].data, grid.width);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
|
|
|||
12
aoc/aoc.h
12
aoc/aoc.h
|
|
@ -17,6 +17,18 @@ Tokens read_lines(Arena *arena, const char *path);
|
|||
char *str_to_cstr(str s, Arena *a);
|
||||
i64 parse_i64(str s, Arena temp);
|
||||
|
||||
typedef struct Grid {
|
||||
u8 *grid;
|
||||
i32 width, height;
|
||||
} Grid;
|
||||
|
||||
Grid parse_grid(str input, Arena *arena);
|
||||
static inline u8
|
||||
grid_at(Grid *grid, i32 x, i32 y) {
|
||||
return grid->grid[y * grid->width + x];
|
||||
}
|
||||
|
||||
|
||||
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||
#include <stdio.h>
|
||||
#define ASSERT(cond) if (!(cond)) { fprintf(stderr, "%s:%d (%s): Assertion failed: %s\n", __FILE__, __LINE__, __func__, #cond); __builtin_trap(); }
|
||||
|
|
|
|||
Loading…
Reference in New Issue