summaryrefslogtreecommitdiff
path: root/8a.c
diff options
context:
space:
mode:
Diffstat (limited to '8a.c')
-rw-r--r--8a.c49
1 files changed, 29 insertions, 20 deletions
diff --git a/8a.c b/8a.c
index 9619dfc..9c747b2 100644
--- a/8a.c
+++ b/8a.c
@@ -2,6 +2,13 @@
#include <stdio.h>
#include <string.h>
+struct digit
+{
+ char sig[10];
+ int digit;
+ int unique;
+};
+
static char *ReadLine(char *p, size_t size, FILE *fp)
{
if ((p=fgets(p, size, fp)))
@@ -17,27 +24,26 @@ static char *ReadLine(char *p, size_t size, FILE *fp)
return p;
}
-static int GetDigit(const char *p)
+static int CompareLen(const void *a, const void *b)
{
- static struct
- {
- const char *sig;
- int digit;
- int unique;
- } digit[10] =
- {
- {"acedgfb", 8, 1},
- {"cdfbe", 5, 0},
- {"gcdfa", 2, 0},
- {"fbcad", 3, 0},
- {"dab", 7, 1},
- {"cefabd", 9, 0},
- {"cdfgeb", 6, 0},
- {"eafb", 4, 1},
- {"cagedb", 0, 0},
- {"ab", 1, 1}
- };
+ const char **pa = a;
+ const char **pb = b;
+ int l1;
+ int l2;
+
+ l1 = strlen(*pa);
+ l2 = strlen(*pb);
+
+ return l1 - l2;
+}
+
+static int CalcCodes(struct digit digit[10], char *signal[10])
+{
+ qsort(signal, 10, sizeof signal[0], CompareLen);
+}
+static int GetDigit(const struct digit *digit, const char *p)
+{
int check[10] = {0};
int f;
@@ -79,6 +85,7 @@ int main(void)
char buff[0x8000];
char *signal[10] = {0};
char *digit[4] = {0};
+ struct digit dig[10];
int num = 0;
int sum = 0;
int f = 0;
@@ -92,13 +99,15 @@ int main(void)
signal[f] = strtok(f == 0 ? buff : NULL, " |");
}
+ CalcCodes(dig, signal);
+
for(f = 0; f < 4; f++)
{
size_t len;
digit[f] = strtok(NULL, " |");
- num = GetDigit(digit[f]);
+ num = GetDigit(dig, digit[f]);
result[f] = '0' + num;
}