about summary refs log tree commit diff
path: root/day03.rkt
blob: 5277ab56fa31515d3b6046dbd9713bef252c29f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#lang racket
(require srfi/1)

(define (priority c)
  (cond [(char<=? #\a c #\z) (- (char->integer c) (char->integer #\a) -1)]
        [(char<=? #\A c #\Z) (- (char->integer c) (char->integer #\A) -1 -26)]))

(for/sum ([line (file->lines "day03")])
  (let* ([len (string-length line)]
         [mid (/ len 2)]
         [p1 (substring line 0 mid)]
         [p2 (substring line mid)]
         [dup (car (lset-intersection eq?
                                      (string->list p1)
                                      (string->list p2)))])
    (priority dup)))
;; 8243

(let loop ([lines (file->lines "day03")]
           [sum 0])
  (if (null? lines)
    sum
    (let*-values ([(group rest) (split-at lines 3)]
                  [(badge) (car (apply lset-intersection eq?
                                       (map string->list group)))])
      (loop rest (+ sum (priority badge))))))
;; 2631