From 266761708869250f17a931f803d0a8f934973682 Mon Sep 17 00:00:00 2001 From: Bruno Raoult Date: Wed, 13 Jan 2021 08:37:55 +0100 Subject: [PATCH] Day 16/Part 1: C version. --- day16/Makefile | 2 +- day16/OUTPUT | 12 ++++ day16/ex1-c.c | 165 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 day16/OUTPUT create mode 100644 day16/ex1-c.c diff --git a/day16/Makefile b/day16/Makefile index a1d3261..211cf64 100644 --- a/day16/Makefile +++ b/day16/Makefile @@ -17,7 +17,7 @@ compile: ex1-c ex2-c ex1: @$(TIME) ex1.bash < $(INPUT) 2>&1 - @#$(TIME) ex1-c 2020 < $(INPUT) 2>&1 + @$(TIME) ex1-c 2020 < $(INPUT) 2>&1 ex2: @$(TIME) ex2.bash < $(INPUT) 2>&1 diff --git a/day16/OUTPUT b/day16/OUTPUT new file mode 100644 index 0000000..b78c96d --- /dev/null +++ b/day16/OUTPUT @@ -0,0 +1,12 @@ +ex1.bash : res=21996 + time: 0:00.19 real, 0.15 user, 0.04 sys + context-switch: 26+98, page-faults: 0+9522 + +ex1-c : res=21996 + time: 0:00.00 real, 0.00 user, 0.00 sys + context-switch: 0+1, page-faults: 0+75 + +ex2.bash : res=650080463519 + time: 0:06.63 real, 6.59 user, 0.04 sys + context-switch: 666+92, page-faults: 0+10157 + diff --git a/day16/ex1-c.c b/day16/ex1-c.c new file mode 100644 index 0000000..74d93dc --- /dev/null +++ b/day16/ex1-c.c @@ -0,0 +1,165 @@ +/* ex1-c: Advent2020 game, day 16/game 1 + */ + +#include +#include +#include + +#define AA "ABCD" +#define LAA sizeof(AA) + +/* tickets ranges */ +struct srange { + char *name; + int s1, e1; + int s2, e2; +}; + +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; + struct srange *p=list.ranges; + + fprintf(stderr, "LIST: address=%p last=%u size=%u\n", list.ranges, n, list.size); + for (i=0; is1, p->e1, p->s2, p->e2, p->name); +} + +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; + list.last++; + return p; +} + +void print_ticket(ticket) + int *ticket; +{ + int i; + for (i=0; i=range->s1 && i<=range->e1) || (i>=range->s2 && i<=range->e2)) + return 1; + else + return 0; +} + +int ex1(ticket) + int *ticket; +{ + int i, r, res=0, found; + + for (i=0; i= '0' && *line <= '9') { + switch (status) { + case 1: + ticket=parse_myticket(line); + break; + case 2: + ticket=parse_ticket(line); + res += ex1(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); + + } + } + printf("%s : res=%d\n", *av, res); + exit (0); +}