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
(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)))