diff --git a/10.rkt b/10.rkt index 6693d43..a3aec6d 100644 --- a/10.rkt +++ b/10.rkt @@ -12,79 +12,8 @@ (for/fold ([prev 0] [ones 0] [threes 0]) ([x (in-list sorted)]) (values x (+ ones (if (= 1 (- x prev)) 1 0)) (+ threes (if (= 3 (- x prev)) 1 0))))) - ; (displayln ones) - ; (displayln (add1 threes)) (* ones (add1 threes))) -; (part1 '(16 -; 10 -; 15 -; 5 -; 1 -; 11 -; 7 -; 19 -; 6 -; 12 -; 4)) -; (part1 '(28 -; 33 -; 18 -; 42 -; 31 -; 14 -; 46 -; 20 -; 48 -; 47 -; 24 -; 23 -; 49 -; 45 -; 19 -; 38 -; 39 -; 11 -; 1 -; 32 -; 25 -; 35 -; 8 -; 17 -; 7 -; 9 -; 4 -; 2 -; 34 -; 10 -; 3)) - - ; (struct res [val ones threes] #:transparent) - ; (define (step input [prev 0]) - ; ; (printf "~a ~a ~a\n" (first input) (second input) (third input)) - ; (define res3 - ; (if (and (> (length input) 2) (<= (- (third input) 3) prev)) - ; (step (cons (first input) (rest (rest (rest input)))) (third input)) - ; #f)) - ; (define res2 - ; (if (and (> (length input) 1) (<= (- (second input) 3) prev)) - ; (step (rest (rest input)) (second input)) - ; #f)) - ; (define res1 - ; (if (and (> (length input) 0) (<= (- (first input) 3) prev)) - ; (step (rest input) (first input)) - ; #f)) - ; (define n1 (and res1 (res-val res1))) - ; (define n2 (and res2 (res-val res2))) - ; (define n3 (and res3 (res-val res3))) - ; (cond - ; [(and res1 (and res2 (>= n1 n2)) (and res3 (>= n1 n3))) (res (add1 n1) (add1 (res-ones res1)) (res-threes res1))] - ; [(and res2 (and res1 (>= n2 n1)) (and res3 (>= n2 n3))) (res (add1 n1) (res-ones res1) (res-threes res1))] - ; [(and res3 (and res1 (>= n3 n1)) (and res2 (>= n3 n2))) (res (add1 n1) (res-ones res1) (add1 (res-threes res1)))] - ; [else (res 1 0 0)])) - ; (define res_ (step sorted)) - ; (* (res-ones res_) (res-threes res_))) - (define (part2 input) (define is (cons 0 (sort input <))) (define-values [edges _] @@ -107,34 +36,18 @@ (define wanted (last is)) - (define memo (make-hash)) - (define (count-paths G node) - (cond [(hash-has-key? memo node) (hash-ref memo node)] - [(= node wanted) 1] - [else - (define ans (for/sum ([x (in-neighbors G node)]) - (count-paths G x))) - (hash-set! memo node ans) - ans])) + (define/memoized (count-paths node) + (cond [(= node wanted) 1] + [else (for/sum ([x (in-neighbors G node)]) + (count-paths x))])) - (count-paths G 0)) - -; (part2 '(16 -; 10 -; 15 -; 5 -; 1 -; 11 -; 7 -; 19 -; 6 -; 12 -; 4)) + (count-paths 0)) (module+ test (require rackunit) - ;; tests here - (displayln "no tests :(")) + (check-equal? + 8 (part2 + '(16 10 15 5 1 11 7 19 6 12 4)))) (module+ main (define input (map string->number (file->lines "inputs/10"))) diff --git a/scripts/aoc.rkt b/scripts/aoc.rkt index acacd3b..ea0ab7d 100644 --- a/scripts/aoc.rkt +++ b/scripts/aoc.rkt @@ -3,7 +3,8 @@ ;; utilities for every challenge script (require "aoc-lib.rkt" threading syntax/parse/define (for-syntax racket/syntax) graph) -(provide answer dbg (all-from-out threading syntax/parse/define graph) +(provide answer dbg memoize define/memoized + (all-from-out threading syntax/parse/define graph) (for-syntax (all-from-out racket/syntax))) ;; in-expression debug print, uwu @@ -11,6 +12,15 @@ (pretty-write x) x) +;; makes a memoization wrapper around a function +(define (memoize func) + (define memo (make-hash)) + (lambda (arg) + (hash-ref! memo arg (λ () (func arg))))) + +(define-simple-macro (define/memoized (name:id arg:id) body:expr ...+) + (define name (memoize (λ (arg) body ...)))) + ;; submit a solution to the server if not already submitted (define (answer day part answer) (printf "answer ~a.~a: ~s\n" day part answer)