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