149 lines
2.5 KiB
C
149 lines
2.5 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <limits.h>
|
|
#include <stdbool.h>
|
|
|
|
struct grid {
|
|
char** A;
|
|
int x, y;
|
|
};
|
|
|
|
void* xrealloc(void* ptr, size_t size) {
|
|
if (size == 0) {
|
|
size = 1;
|
|
}
|
|
ptr = realloc(ptr, size);
|
|
if (ptr == NULL) {
|
|
fprintf(stderr, "fatal: alloc(%zd) failed", size);
|
|
exit(1);
|
|
}
|
|
return ptr;
|
|
}
|
|
|
|
int readfile(const char* filename, struct grid* out) {
|
|
FILE *fp = fopen(filename, "r");
|
|
if (fp == NULL) {
|
|
perror("fopen");
|
|
exit(1);
|
|
}
|
|
|
|
int len = 0;
|
|
int cap = 100;
|
|
char* data = xrealloc(NULL, cap);
|
|
for (;;) {
|
|
if (len >= cap) {
|
|
cap *= 2;
|
|
data = xrealloc(data, cap);
|
|
}
|
|
int c = fgetc(fp);
|
|
if (c == -1) {
|
|
break;
|
|
}
|
|
data[len] = c;
|
|
len++;
|
|
}
|
|
int i, nl=0;
|
|
for (i = 0; i < len; i++) {
|
|
if (data[i] == '\n') {
|
|
nl++;
|
|
}
|
|
}
|
|
// split on newlines
|
|
char** lines = xrealloc(NULL, nl * sizeof(char*));
|
|
char* p = data;
|
|
int mincol = INT_MAX;
|
|
i = 0;
|
|
while (p != NULL && p < data+len) {
|
|
lines[i++] = p;
|
|
char* q = memchr(p, '\n', (data+len)-p);
|
|
if (!q) {
|
|
break; // TODO: final line?
|
|
}
|
|
int col = q - p;
|
|
if (mincol > col && col > 0) {
|
|
mincol = col;
|
|
}
|
|
p = q+1; // skip newline
|
|
}
|
|
|
|
out->A = lines;
|
|
out->x = mincol;
|
|
out->y = nl;
|
|
|
|
return 0;
|
|
}
|
|
|
|
inline bool inbounds(struct grid *g, int x, int y) {
|
|
return (0 <= x && x < g->x && 0 <= y && y < g->y);
|
|
}
|
|
|
|
struct step_r { int x, y, dx, dy; bool ok; };
|
|
struct step_r step(struct grid *g, int x, int y, int dx, int dy) {
|
|
int i;
|
|
struct step_r r = {0};
|
|
if (!inbounds(g, x, y)) {
|
|
return r;
|
|
}
|
|
// if there is nothing in front of you, step forward
|
|
if (!inbounds(g, x+dx, y+dy) || g->A[y+dy][x+dx] != '#') {
|
|
r.x = x+dx;
|
|
r.y = y+dy;
|
|
r.dx = dx;
|
|
r.dy = dy;
|
|
r.ok = true;
|
|
return r;
|
|
} else {
|
|
// if there is something in front of you, turn right
|
|
r.x = x;
|
|
r.y = y;
|
|
r.dx = -dy;
|
|
r.dy = dx;
|
|
r.ok = true;
|
|
return r;
|
|
}
|
|
}
|
|
|
|
|
|
int solve(struct grid *g) {
|
|
int x, y;
|
|
int cx = -1, cy = -1;
|
|
int count = 0;
|
|
for (y = 0; y < g->y; y++)
|
|
for (x = 0; x < g->x; x++) {
|
|
if (g->A[y][x] == '^') {
|
|
cx = x;
|
|
cy = y;
|
|
break;
|
|
}
|
|
}
|
|
int dx = 0, dy = -1; // up
|
|
while(inbounds(g, cx, cy)) {
|
|
if (g->A[cy][cx] != 'x') {
|
|
count++;
|
|
}
|
|
g->A[cy][cx] = 'x';
|
|
|
|
struct step_r r = step(g, cx, cy, dx, dy);
|
|
if (r.ok != true) {
|
|
break;
|
|
}
|
|
cx = r.x;
|
|
cy = r.y;
|
|
dx = r.dx;
|
|
dy = r.dy;
|
|
}
|
|
printf("%d\n", count);
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
struct grid g;
|
|
readfile("sample1.in", &g);
|
|
readfile("input", &g);
|
|
printf("%d %d\n", g.x, g.y);
|
|
solve(&g);
|
|
//solve2(&g);
|
|
printf("%s", g.A[0]);
|
|
}
|