aoc2020/10.rkt

148 lines
3.7 KiB
Racket
Raw Normal View History

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)))))
; (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 _]
(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))
(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]))
(count-paths G 0))
; (part2 '(16
; 10
; 15
; 5
; 1
; 11
; 7
; 19
; 6
; 12
; 4))
(module+ test
(require rackunit)
;; tests here
(displayln "no tests :("))
(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"))