day 3: less spaghet

This commit is contained in:
xenia 2022-12-03 00:38:41 -05:00
parent 6529982611
commit 1e0e9c9e11
1 changed files with 14 additions and 20 deletions

34
3.rkt
View File

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