|
|
@ -4,22 +4,38 @@
|
|
|
|
(define (read-syntax path port)
|
|
|
|
(define (read-syntax path port)
|
|
|
|
(strip-bindings
|
|
|
|
(strip-bindings
|
|
|
|
#`(module mod "day05.rkt"
|
|
|
|
#`(module mod "day05.rkt"
|
|
|
|
(solve-part-a #,(string-trim (port->string port))))))
|
|
|
|
(solve #,(string-trim (port->string port))))))
|
|
|
|
(module+ reader (provide read-syntax))
|
|
|
|
(module+ reader (provide read-syntax))
|
|
|
|
|
|
|
|
|
|
|
|
(provide #%module-begin)
|
|
|
|
(provide #%module-begin)
|
|
|
|
|
|
|
|
|
|
|
|
(define (solve-part-a key)
|
|
|
|
(define (solve key)
|
|
|
|
(display "part a solution: ")
|
|
|
|
(define-values
|
|
|
|
(for*/fold ([password-cs empty])
|
|
|
|
(part-a-solution part-b-solution)
|
|
|
|
([idx (in-naturals)]
|
|
|
|
(for*/fold ([part-a empty]
|
|
|
|
[this-key (in-value (format "~a~a" key idx))]
|
|
|
|
[part-b (make-vector 8 #f)])
|
|
|
|
#:break (= 8 (length password-cs)))
|
|
|
|
([idx (in-naturals 1000000)]
|
|
|
|
(define this-hash (md5 (open-input-string this-key)))
|
|
|
|
[this-key (in-value (format "~a~a" key idx))]
|
|
|
|
(if (string-prefix? this-hash "00000")
|
|
|
|
#:break (and (= 8 (length part-a))
|
|
|
|
(let ([next-str (substring this-hash 5 6)])
|
|
|
|
(andmap string? (vector->list part-b))))
|
|
|
|
(display next-str)
|
|
|
|
(define this-hash (md5 (open-input-string this-key)))
|
|
|
|
(cons next-str password-cs))
|
|
|
|
(define next-part-a
|
|
|
|
password-cs))
|
|
|
|
(if (and (not (= 8 (length part-a)))
|
|
|
|
(displayln ""))
|
|
|
|
(string-prefix? this-hash "00000"))
|
|
|
|
(provide solve-part-a)
|
|
|
|
(let ([next-str (substring this-hash 5 6)])
|
|
|
|
|
|
|
|
(displayln (format "part a progress: ~a" next-str))
|
|
|
|
|
|
|
|
(cons next-str part-a))
|
|
|
|
|
|
|
|
part-a))
|
|
|
|
|
|
|
|
(define next-part-b
|
|
|
|
|
|
|
|
(let ()
|
|
|
|
|
|
|
|
(when (and (string-prefix? this-hash "00000")
|
|
|
|
|
|
|
|
(string->number (substring this-hash 5 6))
|
|
|
|
|
|
|
|
(<= 0 (string->number (substring this-hash 5 6)) 7)
|
|
|
|
|
|
|
|
(not (vector-ref part-b (string->number (substring this-hash 5 6)))))
|
|
|
|
|
|
|
|
(displayln (format "part b progress: idx ~a, hash ~a, ~a at ~a" idx this-hash (substring this-hash 6 7) (substring this-hash 5 6)))
|
|
|
|
|
|
|
|
(vector-set! part-b (string->number (substring this-hash 5 6)) (substring this-hash 6 7)))
|
|
|
|
|
|
|
|
part-b))
|
|
|
|
|
|
|
|
(values next-part-a next-part-b)))
|
|
|
|
|
|
|
|
(displayln (format "part a: ~a" (apply string-append (reverse part-a-solution))))
|
|
|
|
|
|
|
|
(displayln (format "part b: ~a" (apply string-append (vector->list part-b-solution)))))
|
|
|
|
|
|
|
|
(provide solve)
|
|
|
|