Day 08: No need for a hash when a function will do.
This commit is contained in:
parent
62ae2ea1ca
commit
5b3afa6207
27
src/08.rkt
27
src/08.rkt
|
@ -7,7 +7,7 @@
|
||||||
(for/list ([entry (problem-input 8)])
|
(for/list ([entry (problem-input 8)])
|
||||||
(match-let ([(list patterns outputs) (string-split entry " | ")])
|
(match-let ([(list patterns outputs) (string-split entry " | ")])
|
||||||
(list (map string->list (string-words patterns))
|
(list (map string->list (string-words patterns))
|
||||||
(map #{sort (string->list %) char<?} (string-words outputs))))))
|
(map string->list (string-words outputs))))))
|
||||||
|
|
||||||
(define part1
|
(define part1
|
||||||
(~>> input
|
(~>> input
|
||||||
|
@ -15,7 +15,7 @@
|
||||||
(map #{count #{(or/c 2 3 4 7) (length %)} %})
|
(map #{count #{(or/c 2 3 4 7) (length %)} %})
|
||||||
sum))
|
sum))
|
||||||
|
|
||||||
(define (config patterns)
|
(define ((config patterns) segs)
|
||||||
(define (digit segments)
|
(define (digit segments)
|
||||||
(findf #{= (length %) segments} patterns))
|
(findf #{= (length %) segments} patterns))
|
||||||
(define (digits segments)
|
(define (digits segments)
|
||||||
|
@ -30,22 +30,23 @@
|
||||||
(define six (set-union (set-subtract eight one) sixes))
|
(define six (set-union (set-subtract eight one) sixes))
|
||||||
(define zero (set-union (set-subtract eight three) (set-subtract eight four) one))
|
(define zero (set-union (set-subtract eight three) (set-subtract eight four) one))
|
||||||
(define two (set-union (set-subtract zero sixes) fives))
|
(define two (set-union (set-subtract zero sixes) fives))
|
||||||
(hash (sort zero char<?) #\0
|
(cond
|
||||||
(sort one char<?) #\1
|
[(set=? zero segs) #\0]
|
||||||
(sort two char<?) #\2
|
[(set=? one segs) #\1]
|
||||||
(sort three char<?) #\3
|
[(set=? two segs) #\2]
|
||||||
(sort four char<?) #\4
|
[(set=? three segs) #\3]
|
||||||
(sort five char<?) #\5
|
[(set=? four segs) #\4]
|
||||||
(sort six char<?) #\6
|
[(set=? five segs) #\5]
|
||||||
(sort seven char<?) #\7
|
[(set=? six segs) #\6]
|
||||||
(sort eight char<?) #\8
|
[(set=? seven segs) #\7]
|
||||||
(sort nine char<?) #\9))
|
[(set=? eight segs) #\8]
|
||||||
|
[(set=? nine segs) #\9]))
|
||||||
|
|
||||||
(define part2
|
(define part2
|
||||||
(for/sum ([entry input])
|
(for/sum ([entry input])
|
||||||
(let ([mapping (config (first entry))])
|
(let ([mapping (config (first entry))])
|
||||||
(~>> (second entry)
|
(~>> (second entry)
|
||||||
(map #{hash-ref mapping %})
|
(map mapping)
|
||||||
list->string
|
list->string
|
||||||
string->number))))
|
string->number))))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue