From 6706d5a87ef5b90febbf98e05bd8ef80c0d90442 Mon Sep 17 00:00:00 2001 From: laura Date: Thu, 11 Dec 2025 21:45:36 +0100 Subject: [PATCH] solved day 4 in reasonable runtime --- 4/Makefile | 13 +++++++ 4/main.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 4/Makefile create mode 100644 4/main.c diff --git a/4/Makefile b/4/Makefile new file mode 100644 index 0000000..ba44bd3 --- /dev/null +++ b/4/Makefile @@ -0,0 +1,13 @@ +client: main.c + gcc -o main main.c -O3 -Wall -Wextra -g -march=x86-64-v3 + +client-debug: main.c + gcc -o main main.c -O1 -Wall -Wextra -g -fsanitize=address + +client-debug-nosanitize: main.c + gcc -o main main.c -O1 -Wall -Wextra -g + +clean: + rm main + +.PHONY: clean \ No newline at end of file diff --git a/4/main.c b/4/main.c new file mode 100644 index 0000000..4af02cc --- /dev/null +++ b/4/main.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +char input[] = +{ +#embed "input.txt" +,'\0' // null terminator +,'\0' +}; + +int width = 0; +int height = 0; + +uint64_t power(uint64_t base, uint64_t exp) { + uint64_t i, result = 1; + for (i = 0; i < exp; i++) + result *= base; + return result; +} + + +void print_field(uint8_t* surrounds){ + for(int x = 0; x < height; x++){ + for(int y = 0; y < width; y++){ + if(surrounds[(x)*(width+2)+(y)] < 4 && input[(x)*(width+1)+(y)] == '@'){ + printf("%d", surrounds[(x)*(width+2)+(y)]); + }else{ + printf("%c", input[(x)*(width+1)+(y)]); + } + } + printf("\n"); + } +} + +int main(int argc, char *argv[]){ + clock_t start, end; + start = clock(); + + while(input[width] != '\n'){ + width++; + } + //printf("width: %d\n", width); + + while(input[height*(width+1)+1] != 0){ + height++; + } + //printf("height: %d\n", height); + + + uint8_t* surrounds = calloc((height+2)*(width+2), sizeof(uint8_t))+width+1; + + for(int x = 0; x < height; x++){ + for(int y = 0; y< width; y++){ + if(input[(x)*(width+1)+(y)] == '@'){ + surrounds[(x-1)*(width+2)+(y-1)] += 1; + surrounds[(x-1)*(width+2)+(y)] += 1; + surrounds[(x-1)*(width+2)+(y+1)] += 1; + surrounds[(x)*(width+2)+(y-1)] += 1; + surrounds[(x)*(width+2)+(y)] += 0; + surrounds[(x)*(width+2)+(y+1)] += 1; + surrounds[(x+1)*(width+2)+(y-1)] += 1; + surrounds[(x+1)*(width+2)+(y)] += 1; + surrounds[(x+1)*(width+2)+(y+1)] += 1; + } + //if(surrounds[(x)*width+(y)] < 8 && input[(x)*(width+1)+(y)] == '@'){ + // printf("%d", surrounds[(x)*(width+2)+(y)]); + //}else{ + // printf("%c", input[(x)*(width+1)+(y)]); + //} + } + //printf("\n"); + } + + uint64_t count = 0; + for(int x = 0; x < height; x++){ + for(int y = 0; y < width; y++){ + if(surrounds[(x)*(width+2)+(y)] < 4 && input[(x)*(width+1)+(y)] == '@'){ + count++; + //printf("%d", surrounds[(x)*width+(y)]); + }else{ + //printf("%c", input[(x)*width+(y)]); + } + } + //printf("\n"); + } + //print_field(surrounds); + + free(surrounds-width-1); + + end = clock(); + printf("count: %lu\n", count); + clock_t ticks_taken = end - start; + double time_taken = ((double)ticks_taken)/CLOCKS_PER_SEC; + printf("time take: %f ticks\n", time_taken); + printf("CLOCKS_PER_SEC: %ld\n", CLOCKS_PER_SEC); + + + exit(1); +} \ No newline at end of file