2016
Matthew Butterick 8 years ago
parent 8d3cf51a11
commit 87d3d263d7

@ -0,0 +1,2 @@
#lang aoc-racket/2016/day05
ojvtpuvg

@ -1,15 +1,25 @@
#lang br
#lang br/quicklang
(require openssl/md5)
(define key "ojvtpuvg")
(define (read-syntax path port)
(strip-bindings
#`(module mod "day05.rkt"
(solve-part-a #,(string-trim (port->string port))))))
(module+ reader (provide read-syntax))
(let loop ([idx 0][password-cs empty])
(cond
[(= 8 (length password-cs))
(apply string-append (reverse password-cs))]
[else
(define this-key (format "~a~a" key idx))
(define next-hash (md5 (open-input-string this-key)))
(if (string-prefix? next-hash "00000")
(loop (add1 idx) (cons (substring next-hash 5 6) password-cs))
(loop (add1 idx) password-cs))]))
(provide #%module-begin)
(define (solve-part-a key)
(display "part a solution: ")
(for*/fold ([password-cs empty])
([idx (in-naturals)]
[this-key (in-value (format "~a~a" key idx))]
#:break (= 8 (length password-cs)))
(define this-hash (md5 (open-input-string this-key)))
(if (string-prefix? this-hash "00000")
(let ([next-str (substring this-hash 5 6)])
(display next-str)
(cons next-str password-cs))
password-cs))
(displayln ""))
(provide solve-part-a)