aboutsummaryrefslogtreecommitdiff
path: root/2023/10/10b.c
diff options
context:
space:
mode:
Diffstat (limited to '2023/10/10b.c')
-rw-r--r--2023/10/10b.c156
1 files changed, 156 insertions, 0 deletions
diff --git a/2023/10/10b.c b/2023/10/10b.c
new file mode 100644
index 0000000..0b298dc
--- /dev/null
+++ b/2023/10/10b.c
@@ -0,0 +1,156 @@
1#include <stdbool.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#define N 1000
7#define set(i, j, c) if (newmap[i][j] == '.') newmap[i][j] = c;
8
9typedef enum { EAST, SOUTH, WEST, NORTH } direction_t;
10
11char map[N+2][N+2], newmap[N+2][N+2];
12int n, si, sj, count[26] = {0};
13direction_t sdir;
14
15void printmap(char m[][N+2]) {
16 for (int i = 1; i < n; i++) {
17 for (int j = 1; m[i][j] != 'B'; j++)
18 printf("%c", m[i][j]);
19 printf("\n");
20 }
21}
22
23void color_rl(direction_t dir, int i, int j) {
24 switch (dir) {
25 case EAST:
26 set(i-1, j, 'L');
27 set(i+1, j, 'R');
28 break;
29 case SOUTH:
30 set(i, j+1, 'L');
31 set(i, j-1, 'R');
32 break;
33 case WEST:
34 set(i-1, j, 'R');
35 set(i+1, j, 'L');
36 break;
37 case NORTH:
38 set(i, j+1, 'R');
39 set(i, j-1, 'L');
40 break;
41 default:
42 break;
43 }
44}
45
46direction_t firstdir(int i, int j) {
47 if (map[i][j+1] == '7' || map[i][j+1] == '-' || map[i][j+1] == 'J')
48 return EAST;
49 if (map[i+1][j] == 'L' || map[i+1][j] == '|' || map[i+1][j] == 'J')
50 return SOUTH;
51 return WEST;
52}
53
54void advance(int *i, int *j, direction_t dir) {
55 if (dir == EAST) { (*j)++; }
56 else if (dir == SOUTH) { (*i)++; }
57 else if (dir == WEST) { (*j)--; }
58 else (*i)--;
59}
60
61direction_t newdir(int i, int j, direction_t dir) {
62 switch (map[i][j]) {
63 case '|':
64 case '-':
65 return dir;
66 case '7':
67 if (dir == EAST) return SOUTH;
68 if (dir == NORTH) return WEST;
69 case 'F':
70 if (dir == WEST) return SOUTH;
71 if (dir == NORTH) return EAST;
72 case 'J':
73 if (dir == EAST) return NORTH;
74 if (dir == SOUTH) return WEST;
75 case 'L':
76 if (dir == WEST) return NORTH;
77 if (dir == SOUTH) return EAST;
78 default:
79 printf("Error: dead path at (%d, %d)\n", i, j);
80 exit(1);
81 }
82 return EAST;
83}
84
85void walk_and_color_rl(char c) {
86 int i = si, j = sj;
87 direction_t dir = sdir;
88
89 newmap[i][j] = c;
90 color_rl(dir, i, j);
91 advance(&i, &j, dir);
92 for (; map[i][j] != 'S'; advance(&i, &j, dir)) {
93 newmap[i][j] = c;
94 color_rl(dir, i, j);
95 dir = newdir(i, j, dir);
96 color_rl(dir, i, j);
97 }
98 color_rl(dir, i, j);
99}
100
101void fill(int i, int j, char c) {
102 if (newmap[i][j] != '.' && newmap[i][j] != 'R' && newmap[i][j] != 'L')
103 return;
104
105 newmap[i][j] = c;
106 count[c-'a']++;
107 fill(i, j+1, c);
108 fill(i, j-1, c);
109 fill(i+1, j, c);
110 fill(i-1, j, c);
111}
112
113int main() {
114 /* Get input */
115 for (n = 1; fgets(&map[n][1], N, stdin) != NULL; n++) ;
116
117 /* Add borders, copy to newmap */
118 for (int i = 0; i < N+2; i++) {
119 for (int j = 0; j < N+2; j++) {
120 newmap[i][j] = map[i][j];
121 if (map[i][j] == 0 || map[i][j] == '\n')
122 newmap[i][j] = map[i][j] = 'B';
123 }
124 }
125
126 /* Find S and set the initial direction */
127 for (si = 1; si < n; si++)
128 for (sj = 1; map[si][sj] != 'B'; sj++)
129 if (map[si][sj] == 'S')
130 goto found_s;
131
132found_s:
133 /* Set the initial direction and walk the path mark it with X */
134 sdir = firstdir(si, sj);
135 walk_and_color_rl('X');
136
137 /* Replace the unnecessary pipe symbols with dots */
138 for (int i = 1; i < N; i++)
139 for (int j = 1; map[i][j] != 'B'; j++)
140 if (newmap[i][j] != 'X')
141 newmap[i][j] = '.';
142
143 /* Walk again the path and mark adjacent dots left and right */
144 walk_and_color_rl('Y');
145
146 /* Mark the remaining dots left and right */
147 for (int i = 1; i < n; i++)
148 for (int j = 1; newmap[i][j] != 'B'; j++)
149 if (newmap[i][j] == 'R' || newmap[i][j] == 'L')
150 fill(i, j, newmap[i][j] + 'a' - 'A');
151
152 printmap(newmap);
153 printf("r: %d\nl: %d\n", count['r'-'a'], count['l'-'a']);
154 printf("Check with the map printed above which one is 'inside'\n");
155 return 0;
156}

Generated with cgit - Back to sebastiano.tronto.net