advent2023

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

commit 431701cd9d7f15d78230f18a6fef25ee7925f66a
parent a1e926bfc9174e904d5aedcf2fe7e32a8c25514a
Author: bsandro <email@bsandro.tech>
Date:   Sun,  3 Dec 2023 13:53:55 +0200

day 03 p1 aka my first 'can't believe I've missed that'

Diffstat:
Mday03/puzzle.c | 20+++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/day03/puzzle.c b/day03/puzzle.c @@ -21,20 +21,17 @@ bool is_digit(char b) { } bool check_part(int x, int y, const char field[FIELD_SIZE][FIELD_SIZE]) { - //printf("check %d:%d:\n", x, y); for (int dy=-1; dy<=1; ++dy) { for (int dx=-1; dx<=1; ++dx) { int dy1=y+dy; int dx1=x+dx; if (dy1>=0 && dy1<FIELD_SIZE && dx1>=0 && dx1<FIELD_SIZE) { if (!is_digit(field[dy1][dx1]) && field[dy1][dx1]!='.') { - printf("%d:%d:checking %d:%d (%c)\n", x, y, dx1, dy1, field[dy1][dx1]); return true; } } } } - //printf("\n-------------------------------\n"); return false; } @@ -46,7 +43,6 @@ void puzzle(const char *filename, long long *result1, long long *result2) { } char field[FIELD_SIZE][FIELD_SIZE] = {0}; - (void)field; char buf[STR_LEN] = {0}; unsigned int line_num = 0; @@ -54,8 +50,7 @@ void puzzle(const char *filename, long long *result1, long long *result2) { *result2 = 0; while (fgets(buf, STR_LEN, infile) != NULL) { - int len = strlen(buf); - for (int i=0; i<len; ++i) { + for (int i=0; i<FIELD_SIZE; ++i) { field[line_num][i] = buf[i]; } ++line_num; @@ -71,12 +66,15 @@ void puzzle(const char *filename, long long *result1, long long *result2) { int digit = 0; bool is_part = false; while (is_digit(field[y][x++])) { - //printf("%c", field[y][x-1]); - if (!is_part) is_part = check_part(x-1, y, field); - digit = digit*10 + field[y][x-1] - '0'; + int cx = x-1; + if (cx>=FIELD_SIZE) break; // too lazy to make it sane + if (!is_part) is_part = check_part(cx, y, field); + digit = digit*10 + field[y][cx] - '0'; + } + if (digit>0 && is_part) { + //printf("%d\n", digit); + *result1 += digit; } - if (digit>0) printf("%d(%d)\n", digit, is_part); - if (digit>0 && is_part) *result1 += digit; } } }