summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--4.c (renamed from 4a.c)42
-rw-r--r--4b.c51
-rw-r--r--makefile2
3 files changed, 36 insertions, 59 deletions
diff --git a/4a.c b/4.c
index 76d5e0c..1f183b7 100644
--- a/4a.c
+++ b/4.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
char *
@@ -22,10 +23,21 @@ getsect(int * dest, char * pch, char expect)
int
-main()
+main(int argc, char ** argv)
{
char buf[BUFSIZ];
- int overlaps = 0;
+ int part = 1, i, overlaps = 0;
+
+ while ((i = getopt(argc, argv, "p:")) != -1) {
+ switch (i) {
+ case 'p':
+ part = atoi(optarg);
+ break;
+
+ default:
+ return -1;
+ }
+ }
while (fgets(buf, sizeof(buf), stdin)) {
int n[4];
@@ -39,14 +51,30 @@ main()
fprintf(stderr, "Unexpected line: %s\n", buf);
return -1;
}
- if ((n[2] >= n[0] && n[3] <= n[1])
- || (n[0] >= n[2] && n[1] <= n[3]))
- {
- ++overlaps;
+ switch (part) {
+ case 1:
+ if ((n[2] >= n[0] && n[3] <= n[1])
+ || (n[0] >= n[2] && n[1] <= n[3]))
+ {
+ ++overlaps;
+ }
+ break;
+
+ case 2:
+ if (!(n[3] < n[0] || n[2] > n[1])) {
+ ++overlaps;
+ }
+ break;
+
+ default:
+ fprintf(stderr, "Unexpected puzzle part %d\n", part);
+ return -1;
}
}
- printf("Overlaps: %d\n", overlaps);
+ printf("%s overlaps: %d\n",
+ part == 1 ? "Full" : "Partial",
+ overlaps);
return 0;
}
diff --git a/4b.c b/4b.c
deleted file mode 100644
index 6f43c21..0000000
--- a/4b.c
+++ /dev/null
@@ -1,51 +0,0 @@
-
-#include <stdio.h>
-#include <stdlib.h>
-
-
-char *
-getsect(int * dest, char * pch, char expect)
-{
- char * end;
-
- if (!pch)
- return NULL;
- *dest = strtol(pch, &end, 10);
- if (*dest == 0 && end == pch)
- return NULL;
- if (*end != expect)
- return NULL;
- if (expect)
- ++end;
- return end;
-}
-
-
-int
-main()
-{
- char buf[BUFSIZ];
- int overlaps = 0;
-
- while (fgets(buf, sizeof(buf), stdin)) {
- int n[4];
- char * pbuf;
-
- pbuf = getsect(&n[0], buf, '-');
- pbuf = getsect(&n[1], pbuf, ',');
- pbuf = getsect(&n[2], pbuf, '-');
- pbuf = getsect(&n[3], pbuf, '\n');
- if (!pbuf) {
- fprintf(stderr, "Unexpected line: %s\n", buf);
- return -1;
- }
- if (!(n[3] < n[0] || n[2] > n[1])) {
- ++overlaps;
- }
- }
-
- printf("Overlaps: %d\n", overlaps);
-
- return 0;
-}
-
diff --git a/makefile b/makefile
index ad777c2..21ddbb3 100644
--- a/makefile
+++ b/makefile
@@ -5,7 +5,7 @@ all: bin \
bin/1 \
bin/2 \
bin/3 \
- bin/4a bin/4b \
+ bin/4 \
bin/5 \
bin/6 \
bin/7 \