blob: ea1b389403d5a69beb34e10ace598886595f3241 (
plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#lang racket
(require math/statistics
threading
"../../jj-aoc.rkt")
(define chunks (port->lines (open-day 10 2021)))
(define (opening-bracket? c)
(member c (string->list "([{<")))
(define (matching-brackets? c-left c-right)
(member (string c-left c-right) '("()" "[]" "{}" "<>")))
(define (parse-brackets lst [acc '()])
(cond
[(empty? lst) acc]
[(opening-bracket? (first lst)) (parse-brackets (rest lst) (cons (first lst) acc))]
[(matching-brackets? (first acc) (first lst)) (parse-brackets (rest lst) (rest acc))]
[else (get-score (first lst))]))
;; part 1
(define (get-score c)
(match (string c)
[")" 3]
["]" 57]
["}" 1197]
[">" 25137]))
(define (score-invalid-string chunk)
(match (parse-brackets (string->list chunk))
[(? list?) 0]
[n n]))
(for/sum ([chunk (in-list chunks)]) (score-invalid-string chunk))
;; part 2
(define (completion-score lst)
(for/fold ([score 0]) ([c (in-list lst)])
(define val
(match (string c)
["(" 1]
["[" 2]
["{" 3]
["<" 4]))
(+ (* 5 score) val)))
(define (score-incomplete-string chunk)
(match (parse-brackets (string->list chunk))
[(? list? lst) (completion-score lst)]
[n 0]))
(~>> (for*/list ([chunk (in-list chunks)]
[score (in-value (score-incomplete-string chunk))]
#:when (> score 0))
score)
(median <))
|