commit a224a07f453aba7ecedadc5ed4bef17202ec9702
parent 93764486df1b62d148c0a440fb4ff1aa905e8456
Author: bsandro <brian.drosan@gmail.com>
Date: Fri, 5 Jan 2024 21:56:10 +0200
day 10 p2 had some time to experiment
Diffstat:
2 files changed, 22 insertions(+), 21 deletions(-)
diff --git a/day10/main.c b/day10/main.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <time.h>
#include <string.h>
-#include <threads.h>
void puzzle(const char *filename, long long *res1, long long *res2);
//void puzzle_test(const char *filename, long long *res1, long long *res2);
diff --git a/day10/puzzle.c b/day10/puzzle.c
@@ -184,11 +184,16 @@ int traverse(Map *map, Pos start, Map *loop) {
| -> |,F,L
L -> J
F -> 7
-
+7 -> |,F,L
*/
-char wall_get_pair(char w) {
- if (w=='F') return '7';
- if (w=='L') return 'J';
+bool is_wall_closing(char cur, char prev) {
+ if (cur=='J') return prev=='L';
+ if (cur=='7') return prev=='F';
+ if (cur=='|' /*||
+ cur=='F' ||
+ cur=='L'*/) return prev=='|'||prev=='7';
+
+ return false;
}
// | L J F 7 S
@@ -196,25 +201,22 @@ int calc_area(Map *loop) {
int area = 0;
for (int y=0; y<loop->h; ++y) {
Stack walls={.data=NULL,.len=0};
+ bool is_open = false;
for (int x=0; x<loop->w; ++x) {
char sym = loop->area[loop->w*y+x];
- char last = stack_last(&walls);
- if (sym=='|') {
- if (last=='|'||last=='L')
- if (last==0) {
- stack_push(&walls, sym);
- } else {
-
- }
- } else if (sym=='|'||sym=='F'||sym=='L') {
- stack_push(&open, sym);
- } else if (sym=='|'||sym=='J'||sym=='7') {
- open_sym = 0;
+ 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=='.') {
- loop->area[loop->w*y+x] = 'I';
- area++;
- } else //else if () {}
+ if (sym=='.' && is_open) {
+ loop->area[loop->w*y+x] = 'I';
+ area++;
}
}
}