advent2023

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

commit de89cfa39333a782338aff416883dbbd827c4209
parent cd360b8395d441a8a5cff9f379ff63bc14bb5f31
Author: bsandro <email@bsandro.tech>
Date:   Sat, 16 Dec 2023 18:39:49 +0200

day 16 p2 or "I really needed some sort of hashmaps here"

Diffstat:
Mday16/Makefile | 2+-
Mday16/puzzle.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
2 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/day16/Makefile b/day16/Makefile @@ -2,7 +2,7 @@ NAME=$(shell basename ${PWD}) SRC=$(wildcard *.c) DEPS:=$(wildcard *.h) OBJ:=$(SRC:.c=.o) -CFLAGS=-O0 -g -std=c11 -Werror -Wall -Wextra -I. -I../common +CFLAGS=-O2 -std=c11 -Werror -Wall -Wextra -I. -I../common LDFLAGS=-lc all: $(NAME) diff --git a/day16/puzzle.c b/day16/puzzle.c @@ -63,6 +63,11 @@ bool visited_has(Visited *visited, Pos p) { return false; } +void visited_destroy(Visited *visited) { + //@todo + (void)visited; +} + Pos get_next_empty(Pos cur) { Pos p = cur; switch (cur.dir) { @@ -237,6 +242,32 @@ void traverse(Map *map, Map *energized, Visited *visited, Pos start) { } } +long long count_energized(Map *map) { + long long result = 0; + Map energized = {0}; + energized.size = map->size; + energized.w = map->w; + energized.h = map->h; + energized.area = malloc(energized.size); + memset(energized.area, '.', energized.size); + + Visited visited = {0}; + traverse(map, &energized, &visited, map->start); + + for (int y=0; y<energized.h; ++y) { + for (int x=0; x<energized.w; ++x) { + if (energized.area[y*energized.w+x]=='#') { + result++; + } + } + } + + free(energized.area); + visited_destroy(&visited); + + return result; +} + void puzzle(const char *filename, long long *result1, long long *result2) { FILE *infile = fopen(filename, "r"); if (infile == NULL) { @@ -260,33 +291,35 @@ void puzzle(const char *filename, long long *result1, long long *result2) { bzero(buf, STR_LEN); } map.h = line_num; + map.start = (Pos){.x=0,.y=0,.dir=DIR_RIGHT}; + *result1 = count_energized(&map); + *result2 = 0; - Map energized = {0}; - energized.size = map.size; - energized.w = map.w; - energized.h = map.h; - energized.area = malloc(energized.size); - memset(energized.area, '.', energized.size); + for (int x=0; x<map.w; ++x) { + // top row + map.start = (Pos){.x=x,.y=0,.dir=DIR_DOWN}; + long long res = count_energized(&map); + if (res>*result2) *result2 = res; - Visited visited = {0}; + // bottom row + map.start = (Pos){.x=x,.y=map.h-1,.dir=DIR_UP}; + res = count_energized(&map); + if (res>*result2) *result2 = res; + } - traverse(&map, &energized, &visited, map.start); + for (int y=0; y<map.h; ++y) { + // leftmost column + map.start = (Pos){.x=0,.y=y,.dir=DIR_RIGHT}; + long long res = count_energized(&map); + if (res>*result2) *result2 = res; - *result1 = 0; - printf("energized %dx%d:\n", energized.w, energized.h); - for (int y=0; y<energized.h; ++y) { - for (int x=0; x<energized.w; ++x) { - //printf("%c", energized.area[y*energized.w+x]); - if (energized.area[y*energized.w+x]=='#') { - (*result1)++; - } - } - //printf("\n"); + // rightmost column + map.start = (Pos){.x=map.w-1,.y=y,.dir=DIR_RIGHT}; + res = count_energized(&map); + if (res>*result2) *result2 = res; } // mutiny! ignoring feof/ferror. fclose(infile); - (void)result1; - (void)result2; }