summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--1.c94
-rw-r--r--1a.c34
-rw-r--r--1b.c56
-rw-r--r--makefile2
4 files changed, 95 insertions, 91 deletions
diff --git a/1.c b/1.c
new file mode 100644
index 0000000..e8c84fb
--- /dev/null
+++ b/1.c
@@ -0,0 +1,94 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+#define MAX_ELVES 10
+
+
+#define arrlen(x) (sizeof(x) / sizeof(x[0]))
+
+
+long
+checkmax(long * maxcals, long * maxelfs, int n, int cal, int elf)
+{
+ long i, j;
+
+ for (i = 0; i < n; ++i) {
+ if (cal > maxcals[i]) {
+ if ((j = i + 1) < n) {
+ memmove(maxcals + j, maxcals + i, (n - j) * sizeof(long));
+ memmove(maxelfs + j, maxelfs + i, (n - j) * sizeof(long));
+ }
+ maxcals[i] = cal;
+ maxelfs[i] = elf;
+ break;
+ }
+ }
+
+ return i;
+}
+
+
+int
+main(int argc, char ** argv)
+{
+ char buf[BUFSIZ];
+ int elves = 1, i;
+ long cal = 0, maxcal[MAX_ELVES] = {0}, elf = 0, maxelf[MAX_ELVES] = {0};
+
+ while ((i = getopt(argc, argv, "p:n:")) != -1) {
+ switch (i) {
+ case 'p':
+ switch (atoi(optarg)) {
+ case 1:
+ elves = 1;
+ break;
+
+ case 2:
+ elves = 3;
+ break;
+
+ default:
+ fprintf(stderr, "Unexpected puzzle part %s\n", optarg);
+ return -1;
+ }
+ break;
+
+ case 'n':
+ elves = atoi(optarg);
+ if (elves < 1 || elves > MAX_ELVES) {
+ fprintf(stderr, "Unexpected number of elves %s\n", optarg);
+ return -1;
+ }
+ break;
+
+ default:
+ return -1;
+ }
+ }
+
+ while (fgets(buf, sizeof(buf), stdin)) {
+ char * pbuf = buf;
+ long n;
+ if ((n = strtol(buf, &pbuf, 10)) == 0 && pbuf == buf) {
+ checkmax(maxcal, maxelf, elves, cal, elf);
+ cal = 0;
+ ++elf;
+ }
+ else {
+ cal += n;
+ }
+ }
+ checkmax(maxcal, maxelf, elves, cal, elf);
+
+ for (i = 0, cal = 0; i < elves; ++i)
+ cal += maxcal[i];
+
+ printf("maxcal: %ld\n", cal);
+
+ return 0;
+}
+
diff --git a/1a.c b/1a.c
deleted file mode 100644
index fa93b39..0000000
--- a/1a.c
+++ /dev/null
@@ -1,34 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-int
-main()
-{
- char buf[BUFSIZ];
- long cal = 0, maxcal = 0, elf = 0, maxelf = 0;
-
- while (fgets(buf, sizeof(buf), stdin)) {
- char * pbuf = buf;
- long n;
- if ((n = strtol(buf, &pbuf, 10)) == 0 && pbuf == buf) {
- if (cal > maxcal) {
- maxcal = cal;
- maxelf = elf;
- }
- cal = 0;
- ++elf;
- }
- else {
- cal += n;
- }
- }
- if (cal > maxcal) {
- maxcal = cal;
- maxelf = elf;
- }
-
- printf("maxcal: %ld (elf: %ld)\n", maxcal, maxelf);
-
- return 0;
-}
-
diff --git a/1b.c b/1b.c
deleted file mode 100644
index a1fce37..0000000
--- a/1b.c
+++ /dev/null
@@ -1,56 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-
-#define arrlen(x) (sizeof(x) / sizeof(x[0]))
-
-
-long
-checkmax(long * maxcals, long * maxelfs, int n, int cal, int elf)
-{
- long i, j;
-
- for (i = 0; i < n; ++i) {
- if (cal > maxcals[i]) {
- if ((j = i + 1) < n) {
- memmove(maxcals + j, maxcals + i, (n - j) * sizeof(long));
- memmove(maxelfs + j, maxelfs + i, (n - j) * sizeof(long));
- }
- maxcals[i] = cal;
- maxelfs[i] = elf;
- break;
- }
- }
-
- return i;
-}
-
-
-int
-main()
-{
- char buf[BUFSIZ];
- long cal = 0, maxcal[3] = {0}, elf = 0, maxelf[3] = {0};
-
- while (fgets(buf, sizeof(buf), stdin)) {
- char * pbuf = buf;
- long n;
- if ((n = strtol(buf, &pbuf, 10)) == 0 && pbuf == buf) {
- checkmax(maxcal, maxelf, arrlen(maxcal), cal, elf);
- cal = 0;
- ++elf;
- }
- else {
- cal += n;
- }
- }
- checkmax(maxcal, maxelf, arrlen(maxcal), cal, elf);
-
- printf("maxcal: %ld (elf: %ld/%ld/%ld)\n",
- maxcal[0] + maxcal[1] + maxcal[2],
- maxelf[0], maxelf[1], maxelf[2]);
-
- return 0;
-}
-
diff --git a/makefile b/makefile
index 45e5299..0d75c03 100644
--- a/makefile
+++ b/makefile
@@ -2,7 +2,7 @@
CFLAGS=-Wall -O2
all: bin \
- bin/1a bin/1b \
+ bin/1 \
bin/2a bin/2b \
bin/3a bin/3b \
bin/4a bin/4b \