advent-of-code

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

commit 48d2de6074473bc08bb44a1e16b5c1075c1bf7c7
parent eeaa0335d156f878e123eff64c983e835d254a2d
Author: aabacchus <ben@bvnf.space>
Date:   Fri,  4 Feb 2022 03:49:17 +0000

15.4

Diffstat:
A2015/04/4.c | 44++++++++++++++++++++++++++++++++++++++++++++
A2015/04/4.sh | 17+++++++++++++++++
2 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/2015/04/4.c b/2015/04/4.c @@ -0,0 +1,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; +} diff --git a/2015/04/4.sh b/2015/04/4.sh @@ -0,0 +1,17 @@ +#!/bin/sh -e + +input="bgvyzdsv" + +i=1038000 + +while :; do + printf "\r%d" "$i" + case "$(printf "%s%d" "$input" "$i" | md5sum)" in + 00000*) + echo; + break; + ;; + esac + i=$((i+1)) +done +wait