aboutsummaryrefslogtreecommitdiff
path: root/2023/10/10b.c
blob: d63a4b0970d9a8d93b4cda7494f86ca68e74ffa0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>

#define N 1000
#define set(i, j, c) if (newmap[i][j] == '.') newmap[i][j] = c;

typedef enum { EAST, SOUTH, WEST, NORTH } direction_t;

char map[N+2][N+2], newmap[N+2][N+2];
int n, si, sj, count[26] = {0};
direction_t sdir;

void printmap(char m[][N+2]) {
	for (int i = 1; i < n; i++) {
		for (int j = 1; m[i][j] != 'B'; j++)
			printf("%c", m[i][j]);
		printf("\n");
	}
}

void color_rl(direction_t dir, int i, int j) {
	switch (dir) {
	case EAST:
		set(i-1, j, 'L');
		set(i+1, j, 'R');
		break;
	case SOUTH:
		set(i, j+1, 'L');
		set(i, j-1, 'R');
		break;
	case WEST:
		set(i-1, j, 'R');
		set(i+1, j, 'L');
		break;
	case NORTH:
		set(i, j+1, 'R');
		set(i, j-1, 'L');
		break;
	default:
		break;
	}
}

direction_t firstdir(int i, int j) {
	if (map[i][j+1] ==  '7' || map[i][j+1] == '-' || map[i][j+1] == 'J')
		return EAST;
	if (map[i+1][j] ==  'L' || map[i+1][j] == '|' || map[i+1][j] == 'J')
		return SOUTH;
	return WEST;
}

void advance(int *i, int *j, direction_t dir) {
	if (dir == EAST) { (*j)++; }
	else if (dir == SOUTH) { (*i)++; }
	else if (dir == WEST) { (*j)--; }
	else (*i)--;
}

direction_t newdir(int i, int j, direction_t dir) {
	switch (map[i][j]) {
	case '|':
	case '-':
		return dir;
	case '7':
		if (dir == EAST) return SOUTH;
		if (dir == NORTH) return WEST;
	case 'F':
		if (dir == WEST) return SOUTH;
		if (dir == NORTH) return EAST;
	case 'J':
		if (dir == EAST) return NORTH;
		if (dir == SOUTH) return WEST;
	case 'L':
		if (dir == WEST) return NORTH;
		if (dir == SOUTH) return EAST;
	default:
		printf("Error: dead path at (%d, %d)\n", i, j);
		exit(1);
	}
	return EAST;
}

void walk_and_color_rl(char c) {
	int i = si, j = sj;
	direction_t dir = sdir;

	newmap[i][j] = c;
	color_rl(dir, i, j);
	advance(&i, &j, dir);
	for (; map[i][j] != 'S'; advance(&i, &j, dir)) {
		newmap[i][j] = c;
		color_rl(dir, i, j);
		dir = newdir(i, j, dir);
		color_rl(dir, i, j);
	}
	color_rl(dir, i, j);
}

void fill(int i, int j, char c) {
	if (newmap[i][j] != '.' && newmap[i][j] != 'R' && newmap[i][j] != 'L')
		return;

	newmap[i][j] = c;
	count[c-'a']++;
	fill(i, j+1, c);
	fill(i, j-1, c);
	fill(i+1, j, c);
	fill(i-1, j, c);
}

int main() {
	/* Get input */
	for (n = 1; fgets(&map[n][1], N, stdin) != NULL; n++) ;

	/* Add borders, copy to newmap */
	for (int i = 0; i < N+2; i++) {
		for (int j = 0; j < N+2; j++) {
			newmap[i][j] = map[i][j];
			if (map[i][j] == 0 || map[i][j] == '\n')
				newmap[i][j] = map[i][j] = 'B';
		}
	}

	/* Find S and set the initial direction */
	for (si = 1; si < n; si++)
		for (sj = 1; map[si][sj] != 'B'; sj++)
			if (map[si][sj] == 'S')
				goto found_s;

found_s:
	/* Set the initial direction and walk the path mark it with X */
	sdir = firstdir(si, sj);
	walk_and_color_rl('X');

	/* Replace the unnecessary pipe symbols with dots */
	for (int i = 1; i < N; i++)
		for (int j = 1; map[i][j] != 'B'; j++)
			if (newmap[i][j] != 'X')
				newmap[i][j] = '.';

	/* Walk again the path and mark adjacent dots left and right */
	walk_and_color_rl('Y');

	/* Mark the remaining dots left and right */
	for (int i = 1; i < n; i++)
		for (int j = 1; newmap[i][j] != 'B'; j++)
			if (newmap[i][j] == 'R' || newmap[i][j] == 'L')
				fill(i, j, newmap[i][j] + 'a' - 'A');

	printmap(newmap);
	printf("r: %d\nl: %d\n", count['r'-'a'], count['l'-'a']);
	printf("Check with the map printed above which one is 'inside'\n");
	return 0;
}

Generated with cgit - Back to sebastiano.tronto.net