diff --git a/day16/Makefile b/day16/Makefile index 211cf64..0ea01ad 100644 --- a/day16/Makefile +++ b/day16/Makefile @@ -15,13 +15,13 @@ output: compile: ex1-c ex2-c -ex1: +ex1: ex1-c @$(TIME) ex1.bash < $(INPUT) 2>&1 - @$(TIME) ex1-c 2020 < $(INPUT) 2>&1 + @$(TIME) ex1-c < $(INPUT) 2>&1 -ex2: +ex2: ex2-c @$(TIME) ex2.bash < $(INPUT) 2>&1 - @#$(TIME) ex1-c 30000000 < $(INPUT) 2>&1 + @$(TIME) ex2-c < $(INPUT) 2>&1 clean: @rm -f ex1-c ex2-c core diff --git a/day16/ex1-c.c b/day16/ex1-c.c index 74d93dc..e41880d 100644 --- a/day16/ex1-c.c +++ b/day16/ex1-c.c @@ -5,9 +5,6 @@ #include #include -#define AA "ABCD" -#define LAA sizeof(AA) - /* tickets ranges */ struct srange { char *name; diff --git a/day16/ex1.bash b/day16/ex1.bash index 31fc468..0b8f387 100755 --- a/day16/ex1.bash +++ b/day16/ex1.bash @@ -5,7 +5,7 @@ CMD=${0##*/} #shopt -s extglob -declare -A valid=() +declare -A VALID=() declare -i state=0 res=0 while read -r line; do @@ -13,7 +13,7 @@ while read -r line; do n1=$(seq "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}") n2=$(seq "${BASH_REMATCH[5]}" "${BASH_REMATCH[6]}") for i in $n1 $n2; do - valid[$i]=1 + VALID[$i]=1 done elif [[ $line =~ (your ticket:|nearby tickets) ]]; then ((state++)) @@ -21,7 +21,7 @@ while read -r line; do if ((state == 2)); then for i in ${line//,/ }; do # shellcheck disable=SC2100 - [[ ! -v valid[$i] ]] && res=res+i + [[ ! -v VALID[$i] ]] && res=res+i done fi fi diff --git a/day16/ex2-c.c b/day16/ex2-c.c new file mode 100644 index 0000000..3ec0cc2 --- /dev/null +++ b/day16/ex2-c.c @@ -0,0 +1,243 @@ +/* ex1-c: Advent2020 game, day 16/game 1 + */ + +#include +#include +#include + +/* tickets ranges */ +struct srange { + char *name; + int s1, e1; + int s2, e2; + int col; + int possiblecount; + int *possible; /* possible[i]=-1 => column i cannot be */ +}; + +struct list { + int size; + int last; + struct srange *ranges; +} list = { + 0, 0, NULL +}; + +#define BLOCKSIZE 1024 /* number of elements for realloc() */ + +int *myticket=NULL; +int *curticket=NULL; /* current ticket */ +int ticketsize=0; + +void print_ranges() +{ + unsigned i, n=list.last; + int j; + struct srange *p=list.ranges; + + printf("RANGES: address=%p last=%u size=%u\n", list.ranges, n, list.size); + for (i=0; is1, p->e1, p->s2, p->e2, p->name); + printf(" Possible cols (%d remaining) : ", p->possiblecount); + for (j=0; jpossible[j] >= 0) + printf(" %d", p->possible[j]); + putchar('\n'); + } +} + +struct srange *add_range(name, s1, e1, s2, e2) + char *name; + int s1, e1, s2, e2; +{ + struct srange *p; + + if (list.last == list.size) { + list.ranges=realloc(list.ranges, sizeof(struct srange)*BLOCKSIZE); + list.size+=BLOCKSIZE; + } + p=list.ranges+list.last; + p->name=strdup(name); + p->s1=s1; + p->e1=e1; + p->s2=s2; + p->e2=e2; + p->col=-1; + list.last++; + return p; +} + +void print_ticket(ticket) + int *ticket; +{ + int i; + printf("Ticket: "); + for (i=0; i=range->s1 && i<=range->e1) || (i>=range->s2 && i<=range->e2)) + return 1; + else + return 0; +} + +int check_valid(ticket) + int *ticket; +{ + int i, r, found; + + for (i=0; icol >= 0) + continue; + if (range->possiblecount == 1) { + for (i=0; range->possible[i]==-1 && icol=i; + for (r1=0; r1possible[i] != -1) { + range1->possible[i]=-1; + range1->possiblecount--; + unique=0; + } + } + break; + } + } + } + for (r=0; rname, "departure", 9)) { + res*=myticket[range->col]; + } + } + return res; +} + +int main(ac, av) + int ac; + char **av; +{ + char line[1024], pname[80], *elabel; + int s1, e1, s2, e2, end=0, status=0; + unsigned long res=0; + int *ticket; + + while (fgets(line, sizeof line, stdin)) { + if (*line=='\n') + continue; + if (*line >= '0' && *line <= '9') { + switch (status) { + case 1: + ticket=parse_myticket(line); + // print_ranges(); + break; + case 2: + ticket=parse_ticket(line); + // valid ticket + // print_ticket(ticket); + ex2_add(ticket); + break; + } + continue; + } + if (elabel=strchr(line, ':')) { + if (*(elabel+1)=='\n') { + status++; + continue; + } + sscanf(line, "%[^:]: %d-%d or %d-%d %n", + pname, &s1, &e1, &s2, &e2, &end); + add_range(pname, s1, e1, s2, e2); + + } + } + res=ex2(); + printf("%s : res=%lu\n", *av, res); + exit (0); +}