day 6 part 1
This commit is contained in:
parent
ba3639a098
commit
c40379371e
2
day06/Makefile
Normal file
2
day06/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
CFLAGS=-O2 -std=c99 -Wall -Wextra
|
||||||
|
all: sol
|
130
day06/input
Normal file
130
day06/input
Normal file
@ -0,0 +1,130 @@
|
|||||||
|
....#.....#......#...............................#...........#.......#.....#................#...#....#....#................#..#.#.
|
||||||
|
...................................................................................#...........#............#.....................
|
||||||
|
..........#..............#.................................................#..............#.......................................
|
||||||
|
....................#...............#...#.....................#..............................#....#.............................#.
|
||||||
|
...............................##...............................................................#............#....................
|
||||||
|
................#...........#................#...............#....#.......#.............................#..........#...........#..
|
||||||
|
...........................#................................................#.##..................#.............#........#........
|
||||||
|
......#......#.....#..........#.......................#..........#.............#.....................#........#....#..............
|
||||||
|
..................................................#..................................#....#.........#...........#..............#..
|
||||||
|
..................................#..................#..........................................#.........#....................#..
|
||||||
|
#.................#....................#...........#....................................#......................#..................
|
||||||
|
.......#..........................................................................................................#...............
|
||||||
|
...............##.............................#...#.....................#..........................#.......................#......
|
||||||
|
...#.......................................................#..........................................#...................#..#....
|
||||||
|
.......#...#.......#.#.............#.....#.##.........................#...........................................................
|
||||||
|
#................#.........#.......................##............................................#...#.....#........#.......#.....
|
||||||
|
#......##..............................................................#......#....#....#....#.#...............#............#..#..
|
||||||
|
.....#...............................##.......................................#.......#......#............................#.....#.
|
||||||
|
.........#......#...............................#.....#.....#...................##......#.........................................
|
||||||
|
.#.................................................#...............#........#............................#........................
|
||||||
|
#.........#.........................................#.......................#..............#......................#...........#...
|
||||||
|
..........................................##.............................................................##..#.......#............
|
||||||
|
...#.........................#...........................................#........#...............................................
|
||||||
|
#..................#............#.................................................................#.#....................#....#..#
|
||||||
|
...................#.....................................#.#.........#.............#......#............................##.........
|
||||||
|
....#..............................................................##.......................................................#....#
|
||||||
|
.....................#.....#.............................................#.......................#..................#.............
|
||||||
|
...........#.......................................#................#.........#...............#.#...........#.....#.....#........#
|
||||||
|
.#..#.................#....#...............................#.................................................##...................
|
||||||
|
..#.....#......#..........#...................#..#..................................................#.......#...#.................
|
||||||
|
.............................................................#.......................#..........#.....#...................#.......
|
||||||
|
.......#.......................#.#....#...#.....##.......................#.................................................#......
|
||||||
|
.................#.....................#...............................................................#..#.......................
|
||||||
|
...#.................#.#..................................................##......................................#..#............
|
||||||
|
#........................................................#...#.........#................#.#.......................................
|
||||||
|
.................#........#.#........................#.#..................................#.......................................
|
||||||
|
..........#..#......#..................#.........................#................................#...............................
|
||||||
|
.............................................................................................#....................................
|
||||||
|
.....................#...........#........##.....#.........................#..............................#.....#....#............
|
||||||
|
..#........#.........#.....................................................#......................................................
|
||||||
|
...................................#...................#............#.............#...........................#...................
|
||||||
|
....#.........................................................#................................#......#...#..#....................
|
||||||
|
................................................................................#..#.......#..........................#....#......
|
||||||
|
#...........................................................................#........................#............................
|
||||||
|
..#..............#..........#...#..........................................................#......................................
|
||||||
|
.......................................................#.................#.#....#.................#..........#....#...............
|
||||||
|
..................................................................................................................................
|
||||||
|
.................#.........................................................#..........#.........#.........#.................#.....
|
||||||
|
....................#........#...................#................#...#....................#..............#................#......
|
||||||
|
...#.............##........................................#.........#.................#...........#.........#....................
|
||||||
|
................#............#...........#...............#.....................................#...........................#......
|
||||||
|
....................................................#...........#..............................#..........#...........#...........
|
||||||
|
.........#..............#................................#.............#......................................#.........#...#.....
|
||||||
|
..#..#.........................................#.............................#....................................................
|
||||||
|
.#............................##...#..........#............#..#.........................#......................#...#..............
|
||||||
|
........#.....#..............#..................................................#...#......................#....#..............#..
|
||||||
|
...............................................#.........#..................#............#.............................#..........
|
||||||
|
..................................................................................................#....#..........................
|
||||||
|
..............#..............................................................#....................................................
|
||||||
|
.....#....#.....................#......................#...........#...#..#.............#.......................#.................
|
||||||
|
.............................#...#..#...........................#............................................................#....
|
||||||
|
.............................................#...............................#...................................................#
|
||||||
|
..................................................................#......#....#.......................#.......#........#..........
|
||||||
|
..................#.#.........................................................................................##.......#........#.
|
||||||
|
.............................#.......................#.......................................##...................................
|
||||||
|
.....#.............................................................................................................#.#............
|
||||||
|
...............##.........#..#.....#...............#....................#.................................#.#....#................
|
||||||
|
................#......................................................................#...............#...................#......
|
||||||
|
.................#..............#.................................................................................................
|
||||||
|
............#...#...........................................................................................#......#..............
|
||||||
|
..#.....................................................................#.....#.#.#......#....#..........#........................
|
||||||
|
.#........#.............................#.....................#.....................#....#........................................
|
||||||
|
.......................................................................................................#..........................
|
||||||
|
...#........#..................................................#.........................................................#......#.
|
||||||
|
.......#.......#..#.................................................................................................#.............
|
||||||
|
..............................#..#..........................#..#..................#.............#.................................
|
||||||
|
..................#...#.............................................#...................#.......#...............#.................
|
||||||
|
...#.......#........#.....................................#...........#.................................................#.........
|
||||||
|
.#.............#....................................#........................................................##............#...#..
|
||||||
|
...............#.....#....................................................................................#....#................#.
|
||||||
|
....#...........................................................#.....#..............................................#............
|
||||||
|
.........#..........#...........................#.......................................#....#....................#...............
|
||||||
|
...............................................................#..........#......^...............................................#
|
||||||
|
#..........................#.....................#..............................#..............................#..................
|
||||||
|
...................#.....#......#...........#........#............#.......#.......#...............................................
|
||||||
|
.....#.....#.............................................................#..............#........#..........................#....#
|
||||||
|
............#...............................................................#.............#......................#................
|
||||||
|
.....#...............................................##........................................#.......#......#...................
|
||||||
|
.........................................#........................................................................................
|
||||||
|
..........................#...............................#............#............................#.....#.......................
|
||||||
|
...##.........#......#.....#...............................................................#........#.............................
|
||||||
|
......#...............#.#......................#.......#..............................#...#.........#.........#..#.........#......
|
||||||
|
....#......#....#........#.#.............................#......................................................#.....#...........
|
||||||
|
.....#.............#.....................#...#...............#....................................................................
|
||||||
|
......#............#............#.#............#....................#.............................................................
|
||||||
|
.............#..................................................#....................#.............#.......#.............#........
|
||||||
|
.............................................#......................................................................#.............
|
||||||
|
...............................#............................................................#................#...................#
|
||||||
|
............#.....#....................................................................................................#.#........
|
||||||
|
.........................#....................#...##.......#............#..#.................................#......#.............
|
||||||
|
....#......................#.....................#...........#.........................................#......#.#.................
|
||||||
|
..................................................................#..........................#......................#.............
|
||||||
|
.......................#......#..............................................................................#....................
|
||||||
|
.......#.................#...............................##...............................................#.............#.........
|
||||||
|
.#..............#.....................................................................................................#...........
|
||||||
|
.......##.................#................#.#...#......................................#...............#...#.....................
|
||||||
|
.............##......................................................#....................................#..........#............
|
||||||
|
....................................#............#............#............#.....................................#................
|
||||||
|
................#..#............................#..............................................#.....#...................#......#.
|
||||||
|
....................#....#......#..................#................#.............................................................
|
||||||
|
...............##...............#...#..........................#......#...............#..#................#.....#.............#...
|
||||||
|
...........#..........#..........#..#.........#....#...#......#..............#................#....#..#...........................
|
||||||
|
#.....................#..#...................................#...#..............................#......#.#.#......................
|
||||||
|
............##...................#..#.............................................................................................
|
||||||
|
..............................................#.#.......................#.................#....#..................#.......#.......
|
||||||
|
....#................#......#........................................................#........#..............#....................
|
||||||
|
.........#.#...............#............................###...........#...........................................................
|
||||||
|
.........#.................................................................................#......#...#..............#......#.....
|
||||||
|
....#....#......#.#.........#..........#...............#.........#............##.............#..#............................#....
|
||||||
|
....#..#...#.....#...........................#.#.......................................#...#.....................#............#..#
|
||||||
|
...........#............................#........#...#......#...............................................#...#.................
|
||||||
|
..#...............................................................#..............................................#.........#......
|
||||||
|
...................#.....................................#....................................#.#.#............#............#.....
|
||||||
|
.....#.................#...........................##.................#..#........................................................
|
||||||
|
...#.......#...................................#.............................#...........#......##.#...#.........#.#...........#..
|
||||||
|
.#.............#......#..#...#......................................................#..............#.............#.#..............
|
||||||
|
.......................#............#...................#.....................#.......................#.............#..#......#...
|
||||||
|
......................#..#.#.......#........#....#.............#.........#..............................#.#..........#........#...
|
||||||
|
.................#.............#..#..........#...#.........#..................................#..#.............#................#.
|
||||||
|
.#..................................#....#...........................................#..............................#...#.........
|
10
day06/sample1.in
Normal file
10
day06/sample1.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#..^.....
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
10
day06/sample2.in
Normal file
10
day06/sample2.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
....^....#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#........
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
10
day06/sample3.in
Normal file
10
day06/sample3.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
........>#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#........
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
10
day06/sample4.in
Normal file
10
day06/sample4.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#......v.
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#...
|
10
day06/sample5.in
Normal file
10
day06/sample5.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
..#.......
|
||||||
|
.......#..
|
||||||
|
..........
|
||||||
|
.#........
|
||||||
|
........#.
|
||||||
|
#.........
|
||||||
|
......#v..
|
10
day06/sample6.in
Normal file
10
day06/sample6.in
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
....#.....
|
||||||
|
....XXXXX#
|
||||||
|
....X...X.
|
||||||
|
..#.X...X.
|
||||||
|
..XXXXX#X.
|
||||||
|
..X.X.X.X.
|
||||||
|
.#XXXXXXX.
|
||||||
|
.XXXXXXX#.
|
||||||
|
#XXXXXXX..
|
||||||
|
......#X..
|
148
day06/sol.c
Normal file
148
day06/sol.c
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
#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]);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user