-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc11.c
132 lines (123 loc) · 3.76 KB
/
aoc11.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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
const int dirs[8][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{ 0, -1}, { 0, 1},
{ 1, -1}, { 1, 0}, { 1, 1}
};
int personOnSeatNext(int l, int ll, char *b, size_t lines, size_t lineLen) {
int neibours = 0;
for (int i = 0; i < 8; i++) {
int x = ll + dirs[i][0], y = l + dirs[i][1];
if (x >= 0 && y >= 0 && x < lineLen && y < lines &&
b[y*lineLen + x] == '#') {
neibours++;
}
}
return neibours == 0 ? '#' : neibours > 3 ? 'L' : b[l*lineLen + ll];
}
int personOnSeatNext2(int l, int ll, char *b, size_t lines, size_t lineLen) {
int neibours = 0;
for (int i = 0; i < 8; i++) {
bool notFound = true;
int m = 1;
while (notFound) {
int x = ll + dirs[i][0]*m, y = l + dirs[i][1]*m;
if (x < 0 || y < 0 || x >= lineLen || y >= lines || b[y*lineLen + x] == 'L')
notFound = false;
else if (b[y*lineLen + x] == '#') {
neibours++;
notFound = false;
}
m++;
}
}
return neibours == 0 ? '#' : neibours > 4 ? 'L' : b[l*lineLen + ll];
}
void print(char *b, size_t lines, size_t lineLen) {
for (int i = 0; i < lines; i++) {
for (int j = 0; j < lineLen; j++) {
putchar(*b++);
}
putchar('\n');
}
putchar('\n');
}
int gameOfSeats(char *b, size_t lines, size_t lineLen) {
char cur[lines*lineLen];
int rounds = 0, occupied = 0;
do {
memcpy(cur, b, lines*lineLen);
occupied = 0;
char *bp = b;
for (int i = 0; i < lines; i++) {
for (int j = 0; j < lineLen; j++) {
if (*bp =='#' || *bp =='L') {
*bp = personOnSeatNext(i, j, cur, lines, lineLen);
if (*bp == '#') {
occupied++;
}
}
bp++;
}
}
rounds++;
//printf("occupied %d\n", occupied);
//print(b, lines, lineLen);
} while (memcmp(cur, b, lines*lineLen) != 0);
printf("rounds %d\n", rounds);
return occupied;
}
int gameOfSeats2(char *b, size_t lines, size_t lineLen) {
char cur[lines*lineLen];
int rounds = 0, occupied = 0;
do {
memcpy(cur, b, lines*lineLen);
occupied = 0;
char *bp = b;
for (int i = 0; i < lines; i++) {
for (int j = 0; j < lineLen; j++) {
if (*bp =='#' || *bp =='L') {
*bp = personOnSeatNext2(i, j, cur, lines, lineLen);
if (*bp == '#') {
occupied++;
}
}
bp++;
}
}
rounds++;
//printf("occupied %d\n", occupied);
//print(b, lines, lineLen);
} while (memcmp(cur, b, lines*lineLen) != 0);
printf("rounds %d\n", rounds);
return occupied;
}
int main() {
FILE* f = fopen("input11.txt", "r");
if (f != NULL) {
char buf[300];
size_t lines = 0;
size_t lineLen = 0;
while (fgets(buf, sizeof buf, f) != NULL) {
lines++;
}
fseek(f, 0, SEEK_SET);
lineLen = strlen(buf);
if (buf[lineLen - 1] == 10) { // remove double newline
lineLen--;
}
char block[lineLen*lines+1];
size_t i = 0;
while (fgets(&block[i*lineLen], sizeof buf, f) != NULL) {
i++;
}
char blockCopy[lineLen*lines];
memcpy(blockCopy, block, lineLen*lines);
printf("count 1 = %d\n", gameOfSeats(block, lines, lineLen));
printf("count 2 = %d\n", gameOfSeats2(blockCopy, lines, lineLen));
}
return 0;
}