advent2023

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

commit 1d5eca68ec86d199104f69fdce05efe876bceb28
parent bffedd11e743aeb02f16d4b22a24a106adcadf81
Author: bsandro <email@bsandro.tech>
Date:   Sat, 30 Dec 2023 13:28:43 +0200

Returned to AoC to bash my head against d10p2 again, no success yet :<

Diffstat:
Mday10/Makefile | 9+++++++++
Mday10/puzzle.c | 178+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
Aday10/test3.txt | 9+++++++++
Aday10/test4.txt | 10++++++++++
Aday10/test5.txt | 10++++++++++
5 files changed, 195 insertions(+), 21 deletions(-)

diff --git a/day10/Makefile b/day10/Makefile @@ -26,3 +26,12 @@ test: $(NAME) test2: $(NAME) @./$(NAME) test2.txt + +test3: $(NAME) + @./$(NAME) test3.txt + +test4: $(NAME) + @./$(NAME) test4.txt + +test5: $(NAME) + @./$(NAME) test5.txt diff --git a/day10/puzzle.c b/day10/puzzle.c @@ -31,6 +31,15 @@ typedef struct Map { size_t size; // essentially width*height*sizeof(char) } Map; +void map_print(Map *map) { + for (int y=0; y<map->h; ++y) { + for (int x=0; x<map->w; ++x) { + printf("%c", map->area[map->w*y+x]); + } + printf("\n"); + } +} + bool is_valid(Map *map, Pos p) { return p.dir!=DIR_INVALID && p.x>=0 && p.x<map->w && p.y>=0 && p.y<map->h; } @@ -111,25 +120,102 @@ Pos get_next(Map *map, Pos from) { //printf("found start again\n"); return invalid; default: - printf("invalid symbol %c\n", sym); + printf("invalid symbol %c at %d:%d\n", sym, from.x, from.y); return invalid; } return next; } -int traverse(Map *map, Pos start) { +int traverse(Map *map, Pos start, Map *loop) { int len = 1; Pos p = start; while (is_valid(map, p)) { - //printf("get_next %c %d:%d\n", map->area[p.y*map->w+p.x], p.x, p.y); + int offset = p.y*map->w+p.x; + loop->area[offset] = map->area[offset]; + //printf("get_next %c %d:%d\n", map->area[offset], p.x, p.y); p = get_next(map, p); + offset = p.y*map->w+p.x; len++; - if (map->area[p.y*map->w+p.x]=='S') return len; + if (map->area[offset]=='S') { + // we need to transform S into real pipe type + loop->area[offset] = 'S'; + return len; + } } return -1; } +/* + +| -> |,F +L -> J +F -> 7 + + */ + +// | 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; + 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=='.') { + loop->area[loop->w*y+x] = 'I'; + area++; + } else if (sym=='|'||sym=='J') { + open_sym = 0; + } //else if () {} + } + } + } + return area; +} + +char dir2sym(Dir dir) { + if (dir==DIR_NORTH) return 'n'; + if (dir==DIR_SOUTH) return 's'; + if (dir==DIR_WEST) return 'w'; + if (dir==DIR_EAST) return 'e'; + + return 'z'; +} + +char dirs2sym(Dir dir1, Dir dir2) { + if (dir1==DIR_NORTH&&dir2==DIR_SOUTH) return '|'; + if (dir2==DIR_NORTH&&dir1==DIR_SOUTH) return '|'; + + if (dir1==DIR_NORTH&&dir2==DIR_WEST) return 'F'; + if (dir2==DIR_NORTH&&dir1==DIR_WEST) return 'F'; + + if (dir1==DIR_NORTH&&dir2==DIR_EAST) return '7'; + if (dir2==DIR_NORTH&&dir1==DIR_EAST) return '7'; + + if (dir1==DIR_SOUTH&&dir2==DIR_WEST) return 'L'; + if (dir2==DIR_SOUTH&&dir1==DIR_WEST) return 'L'; + + if (dir1==DIR_SOUTH&&dir2==DIR_EAST) return 'J'; + if (dir2==DIR_SOUTH&&dir1==DIR_EAST) return 'J'; + + if (dir1==DIR_WEST&&dir2==DIR_EAST) return '-'; + if (dir2==DIR_WEST&&dir1==DIR_EAST) return '-'; + + printf("invalid dirs %c and %c\n", dir2sym(dir1), dir2sym(dir2)); + return 'E'; +} + +void save_dir(Dir *save1, Dir *save2, Dir dir) { + printf("save_dir() s1:%c s2:%c save:%c\n", dir2sym(*save1), dir2sym(*save2), dir2sym(dir)); + Dir *save = ((*save1)==DIR_INVALID)?save1:save2; + *save = dir; +} + void puzzle(const char *filename, long long *result1, long long *result2) { FILE *infile = fopen(filename, "r"); if (infile == NULL) { @@ -166,31 +252,81 @@ void puzzle(const char *filename, long long *result1, long long *result2) { map.h = line_num; //printf("map %dx%d (%zu)\nstart: x:%d,y:%d\n", map.w, map.h, map.size, start.x, start.y); + Map loop = {0}; + loop.w = map.w; + loop.h = map.h; + loop.size = map.size; + loop.area = malloc(loop.size); + memset(loop.area, '.', loop.size); + Dir start_dir1 = DIR_INVALID, start_dir2 = DIR_INVALID; + int valids = 0; Pos init = start; + + printf("traverse west...\n"); init.x--; init.dir = DIR_WEST; - int r = traverse(&map, init); - if (r!=-1) *result1 = r/2; + int r = traverse(&map, init, &loop); + if (r!=-1) { + *result1 = r/2; + save_dir(&start_dir1, &start_dir2, DIR_EAST); + valids++; + printf("-> west %d\n", valids); + } - init.x++; - init.dir = DIR_EAST; - r = traverse(&map, init); - if (r!=-1) *result1 = r/2; + if (valids<2) { + memset(loop.area, '.', loop.size); + printf("traverse east...\n"); + init = start; + init.x++; + init.dir = DIR_EAST; + r = traverse(&map, init, &loop); + if (r!=-1) { + *result1 = r/2; + save_dir(&start_dir1, &start_dir2, DIR_WEST); + valids++; + printf("-> east %d\n", valids); + } + } - init.y++; - init.dir = DIR_SOUTH; - r = traverse(&map, init); - if (r!=-1) *result1 = r/2; + if (valids<2) { + memset(loop.area, '.', loop.size); + printf("traverse south...\n"); + init = start; + init.y++; + init.dir = DIR_SOUTH; + r = traverse(&map, init, &loop); + if (r!=-1) { + *result1 = r/2; + save_dir(&start_dir1, &start_dir2, DIR_NORTH); + valids++; + printf("-> south %d\n", valids); + } + } - init.y--; - init.dir = DIR_NORTH; - r = traverse(&map, init); - if (r!=-1) *result1 = r/2; + if (valids<2) { + memset(loop.area, '.', loop.size); + printf("traverse north...\n"); + init = start; + init.y--; + init.dir = DIR_NORTH; + r = traverse(&map, init, &loop); + if (r!=-1) { + *result1 = r/2; + save_dir(&start_dir1, &start_dir2, DIR_SOUTH); + valids++; + printf("-> north %d\n", valids); + } + } + + printf("(%d) start_dir1: %c, start_dir2: %c, sym: %c\n", valids, dir2sym(start_dir1), dir2sym(start_dir2), dirs2sym(start_dir1, start_dir2)); + + loop.area[loop.w*start.y+start.x] = dirs2sym(start_dir1, start_dir2); + + *result2 = calc_area(&loop); + // 3670 is too high + map_print(&loop); // mutiny! ignoring feof/ferror. fclose(infile); - - (void)result1; - (void)result2; } diff --git a/day10/test3.txt b/day10/test3.txt @@ -0,0 +1,9 @@ +.......... +.S------7. +.|F----7|. +.||....||. +.||....||. +.|L-7F-J|. +.|..||..|. +.L--JL--J. +.......... diff --git a/day10/test4.txt b/day10/test4.txt @@ -0,0 +1,10 @@ +.F----7F7F7F7F-7.... +.|F--7||||||||FJ.... +.||.FJ||||||||L7.... +FJL7L7LJLJ||LJ.L-7.. +L--J.L7...LJS7F-7L7. +....F-J..F7FJ|L7L7L7 +....L7.F7||L7|.L7L7| +.....|FJLJ|FJ|F7|.LJ +....FJL-7.||.||||... +....L---J.LJ.LJLJ... diff --git a/day10/test5.txt b/day10/test5.txt @@ -0,0 +1,10 @@ +FF7FSF7F7F7F7F7F---7 +L|LJ||||||||||||F--J +FL-7LJLJ||||||LJL-77 +F--JF--7||LJLJ7F7FJ- +L---JF-JLJ.||-FJLJJ7 +|F|F-JF---7F7-L7L|7| +|FFJF7L7F-JF7|JL---7 +7-L-JL7||F7|L7F-7F7| +L.L7LFJ|||||FJL7||LJ +L7JLJL-JLJLJL--JLJ.L