day 3: genious solution

This commit is contained in:
xenia 2022-12-03 00:54:12 -05:00
parent 1e0e9c9e11
commit 342b778f2c
1 changed files with 5 additions and 12 deletions

17
3.rkt
View File

@ -4,24 +4,17 @@
;; solution for day 3 ;; solution for day 3
(define priorities (define (char->prio chr)
(string->list "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")) (- (char->integer chr) (if (char-upper-case? chr) 38 96)))
(define (part1 input) (define (part1 input)
(for/sum ([line (in-list input)]) (for/sum ([line (in-list input)])
(define-values [left right] (split-at line (/ (length line) 2))) (define-values [left right] (split-at line (/ (length line) 2)))
(~>> (set-intersect left right) (~> (set-intersect left right) (set-first) (char->prio))))
(set-first)
(index-of priorities))))
(define (part2 input) (define (part2 input)
(match input (for/sum ([group (in-slice 3 input)])
['() 0] (~> (apply set-intersect group) (set-first) (char->prio))))
[(list* a b c rst)
(+ (~>> (set-intersect a b c)
(set-first)
(index-of priorities))
(part2 rst))]))
(define (parse fname) (define (parse fname)
(map string->list (file->lines fname))) (map string->list (file->lines fname)))