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 <))
|
|
|
|
(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 (part2 input)
|
|
|
|
(define is (cons 0 (sort input <)))
|
|
|
|
(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)))))
|
|
|
|
|
|
|
|
(define G (directed-graph edges))
|
|
|
|
|
|
|
|
(define wanted (last is))
|
|
|
|
|
2020-12-10 06:19:00 +00:00
|
|
|
(define/memoized (count-paths node)
|
|
|
|
(cond [(= node wanted) 1]
|
|
|
|
[else (for/sum ([x (in-neighbors G node)])
|
|
|
|
(count-paths x))]))
|
2020-12-10 05:59:59 +00:00
|
|
|
|
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"))
|