summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--5.c (renamed from 5a.c)57
-rw-r--r--makefile2
2 files changed, 56 insertions, 3 deletions
diff --git a/5a.c b/5.c
index 1fc80d0..f404440 100644
--- a/5a.c
+++ b/5.c
@@ -4,6 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
struct crate {
@@ -13,7 +14,7 @@ struct crate {
int
-crates_move(struct crate ** stack, int stacks, int dest, int src, int count)
+crates_move_9000(struct crate ** stack, int stacks, int dest, int src, int count)
{
int i;
@@ -45,6 +46,39 @@ crates_move(struct crate ** stack, int stacks, int dest, int src, int count)
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;
@@ -65,13 +99,32 @@ crates_dump(struct crate ** stack, int stacks)
int
-main()
+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;
diff --git a/makefile b/makefile
index cd67fac..a13a109 100644
--- a/makefile
+++ b/makefile
@@ -5,7 +5,7 @@ all: bin/1a bin/1b \
bin/2a bin/2b \
bin/3a bin/3b \
bin/4a bin/4b \
- bin/5a \
+ bin/5 \
bin/%: %.c
$(CC) $(CFLAGS) -o $@ $<