diff options
Diffstat (limited to '8a.c')
-rw-r--r-- | 8a.c | 111 |
1 files changed, 111 insertions, 0 deletions
@@ -0,0 +1,111 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static int GetDigit(const char *p) +{ + 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} + }; + + int check[10] = {0}; + int f; + + for(f = 0; f < 10; f++) + { + if (strlen(digit[f].sig) == strlen(p)) + { + int n; + + check[f] = 1; + + if (!digit[f].unique) + { + for(n = 0; n < strlen(p); n++) + { + if(!strchr(digit[f].sig, p[n])) + { + check[f] = 0; + } + } + } + } + } + + for(f = 0; f < 10; f++) + { + if(check[f]) + { + return digit[f].digit; + } + } + + printf("No digit for %s\n", p); + exit(1); +} + +int main(void) +{ + char buff[0x8000]; + char *signal[10] = {0}; + char *digit[4] = {0}; + int num = 0; + int sum = 0; + int f = 0; + + while(ReadLine(buff, sizeof buff, stdin)) + { + char result[5] = "0000"; + + for(f = 0; f < 10; f++) + { + signal[f] = strtok(f == 0 ? buff : NULL, " |"); + } + + for(f = 0; f < 4; f++) + { + size_t len; + + digit[f] = strtok(NULL, " |"); + + num = GetDigit(digit[f]); + result[f] = '0' + num; + } + + sum += atoi(result); + } + + printf("sum = %d\n", sum); + + return 0; +} |