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
182
183
184
185
186
187
188
189
|
#include <ctype.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct crate {
struct crate * next;
char id;
};
int
crates_move_9000(struct crate ** stack, int stacks, int dest, int src, int count)
{
int i;
if (dest < 0 || dest >= stacks
|| src < 0 || src >= stacks)
{
fprintf(stderr, "Invalid src/dest stack (%d/%d/%d)\n",
stacks, dest, src);
return -1;
}
for (i = 0; i < count; ++i) {
struct crate * c;
if (stack[src] == NULL) {
fprintf(stderr, "No crates available in stack %d\n",
src);
return -1;
}
c = stack[src];
stack[src] = c->next;
c->next = stack[dest];
stack[dest] = c;
}
return 0;
}
int
crates_move_9001(struct crate ** stack, int stacks, int dest, int src, int count)
{
int i;
struct crate * first, * last;
if (dest < 0 || dest >= stacks
|| src < 0 || src >= stacks)
{
fprintf(stderr, "Invalid src/dest stack (%d/%d/%d)\n",
stacks, dest, src);
return -1;
}
last = first = stack[src];
for (i = 1; i < count; ++i) {
if (last)
last = last->next;
}
if (!last) {
fprintf(stderr, "Fewer than %d crates available in stack %d\n",
count, src);
return -1;
}
stack[src] = last->next;
last->next = stack[dest];
stack[dest] = first;
return 0;
}
int
crates_dump(struct crate ** stack, int stacks)
{
int i;
for (i = 0; i < stacks; ++i) {
struct crate * c;
fprintf(stderr, "%d:", i + 1);
for (c = stack[i]; c != NULL; c = c->next) {
fprintf(stderr, " %c", c->id);
}
fprintf(stderr, "\n");
}
fprintf(stderr, "\n");
return 0;
}
int
main(int argc, char ** argv)
{
int (*crates_move)(struct crate **, int, int, int, int) = crates_move_9000;
char buf[BUFSIZ];
int stacks = 0, i;
struct crate ** stack = NULL;
regex_t movecmd;
while ((i = getopt(argc, argv, "m:")) != -1) {
switch (i) {
case 'm':
if (strcmp(optarg, "9000") == 0)
crates_move = crates_move_9000;
else if (strcmp(optarg, "9001") == 0)
crates_move = crates_move_9001;
else {
fprintf(stderr, "Unknown CrateMover %s\n", optarg);
return -1;
}
break;
default:
return -1;
}
}
while (fgets(buf, sizeof(buf), stdin)) {
int len;
len = strlen(buf);
if (len == 1 && buf[0] == '\n') {
break;
}
if (stacks == 0 && stack == NULL) {
if (len % 4 != 0) {
fprintf(stderr, "Unexpected first line width: %d\n", len);
return -1;
}
stacks = len / 4;
stack = calloc(stacks, sizeof(struct crate *));
}
if (len != stacks * 4) {
fprintf(stderr, "Unexpected input line width: %d/%d\n", len, stacks * 4);
return -1;
}
for (i = 0; i < stacks; ++i) {
if (isupper(buf[i * 4 + 1])) {
struct crate ** pc = stack + i;
while (*pc != NULL)
pc = &(*pc)->next;
*pc = malloc(sizeof(struct crate));
(*pc)->next = NULL;
(*pc)->id = buf[i * 4 + 1];
}
}
#ifdef CRATES_DUMP
crates_dump(stack, stacks);
#endif
}
regcomp(&movecmd, "move ([[:digit:]]+) from ([[:digit:]]+) to ([[:digit:]]+)\n", REG_EXTENDED);
while (fgets(buf, sizeof(buf), stdin)) {
regmatch_t matches[4];
if (regexec(&movecmd, buf, 4, matches, 0) != 0) {
fprintf(stderr, "Unexpected move command: %s\n", buf);
}
crates_move(stack, stacks,
atoi(buf + matches[3].rm_so) - 1,
atoi(buf + matches[2].rm_so) - 1,
atoi(buf + matches[1].rm_so));
#ifdef CRATES_DUMP
crates_dump(stack, stacks);
#endif
}
regfree(&movecmd);
for (i = 0; i < stacks; ++i) {
fputc(stack[i] ? stack[i]->id : ' ', stdout);
}
fputc('\n', stdout);
return 0;
}
|