day 3: genious solution
This commit is contained in:
parent
1e0e9c9e11
commit
342b778f2c
17
3.rkt
17
3.rkt
|
@ -4,24 +4,17 @@
|
|||
|
||||
;; solution for day 3
|
||||
|
||||
(define priorities
|
||||
(string->list "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"))
|
||||
(define (char->prio chr)
|
||||
(- (char->integer chr) (if (char-upper-case? chr) 38 96)))
|
||||
|
||||
(define (part1 input)
|
||||
(for/sum ([line (in-list input)])
|
||||
(define-values [left right] (split-at line (/ (length line) 2)))
|
||||
(~>> (set-intersect left right)
|
||||
(set-first)
|
||||
(index-of priorities))))
|
||||
(~> (set-intersect left right) (set-first) (char->prio))))
|
||||
|
||||
(define (part2 input)
|
||||
(match input
|
||||
['() 0]
|
||||
[(list* a b c rst)
|
||||
(+ (~>> (set-intersect a b c)
|
||||
(set-first)
|
||||
(index-of priorities))
|
||||
(part2 rst))]))
|
||||
(for/sum ([group (in-slice 3 input)])
|
||||
(~> (apply set-intersect group) (set-first) (char->prio))))
|
||||
|
||||
(define (parse fname)
|
||||
(map string->list (file->lines fname)))
|
||||
|
|
Loading…
Reference in New Issue