aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

10a.c (1457B)


      1 #include <stdio.h>
      2 
      3 #define N 1000
      4 
      5 typedef enum { EAST, SOUTH, WEST, NORTH } direction_t;
      6 
      7 int main() {
      8 	char map[N][N];
      9 	int i, j, k, n, s;
     10 	direction_t dir;
     11 
     12 	for (n = 0; fgets(map[n], N, stdin) != NULL; n++) ;
     13 
     14 	for (i = 0; i < n; i++)
     15 		for (j = 0; map[i][j]; j++)
     16 			if (map[i][j] == 'S')
     17 				goto found_s;
     18 
     19 found_s:
     20 	if (map[i][j+1] ==  '7' || map[i][j+1] == '-' || map[i][j+1] == 'J') {
     21 		j++;
     22 		dir = EAST;
     23 		goto found_direction;
     24 	}
     25 	if (map[i+1][j] ==  'L' || map[i+1][j] == '|' || map[i+1][j] == 'J') {
     26 		i++;
     27 		dir = SOUTH;
     28 		goto found_direction;
     29 	}
     30 	if (j > 0 && (map[i][j-1] ==  'L' || map[i][j-1] == '-' || map[i][j-1] == 'F')) {
     31 		j--;
     32 		dir = WEST;
     33 		goto found_direction;
     34 	}
     35 
     36 found_direction:
     37 	for (k = 1; map[i][j] != 'S'; k++) {
     38 		switch (map[i][j]) {
     39 		case '|':
     40 			if (dir == SOUTH) i++;
     41 			if (dir == NORTH) i--;
     42 			break;
     43 		case '-':
     44 			if (dir == EAST) j++;
     45 			if (dir == WEST) j--;
     46 			break;
     47 		case '7':
     48 			if (dir == EAST) { i++; dir = SOUTH; }
     49 			if (dir == NORTH) { j--; dir = WEST; }
     50 			break;
     51 		case 'F':
     52 			if (dir == WEST) { i++; dir = SOUTH; }
     53 			if (dir == NORTH) { j++; dir = EAST; }
     54 			break;
     55 		case 'J':
     56 			if (dir == EAST) { i--; dir = NORTH; }
     57 			if (dir == SOUTH) { j--; dir = WEST; }
     58 			break;
     59 		case 'L':
     60 			if (dir == WEST) { i--; dir = NORTH; }
     61 			if (dir == SOUTH) { j++; dir = EAST; }
     62 			break;
     63 		default:
     64 			printf("Error: dead path\n");
     65 			return 1;
     66 		}
     67 	}
     68 
     69 	s = (k+1)/2;
     70 
     71 	printf("%d\n", s);
     72 	return 0;
     73 }