1
0
Fork 0

Day 10: Squish cases.

This commit is contained in:
Jonathan Chan 2021-12-10 10:32:57 -08:00 committed by Jonathan Chan
parent 8021964610
commit 3cf10af7c1
1 changed files with 3 additions and 4 deletions

View File

@ -8,6 +8,8 @@
(assocf char '((#\) . 3) (#\] . 57) (#\} . 1197) (#\> . 25137)))) (assocf char '((#\) . 3) (#\] . 57) (#\} . 1197) (#\> . 25137))))
(define (incomplete-score char) (define (incomplete-score char)
(assocf char '((#\( . 1) (#\[ . 2) (#\{ . 3) (#\< . 4)))) (assocf char '((#\( . 1) (#\[ . 2) (#\{ . 3) (#\< . 4))))
(define (matching open close)
(member `(,open ,close) '((#\( #\)) (#\[ #\]) (#\{ #\}) (#\< #\>))))
(define (autocomplete-score stack) (define (autocomplete-score stack)
(for/fold ([score 0]) (for/fold ([score 0])
@ -26,10 +28,7 @@
[(cons char line) [(cons char line)
(match* (char stack) (match* (char stack)
[((or #\( #\[ #\{ #\<) _) (loop (cons char stack) line)] [((or #\( #\[ #\{ #\<) _) (loop (cons char stack) line)]
[(#\) (cons #\( stack)) (loop stack line)] [(close (cons open stack)) #:when (matching open close) (loop stack line)]
[(#\] (cons #\[ stack)) (loop stack line)]
[(#\} (cons #\{ stack)) (loop stack line)]
[(#\> (cons #\< stack)) (loop stack line)]
[(_ _) (values (+ corrupted (corrupted-score char)) incomplete)])])))) [(_ _) (values (+ corrupted (corrupted-score char)) incomplete)])]))))
(show-solution part1 part2) (show-solution part1 part2)