-
Notifications
You must be signed in to change notification settings - Fork 0
/
cucumbers.c
106 lines (90 loc) · 2.39 KB
/
cucumbers.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 160
struct Map {
unsigned int width;
unsigned int height;
char data[MAX_SIZE * MAX_SIZE];
};
struct Map *read_input(void) {
struct Map *map = malloc(sizeof(struct Map));
memset(map, 0, sizeof(struct Map));
char buffer[1024];
while(fgets(buffer, sizeof(buffer), stdin) != NULL) {
unsigned int buflen = strlen(buffer) - 1; // Subtract trailing newline
if(map->width > 0) {
if(map->width != buflen) {
fprintf(
stderr,
"Inconsistent input row length: expected %u bytes, got %u\n",
map->width, buflen
);
exit(EXIT_FAILURE);
}
} else {
if(buflen > MAX_SIZE) {
fprintf(stderr, "Max width (%d) exceeded\n", MAX_SIZE);
exit(EXIT_FAILURE);
}
map->width = buflen;
}
if(map->height == MAX_SIZE) {
fprintf(stderr, "Max width (%d) exceeded\n", MAX_SIZE);
exit(EXIT_FAILURE);
}
memcpy(map->data + (map->height * map->width), buffer, map->width);
map->height += 1;
}
return map;
}
unsigned int calc_step(struct Map *map) {
unsigned int count = 0;
unsigned int datalen = map->width * map->height;
for(unsigned int y = 0; y < map->height; ++y) {
for(unsigned int x = 0; x < map->width; ++x) {
const unsigned int offset = (y * map->width) + x;
if(map->data[offset] != '>') continue;
const unsigned int new_off = (y * map->width) + ((x + 1) % map->width);
if(map->data[new_off] == '.') {
map->data[offset] = 'x';
map->data[new_off] = '!';
++count;
}
}
}
for(unsigned int i = 0; i < datalen; ++i) {
if(map->data[i] == '!')
map->data[i] = '>';
else if(map->data[i] == 'x')
map->data[i] = '.';
}
for(unsigned int y = 0; y < map->height; ++y) {
for(unsigned int x = 0; x < map->width; ++x) {
const unsigned int offset = (y * map->width) + x;
if(map->data[offset] != 'v') continue;
const unsigned int new_off = (((y + 1) % map->height) * map->width) + x;
if(map->data[new_off] == '.') {
map->data[offset] = 'x';
map->data[new_off] = '!';
++count;
}
}
}
for(unsigned int i = 0; i < datalen; ++i) {
if(map->data[i] == '!')
map->data[i] = 'v';
else if(map->data[i] == 'x')
map->data[i] = '.';
}
return count;
}
int main(void) {
struct Map *map = read_input();
unsigned int steps;
for(steps = 1; calc_step(map) != 0; ++steps);
print(map);
printf("Result: %d\n", steps);
free(map);
return 0;
}