advent2023

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

commit dee410a39093919a0c2ea665f5694daf13f66c05
parent a224a07f453aba7ecedadc5ed4bef17202ec9702
Author: bsandro <email@bsandro.tech>
Date:   Wed, 10 Jan 2024 22:11:18 +0200

day 10 p2 finally solved

Diffstat:
Mday10/puzzle.c | 55+++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 39 insertions(+), 16 deletions(-)

diff --git a/day10/puzzle.c b/day10/puzzle.c @@ -180,19 +180,31 @@ int traverse(Map *map, Pos start, Map *loop) { } /* - | -> |,F,L L -> J F -> 7 7 -> |,F,L */ -bool is_wall_closing(char cur, char prev) { +bool is_wall_toggles(char cur, char prev) { + if (cur=='-') return false; + //if (cur=='|') return prev!=0; + if (cur=='J') return prev=='L'; + if (cur=='L') return prev=='J'; + if (cur=='7') return prev=='F'; - if (cur=='|' /*|| - cur=='F' || - cur=='L'*/) return prev=='|'||prev=='7'; + if (cur=='F') return prev=='7'; + return false; //prev=='|'; +} + +/* +F -> J +L -> 7 + */ +bool is_wall_continues(char cur, char prev) { + if (cur=='J') return prev=='F'; + if (cur=='7') return prev=='L'; return false; } @@ -205,18 +217,29 @@ int calc_area(Map *loop) { for (int x=0; x<loop->w; ++x) { char sym = loop->area[loop->w*y+x]; char prev = stack_last(&walls); - if (is_wall_closing(sym, prev)) { - stack_pop(&walls); - is_open = false; - printf("[%d] wall closed from %c to %c (%d)\n", y, prev, sym, is_open); - } else if (sym!='.'&&sym!='-'&&!is_open) { - stack_push(&walls, sym); - is_open = true; - printf("[%d] wall opened from %c to %c (%d)\n", y, prev, sym, is_open); + + if (sym=='.') { + if (is_open) { + loop->area[loop->w*y+x] = 'I'; + area++; + } + continue; } - if (sym=='.' && is_open) { - loop->area[loop->w*y+x] = 'I'; - area++; + + if (is_wall_continues(sym, prev)) { + //printf("[%2d:%2d] wall continues from %c to %c (%d)\n", y, x, prev, sym, is_open); + //stack_pop(&walls); + //prev = stack_pop(&walls); + stack_push(&walls, sym); + //is_open = !is_open; + } else if (sym!='-') { + is_open = !is_open; + stack_push(&walls, sym); + if (is_open) { + printf("[%2d:%2d] wall opened from %c to %c (%d)\n", y, x, prev, sym, is_open); + } else { + printf("[%2d:%2d] wall closed from %c to %c (%d)\n", y, x, prev, sym, is_open); + } } } }