-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10part2.c
181 lines (159 loc) · 4.42 KB
/
day10part2.c
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#define FILE_NAME "input.txt"
#define SIZE 140
#define NORTH 0b0001
#define EAST 0b0010
#define SOUTH 0b0100
#define WEST 0b1000
// hardcode the type of pipe the start is
// vertical in my case
#define START NORTH | SOUTH
#define S_CHAR '|'
typedef struct Node Node;
struct Node {
int r, c;
Node* next;
};
// bfs queue
Node* front;
Node* back;
void enqueue(int r, int c) {
if (front == NULL) {
front = (Node*)calloc(1, sizeof(Node));
back = front;
front->r = r;
front->c = c;
}
else {
Node* newNode = (Node*)calloc(1, sizeof(Node));
newNode-> r = r;
newNode->c = c;
back->next = newNode;
back = newNode;
}
}
void dequeue(int* r, int* c) {
*r = front->r;
*c = front->c;
Node* newFront = front->next;
free(front);
front = newFront;
}
int main() {
FILE* f = fopen(FILE_NAME, "r");
int map[SIZE][SIZE] = {0};
char mapc[SIZE][SIZE] = {0};
int distances[SIZE][SIZE] = {0};
for (int r = 0; r < SIZE; r++) {
for (int c = 0; c < SIZE; c++) {
distances[r][c] = -1;
int character = fgetc(f);
mapc[r][c] = character;
if (character == '|') {
map[r][c] = NORTH | SOUTH;
}
if (character == '-') {
map[r][c] = EAST | WEST;
}
if (character == 'L') {
map[r][c] = NORTH | EAST;
}
if (character == 'J') {
map[r][c] = NORTH | WEST;
}
if (character == '7') {
map[r][c] = SOUTH | WEST;
}
if (character == 'F') {
map[r][c] = SOUTH | EAST;
}
if (character == '.') {
map[r][c] = 0;
}
if (character == 'S') {
map[r][c] = START;
enqueue(r, c);
distances[r][c] = 0;
mapc[r][c] = S_CHAR;
}
}
fscanf(f, " ");
}
while (front) {
int r, c;
dequeue(&r, &c);
int directions = map[r][c];
if ((directions & NORTH) && r-1 >= 0 && distances[r-1][c] == -1) {
distances[r-1][c] = distances[r][c] + 1;
enqueue(r-1,c);
}
if ((directions & SOUTH) && r+1 < SIZE && distances[r+1][c] == -1) {
distances[r+1][c] = distances[r][c] + 1;
enqueue(r+1,c);
}
if ((directions & WEST) && c-1 >= 0 && distances[r][c-1] == -1) {
distances[r][c-1] = distances[r][c] + 1;
enqueue(r,c-1);
}
if ((directions & EAST) && c+1 < SIZE && distances[r][c+1] == -1) {
distances[r][c+1] = distances[r][c] + 1;
enqueue(r,c+1);
}
}
int count = 0;
// ray casting algorithm
for (int r = 0; r < SIZE; r++) {
for (int c = 0; c < SIZE; c++) {
if (distances[r][c] == -1) {
int inLoop = 0;
for (int c2 = 0; c2 < c; c2++) {
if (distances[r][c2] != -1 && (mapc[r][c2] == 'L' || mapc[r][c2] == 'J' || mapc[r][c2] == '|')) {
inLoop = !inLoop;
}
}
if (inLoop) {
mapc[r][c] = '+';
count ++;
}
else {
mapc[r][c] = '.';
}
}
}
}
// loop looks really pretty if we print it like this
for (int r = 0; r < SIZE; r++) {
for (int c = 0; c < SIZE; c++) {
if (distances[r][c] != -1) {
if (mapc[r][c] == 'F') {
printf("┌");
}
if (mapc[r][c] == '7') {
printf("┐");
}
if (mapc[r][c] == 'L') {
printf("└");
}
if (mapc[r][c] == 'J') {
printf("┘");
}
if (mapc[r][c] == '|') {
printf("│");
}
if (mapc[r][c] == '-') {
printf("─");
}
}
else {
printf("%c", mapc[r][c]);
}
}
printf("\n");
}
printf("%d\n", count);
fclose(f);
}