aoc

Thing1's 2025 aoc
Log | Files | Refs

commit 5119833f6a8e73d814353d5314346fcbd8d54299
parent 6f21b3ca67bc035c2cc828736363abb9787b6bfa
Author: thing1 <thing1@seacrossedlovers.xyz>
Date:   Thu,  4 Dec 2025 15:57:41 +0000

day 3 done finally

Diffstat:
M3/3b.c | 71+++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 37 insertions(+), 34 deletions(-)

diff --git a/3/3b.c b/3/3b.c @@ -2,61 +2,64 @@ #include <stdlib.h> #include <string.h> -/* TODO -make it so i have an array of pointers of all the high values, then i sort that array +/* method + +we know we need 12 bats, so to find the largest number, +we can find the largest digit for each, position. To do this +we can find the difference in length of the input and 12, +then from the first n (the difference in length) digits, pick +the highest one. Then repeat the function for the other +digits, using the previously chosen digit as an input and +subtracting 1 from the length */ char * -findhighest(char *s, char **exclude) { - char z = 0; - char *highest = &z; - - for (char *c = s; *c != '\n' && *c != 0; c++) { - for (int i = 0; i <12; i++) - if (exclude[i] == c) goto again; - - if (*c > *highest) - highest = c; -again: +findhighest(char *start, char *end) { + char *highest = start++; + while (start != end) { + if (*start > *highest) + highest = start; + start++; } return highest; } -int -sort(const void *a, const void *b) { - char *s1 = *(char **)a, *s2 = *(char **)b; - return (s1 < s2); -} - char * -highestdigits(char *s) { - static char high[12] = {0}; - char *highest[12] = {0}; +finddigit(char *in, int len) { + int off; + char *digit; - for (int i = 0; i < 12; i++) { - highest[i] = findhighest(s, highest); - } + off = strlen(in) - len; + digit = findhighest(in, in + off + 1); - qsort(highest, 12, sizeof(char *), &sort); + return digit; +} - for (int i = 0; i < 12; i++) - high[i] = *highest[i]; +char ** +finddigits(char *in) { + static char *digits[12]; - return high; -} + for (int len = 12; len >= 0; len--) { + digits[12 - len] = finddigit(in, len); + in = digits[12 - len] + 1; + } + return digits; +} int main() { long long int total = 0; - char *digits, num[13] = {0}, line[256]; + char **digits, num[13] = {0}; + char line[256] = {0}; while (fgets(line, 256, stdin)) { - digits = highestdigits(line); - memcpy(num, digits, 12); + line[strlen(line) - 1] = 0; + digits = finddigits(line); + for (int i = 0; i < 12; i++) + num[i] = *digits[i]; printf("%s\n", num); total += atoll(num); } - printf("%lld\n", total); } \ No newline at end of file