2020-12-10 05:59:59 +00:00
|
|
|
#lang curly-fn racket
|
|
|
|
|
|
|
|
(require "scripts/aoc.rkt")
|
|
|
|
|
|
|
|
;; solution for day 10
|
|
|
|
|
|
|
|
;; helper functions here
|
|
|
|
|
|
|
|
(define (part1 input)
|
|
|
|
(define sorted (sort input <))
|
2020-12-10 06:24:50 +00:00
|
|
|
(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)))
|
2020-12-10 05:59:59 +00:00
|
|
|
|
|
|
|
(define (part2 input)
|
2020-12-10 06:24:50 +00:00
|
|
|
;; sort
|
|
|
|
;; also add 0
|
2020-12-10 05:59:59 +00:00
|
|
|
(define is (cons 0 (sort input <)))
|
2020-12-10 06:24:50 +00:00
|
|
|
;; make graph
|
2020-12-10 05:59:59 +00:00
|
|
|
(define-values [edges _]
|
|
|
|
(for/fold ([edges '()] [lst (rest is)]) ([x (in-list is)])
|
|
|
|
(define res3
|
|
|
|
(if (and (> (length lst) 2) (<= (- (third lst) 3) x))
|
|
|
|
(list (list x (third lst)))
|
|
|
|
'()))
|
|
|
|
(define res2
|
|
|
|
(if (and (> (length lst) 1) (<= (- (second lst) 3) x))
|
|
|
|
(list (list x (second lst)))
|
|
|
|
'()))
|
|
|
|
(define res1
|
|
|
|
(if (and (> (length lst) 0) (<= (- (first lst) 3) x))
|
|
|
|
(list (list x (first lst)))
|
|
|
|
'()))
|
|
|
|
(values (append edges res1 res2 res3) (if (empty? lst) '() (rest lst)))))
|
|
|
|
|
2020-12-10 06:24:50 +00:00
|
|
|
;; yay graph
|
2020-12-10 05:59:59 +00:00
|
|
|
(define G (directed-graph edges))
|
|
|
|
|
2020-12-10 06:24:50 +00:00
|
|
|
;; the node we want
|
2020-12-10 05:59:59 +00:00
|
|
|
(define wanted (last is))
|
|
|
|
|
2020-12-10 06:24:50 +00:00
|
|
|
;; count the total number of paths that end in wanted
|
2020-12-10 06:19:00 +00:00
|
|
|
(define/memoized (count-paths node)
|
2020-12-10 06:24:50 +00:00
|
|
|
(cond [(= node wanted) 1] ;; there's one path to the wanted node from wherever we went
|
|
|
|
;; add up all possible paths recursively
|
2020-12-10 06:19:00 +00:00
|
|
|
[else (for/sum ([x (in-neighbors G node)])
|
|
|
|
(count-paths x))]))
|
2020-12-10 05:59:59 +00:00
|
|
|
|
2020-12-10 06:24:50 +00:00
|
|
|
;; yeet
|
2020-12-10 06:19:00 +00:00
|
|
|
(count-paths 0))
|
2020-12-10 05:59:59 +00:00
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(require rackunit)
|
2020-12-10 06:19:00 +00:00
|
|
|
(check-equal?
|
|
|
|
8 (part2
|
|
|
|
'(16 10 15 5 1 11 7 19 6 12 4))))
|
2020-12-10 05:59:59 +00:00
|
|
|
|
|
|
|
(module+ main
|
|
|
|
(define input (map string->number (file->lines "inputs/10")))
|
|
|
|
;; part 1
|
|
|
|
(answer 10 1 (part1 input))
|
|
|
|
|
|
|
|
;; part 2
|
|
|
|
(answer 10 2 (part2 input))
|
|
|
|
|
|
|
|
(displayln "meow"))
|