day 10: shark

This commit is contained in:
xenia 2020-12-10 01:24:50 -05:00
parent 3cb26a8f47
commit a51a25f50e
1 changed files with 15 additions and 6 deletions

21
10.rkt
View File

@ -8,14 +8,18 @@
(define (part1 input)
(define sorted (sort input <))
(define-values [_ ones threes]
(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)))))
(* ones (add1 threes)))
(define deltas (for/list ([a (in-list (rest sorted))] [b (in-list sorted)])
(- a b)))
(define ones (count #{= 1 %} deltas))
(define threes (count #{= 3 %} deltas))
;; idk but add1 to both because reasons(tm)
(* (add1 ones) (add1 threes)))
(define (part2 input)
;; sort
;; also add 0
(define is (cons 0 (sort input <)))
;; make graph
(define-values [edges _]
(for/fold ([edges '()] [lst (rest is)]) ([x (in-list is)])
(define res3
@ -32,15 +36,20 @@
'()))
(values (append edges res1 res2 res3) (if (empty? lst) '() (rest lst)))))
;; yay graph
(define G (directed-graph edges))
;; the node we want
(define wanted (last is))
;; count the total number of paths that end in wanted
(define/memoized (count-paths node)
(cond [(= node wanted) 1]
(cond [(= node wanted) 1] ;; there's one path to the wanted node from wherever we went
;; add up all possible paths recursively
[else (for/sum ([x (in-neighbors G node)])
(count-paths x))]))
;; yeet
(count-paths 0))
(module+ test