diff --git a/day06/Makefile b/day06/Makefile new file mode 100644 index 0000000..8b7b9d4 --- /dev/null +++ b/day06/Makefile @@ -0,0 +1,2 @@ +CFLAGS=-O2 -std=c99 -Wall -Wextra +all: sol diff --git a/day06/input b/day06/input new file mode 100644 index 0000000..22c7157 --- /dev/null +++ b/day06/input @@ -0,0 +1,130 @@ +....#.....#......#...............................#...........#.......#.....#................#...#....#....#................#..#.#. +...................................................................................#...........#............#..................... +..........#..............#.................................................#..............#....................................... +....................#...............#...#.....................#..............................#....#.............................#. +...............................##...............................................................#............#.................... +................#...........#................#...............#....#.......#.............................#..........#...........#.. +...........................#................................................#.##..................#.............#........#........ +......#......#.....#..........#.......................#..........#.............#.....................#........#....#.............. +..................................................#..................................#....#.........#...........#..............#.. +..................................#..................#..........................................#.........#....................#.. +#.................#....................#...........#....................................#......................#.................. +.......#..........................................................................................................#............... +...............##.............................#...#.....................#..........................#.......................#...... +...#.......................................................#..........................................#...................#..#.... +.......#...#.......#.#.............#.....#.##.........................#........................................................... +#................#.........#.......................##............................................#...#.....#........#.......#..... +#......##..............................................................#......#....#....#....#.#...............#............#..#.. +.....#...............................##.......................................#.......#......#............................#.....#. +.........#......#...............................#.....#.....#...................##......#......................................... +.#.................................................#...............#........#............................#........................ +#.........#.........................................#.......................#..............#......................#...........#... +..........................................##.............................................................##..#.......#............ +...#.........................#...........................................#........#............................................... +#..................#............#.................................................................#.#....................#....#..# +...................#.....................................#.#.........#.............#......#............................##......... +....#..............................................................##.......................................................#....# +.....................#.....#.............................................#.......................#..................#............. +...........#.......................................#................#.........#...............#.#...........#.....#.....#........# +.#..#.................#....#...............................#.................................................##................... +..#.....#......#..........#...................#..#..................................................#.......#...#................. +.............................................................#.......................#..........#.....#...................#....... +.......#.......................#.#....#...#.....##.......................#.................................................#...... +.................#.....................#...............................................................#..#....................... +...#.................#.#..................................................##......................................#..#............ +#........................................................#...#.........#................#.#....................................... +.................#........#.#........................#.#..................................#....................................... +..........#..#......#..................#.........................#................................#............................... +.............................................................................................#.................................... +.....................#...........#........##.....#.........................#..............................#.....#....#............ +..#........#.........#.....................................................#...................................................... +...................................#...................#............#.............#...........................#................... +....#.........................................................#................................#......#...#..#.................... +................................................................................#..#.......#..........................#....#...... +#...........................................................................#........................#............................ +..#..............#..........#...#..........................................................#...................................... +.......................................................#.................#.#....#.................#..........#....#............... +.................................................................................................................................. +.................#.........................................................#..........#.........#.........#.................#..... +....................#........#...................#................#...#....................#..............#................#...... +...#.............##........................................#.........#.................#...........#.........#.................... +................#............#...........#...............#.....................................#...........................#...... +....................................................#...........#..............................#..........#...........#........... +.........#..............#................................#.............#......................................#.........#...#..... +..#..#.........................................#.............................#.................................................... +.#............................##...#..........#............#..#.........................#......................#...#.............. +........#.....#..............#..................................................#...#......................#....#..............#.. +...............................................#.........#..................#............#.............................#.......... +..................................................................................................#....#.......................... +..............#..............................................................#.................................................... +.....#....#.....................#......................#...........#...#..#.............#.......................#................. +.............................#...#..#...........................#............................................................#.... +.............................................#...............................#...................................................# +..................................................................#......#....#.......................#.......#........#.......... +..................#.#.........................................................................................##.......#........#. +.............................#.......................#.......................................##................................... +.....#.............................................................................................................#.#............ +...............##.........#..#.....#...............#....................#.................................#.#....#................ +................#......................................................................#...............#...................#...... +.................#..............#................................................................................................. +............#...#...........................................................................................#......#.............. +..#.....................................................................#.....#.#.#......#....#..........#........................ +.#........#.............................#.....................#.....................#....#........................................ +.......................................................................................................#.......................... +...#........#..................................................#.........................................................#......#. +.......#.......#..#.................................................................................................#............. +..............................#..#..........................#..#..................#.............#................................. +..................#...#.............................................#...................#.......#...............#................. +...#.......#........#.....................................#...........#.................................................#......... +.#.............#....................................#........................................................##............#...#.. +...............#.....#....................................................................................#....#................#. +....#...........................................................#.....#..............................................#............ +.........#..........#...........................#.......................................#....#....................#............... +...............................................................#..........#......^...............................................# +#..........................#.....................#..............................#..............................#.................. +...................#.....#......#...........#........#............#.......#.......#............................................... +.....#.....#.............................................................#..............#........#..........................#....# +............#...............................................................#.............#......................#................ +.....#...............................................##........................................#.......#......#................... +.........................................#........................................................................................ +..........................#...............................#............#............................#.....#....................... +...##.........#......#.....#...............................................................#........#............................. +......#...............#.#......................#.......#..............................#...#.........#.........#..#.........#...... +....#......#....#........#.#.............................#......................................................#.....#........... +.....#.............#.....................#...#...............#.................................................................... +......#............#............#.#............#....................#............................................................. +.............#..................................................#....................#.............#.......#.............#........ +.............................................#......................................................................#............. +...............................#............................................................#................#...................# +............#.....#....................................................................................................#.#........ +.........................#....................#...##.......#............#..#.................................#......#............. +....#......................#.....................#...........#.........................................#......#.#................. +..................................................................#..........................#......................#............. +.......................#......#..............................................................................#.................... +.......#.................#...............................##...............................................#.............#......... +.#..............#.....................................................................................................#........... +.......##.................#................#.#...#......................................#...............#...#..................... +.............##......................................................#....................................#..........#............ +....................................#............#............#............#.....................................#................ +................#..#............................#..............................................#.....#...................#......#. +....................#....#......#..................#................#............................................................. +...............##...............#...#..........................#......#...............#..#................#.....#.............#... +...........#..........#..........#..#.........#....#...#......#..............#................#....#..#........................... +#.....................#..#...................................#...#..............................#......#.#.#...................... +............##...................#..#............................................................................................. +..............................................#.#.......................#.................#....#..................#.......#....... +....#................#......#........................................................#........#..............#.................... +.........#.#...............#............................###...........#........................................................... +.........#.................................................................................#......#...#..............#......#..... +....#....#......#.#.........#..........#...............#.........#............##.............#..#............................#.... +....#..#...#.....#...........................#.#.......................................#...#.....................#............#..# +...........#............................#........#...#......#...............................................#...#................. +..#...............................................................#..............................................#.........#...... +...................#.....................................#....................................#.#.#............#............#..... +.....#.................#...........................##.................#..#........................................................ +...#.......#...................................#.............................#...........#......##.#...#.........#.#...........#.. +.#.............#......#..#...#......................................................#..............#.............#.#.............. +.......................#............#...................#.....................#.......................#.............#..#......#... +......................#..#.#.......#........#....#.............#.........#..............................#.#..........#........#... +.................#.............#..#..........#...#.........#..................................#..#.............#................#. +.#..................................#....#...........................................#..............................#...#......... diff --git a/day06/sample1.in b/day06/sample1.in new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/day06/sample1.in @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... diff --git a/day06/sample2.in b/day06/sample2.in new file mode 100644 index 0000000..87a9461 --- /dev/null +++ b/day06/sample2.in @@ -0,0 +1,10 @@ +....#..... +....^....# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... diff --git a/day06/sample3.in b/day06/sample3.in new file mode 100644 index 0000000..aee61de --- /dev/null +++ b/day06/sample3.in @@ -0,0 +1,10 @@ +....#..... +........># +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#... diff --git a/day06/sample4.in b/day06/sample4.in new file mode 100644 index 0000000..019aa63 --- /dev/null +++ b/day06/sample4.in @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#......v. +........#. +#......... +......#... diff --git a/day06/sample5.in b/day06/sample5.in new file mode 100644 index 0000000..ea145a0 --- /dev/null +++ b/day06/sample5.in @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#........ +........#. +#......... +......#v.. diff --git a/day06/sample6.in b/day06/sample6.in new file mode 100644 index 0000000..197af00 --- /dev/null +++ b/day06/sample6.in @@ -0,0 +1,10 @@ +....#..... +....XXXXX# +....X...X. +..#.X...X. +..XXXXX#X. +..X.X.X.X. +.#XXXXXXX. +.XXXXXXX#. +#XXXXXXX.. +......#X.. diff --git a/day06/sol.c b/day06/sol.c new file mode 100644 index 0000000..f93268f --- /dev/null +++ b/day06/sol.c @@ -0,0 +1,148 @@ +#include +#include +#include +#include +#include + +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]); +}