commit f18e52045ff61ae209f1752859199ceb4648743f
parent 1d5eca68ec86d199104f69fdce05efe876bceb28
Author: bsandro <email@bsandro.tech>
Date: Wed, 10 Jan 2024 22:12:20 +0200
merge day10 -> master
Diffstat:
2 files changed, 80 insertions(+), 13 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
@@ -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,
@@ -147,31 +180,66 @@ int traverse(Map *map, Pos start, Map *loop) {
}
/*
-
-| -> |,F
+| -> |,F,L
L -> J
F -> 7
+7 -> |,F,L
+ */
+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=='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;
+}
// | 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};
+ bool is_open = false;
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;
- }
- } else {
- if (sym=='.') {
+ char prev = stack_last(&walls);
+
+ if (sym=='.') {
+ if (is_open) {
loop->area[loop->w*y+x] = 'I';
area++;
- } else if (sym=='|'||sym=='J') {
- open_sym = 0;
- } //else if () {}
+ }
+ continue;
+ }
+
+ 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);
+ }
}
}
}