From b8a13007034b108158e50bed0853fea7050c19e7 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sun, 1 Dec 2024 00:20:36 -0500 Subject: [PATCH] Optimization and cleanup --- c/2024/1/historian_hysteria.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/c/2024/1/historian_hysteria.c b/c/2024/1/historian_hysteria.c index 039be4f..a80ec5c 100644 --- a/c/2024/1/historian_hysteria.c +++ b/c/2024/1/historian_hysteria.c @@ -4,11 +4,14 @@ #include "../../lib/aoc.h" +// TODO figure this out dynamically +#define ARRAY_SIZE 1000 + int main() { char *line; - int left[1000]; - int right[1000]; + int left[ARRAY_SIZE]; + int right[ARRAY_SIZE]; int index = 0; while ((line = aoc_read_line()) != NULL) { @@ -19,22 +22,25 @@ int main() { index++; } - qsort(left, 1000, sizeof(int), aoc_sort_int); - qsort(right, 1000, sizeof(int), aoc_sort_int); + qsort(left, ARRAY_SIZE, sizeof(int), aoc_sort_int); + qsort(right, ARRAY_SIZE, sizeof(int), aoc_sort_int); int sum = 0; - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < ARRAY_SIZE; i++) { sum += abs(left[i] - right[i]); } printf("Part 1: %d\n", sum); int similarity_score = 0; - for (int i = 0; i < 1000; i++) { + int j = 0; + + for (int i = 0; i < ARRAY_SIZE; i++) { int right_count = 0; - for (int j = 0; j < 1000; j++) { + for (; j < ARRAY_SIZE; j++) { if (left[i] == right[j]) right_count++; + else if (left[i] < right[j]) break; } similarity_score += (left[i] * right_count); }