advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

4.c (933B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <md5.h>

int main(void) {
    uint8_t input[] = {'b', 'g', 'v', 'y', 'z', 'd', 's', 'v', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    const size_t input_len = 8;
    const size_t input_len_m = 18;

    MD5_CTX *m = malloc(sizeof(MD5_CTX));
    if (m == NULL) {
        perror("malloc");
        return 1;
    }

    char *digest = NULL;

    long j = 1;

    while (1) {
        printf("%ld\r", j);
        int used = snprintf((char *)input + input_len, input_len_m - input_len, "%ld", j);

        MD5Init(m);
        MD5Update(m, input, input_len+used);
        digest = MD5End(m, digest);

        for (int i = 0; i < 6; i++) {
            if (digest[i] != '0')
                break;
            if (i == 5)
                goto end;
        }
        j++;
    }
end:
    printf("%ld\n", j);
    printf("%s\n", digest);
    free(digest);
    free(m);

    return 0;
}