advent2023

Advent of Code 2023 solutions
git clone git://bsandro.tech/advent2023
Log | Files | Refs | LICENSE

commit 7c444a6524b93c2656826825105cbd7009eaeeb7
parent f94ed0e0c80467ed6afcd4a4011e8f98155ebe8f
Author: bsandro <email@bsandro.tech>
Date:   Mon,  4 Dec 2023 23:41:30 +0200

day 04 p2

Diffstat:
Mday03/Makefile | 2+-
Mday04/Makefile | 2+-
Mday04/puzzle.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
3 files changed, 57 insertions(+), 14 deletions(-)

diff --git a/day03/Makefile b/day03/Makefile @@ -2,7 +2,7 @@ NAME=$(shell basename ${PWD}) SRC=$(wildcard *.c) DEPS:=$(wildcard *.h) OBJ:=$(SRC:.c=.o) -CFLAGS=-O0 -g -std=c11 -Werror -Wall -Wextra -I. -I../common +CFLAGS=-O2 -std=c11 -Werror -Wall -Wextra -I. -I../common LDFLAGS=-lc all: $(NAME) diff --git a/day04/Makefile b/day04/Makefile @@ -2,7 +2,7 @@ NAME=$(shell basename ${PWD}) SRC=$(wildcard *.c) DEPS:=$(wildcard *.h) OBJ:=$(SRC:.c=.o) -CFLAGS=-O0 -g -std=c11 -Werror -Wall -Wextra -I. -I../common +CFLAGS=-O2 -std=c11 -Werror -Wall -Wextra -I. -I../common LDFLAGS=-lc all: $(NAME) diff --git a/day04/puzzle.c b/day04/puzzle.c @@ -22,6 +22,27 @@ typedef enum LinePart { LINE_PART_NUMS } LinePart; +typedef struct Card { + int id; + int count; + int wins; + struct Card *next; +} Card; + +Card * make_card() { + Card *c = malloc(sizeof(Card)); + memset(c, 0, sizeof(Card)); + return c; +} + +void print_cards(Card *first) { + Card *c = first; + while (c!=NULL) { + printf("%d: %d\n", c->id, c->wins); + c = c->next; + } +} + typedef struct Number { int value; struct Number *next; @@ -56,11 +77,15 @@ void puzzle(const char *filename, long long *result1, long long *result2) { *result1 = 0; *result2 = 0; + Card *cards = NULL; + Card *card_last = NULL; + while (fgets(buf, STR_LEN, infile) != NULL) { Number *wins = NULL; Number *wins_last = NULL; Number *nums = NULL; Number *nums_last = NULL; + Card *card = make_card(); int len = strlen(buf); int i = 0; LinePart part = LINE_PART_CARD; @@ -68,21 +93,23 @@ void puzzle(const char *filename, long long *result1, long long *result2) { while (i<len) { if (part==LINE_PART_CARD && buf[i]==':') { part = LINE_PART_WINS; - //printf("going to wins\n"); + card->id = num; + card->count = 1; + if (cards==NULL) cards = card; + if (card_last!=NULL) card_last->next = card; + card_last = card; + num = 0; } else if (part==LINE_PART_WINS && buf[i]=='|') { part = LINE_PART_NUMS; - //printf("going to nums\n"); - } else if (part!=LINE_PART_CARD && is_digit(buf[i])) { + } else if (is_digit(buf[i])) { num = num*10 + buf[i] - '0'; - } else if ((buf[i]==' '||buf[i]==10) && num!=0) { + } else if (part!=LINE_PART_CARD && (buf[i]==' '||buf[i]==10) && num!=0) { if (part==LINE_PART_WINS) { - //printf("win num %d\n", num); Number *n = make_number(num); if (wins==NULL) wins=n; if (wins_last!=NULL) wins_last->next = n; wins_last = n; } else if (part==LINE_PART_NUMS) { - //printf("our num %d\n", num); Number *n = make_number(num); if (nums==NULL) nums=n; if (nums_last!=NULL) nums_last->next = n; @@ -92,28 +119,44 @@ void puzzle(const char *filename, long long *result1, long long *result2) { } ++i; } - printf("wins: "); - print_numbers(wins); - printf("nums: "); - print_numbers(nums); - printf("\n-------------------------\n"); int score = 0; for (Number *n = nums; n!=NULL; n=n->next) { for (Number *w = wins; w!=NULL; w=w->next) { if (n->value==w->value) { + card->wins++; if (score==0) score=1; else score*=2; } } } - printf("score: %d\n", score); *result1 += score; ++line_num; bzero(buf, STR_LEN); } + int cnt = card_last->id; + int copies[cnt]; + memset(&copies, 0, sizeof(int)*cnt); + for (int i=0; i<cnt; ++i) { + copies[i] = 1; + } + + Card *card = cards; + while (card!=NULL) { + int cid = card->id-1; + for (int j=0; j<copies[cid]; ++j) { + for (int i=1; i<=card->wins; ++i) { + copies[cid+i]++; + } + } + card = card->next; + } + for (int i=0; i<cnt; ++i) { + *result2 += copies[i]; + } + // mutiny! ignoring feof/ferror. fclose(infile); }