advent2023

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

commit 93764486df1b62d148c0a440fb4ff1aa905e8456
parent 1d5eca68ec86d199104f69fdce05efe876bceb28
Author: bsandro <email@bsandro.tech>
Date:   Wed,  3 Jan 2024 02:01:24 +0200

day 10 p2 paintrain

Diffstat:
Mday10/puzzle.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++----------
1 file changed, 53 insertions(+), 10 deletions(-)

diff --git a/day10/puzzle.c b/day10/puzzle.c @@ -12,6 +12,39 @@ #define STR_LEN 2048 +typedef struct Stack { + char *data; + int len; +} Stack; + +void stack_push(Stack *s, char c) { + assert(s!=NULL); + s->len += sizeof(char); + s->data = realloc(s->data, s->len); + s->data[s->len-1] = c; +} + +char stack_pop(Stack *s) { + assert(s!=NULL); + if (s->len==0) return 0; + s->len--; + char r = s->data[s->len]; + s->data = realloc(s->data, s->len); + return r; +} + +char stack_last(Stack *s) { + assert(s!=NULL); + if (s->len==0) return 0; + return s->data[s->len-1]; +} + +void stack_free(Stack *s) { + assert(s!=NULL); + free(s->data); + s->len = 0; +} + typedef enum Dir { DIR_INVALID, DIR_NORTH, @@ -148,30 +181,40 @@ int traverse(Map *map, Pos start, Map *loop) { /* -| -> |,F +| -> |,F,L L -> J F -> 7 */ +char wall_get_pair(char w) { + if (w=='F') return '7'; + if (w=='L') return 'J'; +} // | L J F 7 S int calc_area(Map *loop) { int area = 0; for (int y=0; y<loop->h; ++y) { - char open_sym = 0; + Stack walls={.data=NULL,.len=0}; for (int x=0; x<loop->w; ++x) { char sym = loop->area[loop->w*y+x]; - if (open_sym==0) { - if (sym=='|'||sym=='F'||sym=='L'||sym=='S') { - open_sym = sym; + char last = stack_last(&walls); + if (sym=='|') { + if (last=='|'||last=='L') + if (last==0) { + stack_push(&walls, sym); + } else { + } - } else { - if (sym=='.') { + } else if (sym=='|'||sym=='F'||sym=='L') { + stack_push(&open, sym); + } else if (sym=='|'||sym=='J'||sym=='7') { + open_sym = 0; + } + if (sym=='.') { loop->area[loop->w*y+x] = 'I'; area++; - } else if (sym=='|'||sym=='J') { - open_sym = 0; - } //else if () {} + } else //else if () {} } } }