#include "lib.h" #define DBG 0 static unsigned char input[] = { #embed "day4_input.txt" }; static unsigned char test[] = { #embed "day4_test.txt" }; static unsigned char buf[UINT16_MAX]; static unsigned long do_part1(size_t file_len, unsigned char file[file_len]) { unsigned char *s = file, *d = buf; unsigned long result = 0; #if DBG print("\n"); #endif unsigned file_stride; while (*s != '\n') s++; file_stride = s - file + 1; s = file; unsigned line_count = file_len / file_stride; unsigned line_stride = file_stride + 1; memset(d, '0', sizeof(buf)); #if DBG printh(file_stride); print("\n"); printh(line_stride * (line_count + 2)); print("\n"); #endif #if DBG printns(line_stride, d); print("\n"); #endif d += line_stride; do { memcpy(d + 1, s, file_stride - 1); #if DBG printns(line_stride, d); print("\n"); #endif d += line_stride; s += file_stride; } while (s != &file[file_len]); #if DBG printns(line_stride, d); print("\n"); #endif unsigned char *l = buf + line_stride + 1; for (unsigned i = line_count; i; i--) { d = l; for (unsigned j = line_stride - 2; j; j--) { if (*d == '@') { unsigned neighbors = 0 + (*(d - 1 - line_stride) == '@') + (*(d - 0 - line_stride) == '@') + (*(d + 1 - line_stride) == '@') + (*(d - 1 - 0) == '@') + (*(d + 1 - 0) == '@') + (*(d - 1 + line_stride) == '@') + (*(d - 0 + line_stride) == '@') + (*(d + 1 + line_stride) == '@'); if (neighbors < 4) { result++; } } d++; } l += line_stride; } return result; } static unsigned long do_part2(size_t file_len, unsigned char file[file_len]) { unsigned char *s = file, *d = buf; unsigned long result = 0; #if DBG print("\n"); #endif unsigned file_stride; while (*s != '\n') s++; file_stride = s - file + 1; s = file; unsigned line_count = file_len / file_stride; unsigned line_stride = file_stride + 1; memset(d, '0', sizeof(buf)); #if DBG printh(file_stride); print("\n"); printh(line_stride * (line_count + 2)); print("\n"); #endif #if DBG printns(line_stride, d); print("\n"); #endif d += line_stride; do { memcpy(d + 1, s, file_stride - 1); #if DBG printns(line_stride, d); print("\n"); #endif d += line_stride; s += file_stride; } while (s != &file[file_len]); #if DBG printns(line_stride, d); print("\n"); #endif unsigned char *l = buf + line_stride + 1; for (unsigned i = line_count; i; i--) { line_start: d = l; for (unsigned j = line_stride - 2; j; j--) { if (*d == '@') { unsigned neighbors = 0 + (*(d - 1 - line_stride) == '@') + (*(d - 0 - line_stride) == '@') + (*(d + 1 - line_stride) == '@') + (*(d - 1 - 0) == '@') + (*(d + 1 - 0) == '@') + (*(d - 1 + line_stride) == '@') + (*(d - 0 + line_stride) == '@') + (*(d + 1 + line_stride) == '@'); if (neighbors < 4) { *d = '.'; result++; if (i > line_count-1) { i = line_count; l = buf + line_stride + 1; goto line_start; } else { i += 1; l -= line_stride * 1; goto line_start; } } } d++; } l += line_stride; } return result; } #define RUN_TEST1 1 #define RUN_PART1 1 #define RUN_TEST2 1 #define RUN_PART2 1 #define TEST1_EXPECT 13 #define TEST2_EXPECT 43 void run() { #if RUN_TEST1 print("PART 1 TEST: "); if (unsigned long v = do_part1(countof(test), test); v != TEST1_EXPECT) { print("FAILED (got "); printd(v); print(", expected " xstr(TEST1_EXPECT) ")\n"); } else { print("PASSED\n"); } #endif #if RUN_PART1 print("PART 1 RESULT: "); printd(do_part1(countof(input), input)); print("\n"); #endif #if RUN_TEST2 print("PART 2 TEST: "); if (unsigned long v = do_part2(countof(test), test); v != TEST2_EXPECT) { print("FAILED (got "); printd(v); print(", expected " xstr(TEST2_EXPECT) ")\n"); } else { print("PASSED\n"); } #endif #if RUN_PART2 print("PART 2 RESULT: "); printd(do_part2(countof(input), input)); print("\n"); #endif exit_group(0); }