diff --git a/14.rkt b/14.rkt index b791256..bdfee9f 100644 --- a/14.rkt +++ b/14.rkt @@ -5,24 +5,25 @@ ;; solution for day 14 (define (part1 input) - (void)) - ; (define (parse-mask msk) - ; (define orval (for/fold ([val 0]) ([chr (in-string msk)]) - ; (+ (* val 2) (if (equal? chr #\1) 1 0)))) - ; (define andval (for/fold ([val 0]) ([chr (in-string msk)]) - ; (+ (* val 2) (if (equal? chr #\0) 0 1)))) - ; (cons orval andval)) - ; (define orval 0) - ; (define andval (sub1 (expt 2 37))) - ; (define mem (make-hash)) - ; (for ([ins (in-list input)]) - ; (match ins - ; [(cons 'mask (cons ov av)) (set! orval ov) (set! andval av)] - ; [(cons 'set (cons addr val)) - ; (define new-val (bitwise-ior (bitwise-and val andval) orval)) - ; (hash-set! mem addr new-val)])) - ; (for/sum ([(k v) (in-hash mem)]) - ; v)) + (define (parse-mask msk) + (define orval (for/fold ([val 0]) ([chr (in-string msk)]) + (+ (* val 2) (if (equal? chr #\1) 1 0)))) + (define andval (for/fold ([val 0]) ([chr (in-string msk)]) + (+ (* val 2) (if (equal? chr #\0) 0 1)))) + (values orval andval)) + (define orval 0) + (define andval (sub1 (expt 2 37))) + (define mem (make-hash)) + (for ([ins (in-list input)]) + (match ins + [(cons 'mask mask) + (define-values [ov av] (parse-mask mask)) + (set! orval ov) (set! andval av)] + [(cons 'set (cons addr val)) + (define new-val (bitwise-ior (bitwise-and val andval) orval)) + (hash-set! mem addr new-val)])) + (for/sum ([(k v) (in-hash mem)]) + v)) (define (addr->bits addr) (vector-copy @@ -37,7 +38,7 @@ (define mask #f) (for ([ins (in-list input)]) (match ins - [(cons 'mask m) (set! mask m) (displayln mask)] + [(cons 'mask m) (set! mask m)] [(cons 'set (cons addr val)) (define addr-bits (addr->bits addr)) (for ([chr (in-string mask)] [i (in-naturals)])