Day 08: No need for a hash when a function will do.

This commit is contained in:
Jonathan Chan 2021-12-08 02:29:43 -08:00
parent 62ae2ea1ca
commit 5b3afa6207
1 changed files with 14 additions and 13 deletions

View File

@ -7,7 +7,7 @@
(for/list ([entry (problem-input 8)])
(match-let ([(list patterns outputs) (string-split entry " | ")])
(list (map string->list (string-words patterns))
(map #{sort (string->list %) char<?} (string-words outputs))))))
(map string->list (string-words outputs))))))
(define part1
(~>> input
@ -15,7 +15,7 @@
(map #{count #{(or/c 2 3 4 7) (length %)} %})
sum))
(define (config patterns)
(define ((config patterns) segs)
(define (digit segments)
(findf #{= (length %) segments} patterns))
(define (digits segments)
@ -30,22 +30,23 @@
(define six (set-union (set-subtract eight one) sixes))
(define zero (set-union (set-subtract eight three) (set-subtract eight four) one))
(define two (set-union (set-subtract zero sixes) fives))
(hash (sort zero char<?) #\0
(sort one char<?) #\1
(sort two char<?) #\2
(sort three char<?) #\3
(sort four char<?) #\4
(sort five char<?) #\5
(sort six char<?) #\6
(sort seven char<?) #\7
(sort eight char<?) #\8
(sort nine char<?) #\9))
(cond
[(set=? zero segs) #\0]
[(set=? one segs) #\1]
[(set=? two segs) #\2]
[(set=? three segs) #\3]
[(set=? four segs) #\4]
[(set=? five segs) #\5]
[(set=? six segs) #\6]
[(set=? seven segs) #\7]
[(set=? eight segs) #\8]
[(set=? nine segs) #\9]))
(define part2
(for/sum ([entry input])
(let ([mapping (config (first entry))])
(~>> (second entry)
(map #{hash-ref mapping %})
(map mapping)
list->string
string->number))))