summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan C <ianc@noddybox.co.uk>2021-12-09 23:50:11 +0000
committerIan C <ianc@noddybox.co.uk>2021-12-09 23:50:11 +0000
commitc52a19c71cf16346c5de5e21425063047311b786 (patch)
tree2ae3b42e8f4620e549b1d966ebc0d5e9aa1de95b
parent1dfae1e03aee84b73d0114dfa16d827373ea6ce7 (diff)
Added day 8. Part 2 doesn't work.
-rw-r--r--8.c53
-rw-r--r--8a.c111
-rw-r--r--data.8a10
3 files changed, 174 insertions, 0 deletions
diff --git a/8.c b/8.c
new file mode 100644
index 0000000..c0bf7c5
--- /dev/null
+++ b/8.c
@@ -0,0 +1,53 @@
+#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;
+}
+
+int main(void)
+{
+ char buff[0x8000];
+ char *signal[10] = {0};
+ char *digit[4] = {0};
+ int sum = 0;
+ int f = 0;
+
+ while(ReadLine(buff, sizeof buff, stdin))
+ {
+ 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, " |");
+
+ len = strlen(digit[f]);
+
+ if (len == 2 || len == 4 || len == 3 || len == 7)
+ {
+ sum++;
+ }
+ }
+ }
+
+ printf("sum = %d\n", sum);
+
+ return 0;
+}
diff --git a/8a.c b/8a.c
new file mode 100644
index 0000000..9619dfc
--- /dev/null
+++ b/8a.c
@@ -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;
+}
diff --git a/data.8a b/data.8a
new file mode 100644
index 0000000..c9f629b
--- /dev/null
+++ b/data.8a
@@ -0,0 +1,10 @@
+be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
+edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
+fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
+fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
+aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
+fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
+dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
+bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
+egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
+gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce