From b19aa5edca655b92aad56ce67a85b05732a60ce7 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Thu, 7 Dec 2023 00:27:09 +0400 Subject: [PATCH] A little refactoring for day 4. --- day4/p1.c | 3 ++- day4/p2.c | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/day4/p1.c b/day4/p1.c index d370be7..ba08bd9 100644 --- a/day4/p1.c +++ b/day4/p1.c @@ -3,7 +3,6 @@ #include #include #include -#include uint64_t parse_number(char *end); @@ -43,12 +42,14 @@ int main(int argc, char **argv) { for (char *n_pos = pipe_pos + 4;; n_pos += 3) { uint8_t n = parse_number(n_pos-1); + for (size_t i = 0; i < winning_numbers_len; ++i) { if (n != winning_numbers[i]) continue; points = (points == 0) ? 1 : points * 2; break; } + if (*n_pos == '\n') break; } diff --git a/day4/p2.c b/day4/p2.c index 5a60fec..62764bb 100644 --- a/day4/p2.c +++ b/day4/p2.c @@ -3,7 +3,6 @@ #include #include #include -#include #define STACK_SZ UINT8_MAX+1 @@ -42,24 +41,25 @@ int main(int argc, char **argv) { winning_numbers = (uint8_t *) calloc(winning_numbers_len, sizeof(uint8_t)); } - for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) { + for (size_t i = 0, wl_pos = (size_t)colon_pos+4; i < winning_numbers_len; ++i, wl_pos += 3) winning_numbers[i] = parse_number((char *)(wl_pos-1)); - } uint64_t card_copies = 1 + stack[stack_pos++]; total_copies += card_copies; - size_t old_sp = stack_pos; + stackpointer_t old_sp = stack_pos; for (char *n_pos = pipe_pos + 4;; n_pos += 3) { uint8_t n = parse_number(n_pos-1); + for (size_t i = 0; i < winning_numbers_len; ++i) { if (n != winning_numbers[i]) continue; stack[stack_pos++] += card_copies; break; } + if (*n_pos == '\n') break; }