day 3: less spaghet
This commit is contained in:
parent
6529982611
commit
1e0e9c9e11
34
3.rkt
34
3.rkt
|
@ -4,33 +4,27 @@
|
|||
|
||||
;; solution for day 3
|
||||
|
||||
(define prios (string->list "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
|
||||
(define priorities
|
||||
(string->list "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
|
||||
|
||||
(define (part1 input)
|
||||
(for/sum ([line (in-list input)])
|
||||
(define len (string-length line))
|
||||
(define h1 (substring line 0 (/ len 2)))
|
||||
(define h2 (substring line (/ len 2) len))
|
||||
(define g1 (list->set (string->list h1)))
|
||||
(define g2 (list->set (string->list h2)))
|
||||
(define both (set-intersect g1 g2))
|
||||
(index-of prios (first (set->list both)))))
|
||||
|
||||
(define-values [left right] (split-at line (/ (length line) 2)))
|
||||
(~>> (set-intersect left right)
|
||||
(set-first)
|
||||
(index-of priorities))))
|
||||
|
||||
(define (part2 input)
|
||||
(displayln (length input))
|
||||
(for/sum ([i (in-range 0 (length input) 3)])
|
||||
(define l1 (list-ref input (+ i 0)))
|
||||
(define l2 (list-ref input (+ i 1)))
|
||||
(define l3 (list-ref input (+ i 2)))
|
||||
(define g1 (list->set (string->list l1)))
|
||||
(define g2 (list->set (string->list l2)))
|
||||
(define g3 (list->set (string->list l3)))
|
||||
(define both (set-intersect (set-intersect g1 g2 ) g3))
|
||||
(index-of prios (first (set->list both)))))
|
||||
(match input
|
||||
['() 0]
|
||||
[(list* a b c rst)
|
||||
(+ (~>> (set-intersect a b c)
|
||||
(set-first)
|
||||
(index-of priorities))
|
||||
(part2 rst))]))
|
||||
|
||||
(define (parse fname)
|
||||
(file->lines fname))
|
||||
(map string->list (file->lines fname)))
|
||||
|
||||
(module+ main
|
||||
(define input (parse "inputs/3"))
|
||||
|
|
Loading…
Reference in New Issue