1
0
Fork 0

Day 08: Directly produce digits instead of individual segments.

This commit is contained in:
Jonathan Chan 2021-12-08 02:19:29 -08:00 committed by Jonathan Chan
parent 466c59e52b
commit c623a281ac
1 changed files with 15 additions and 23 deletions

View File

@ -22,32 +22,24 @@
(filter #{= (length %) segments} patterns)) (filter #{= (length %) segments} patterns))
(define-values (one four seven eight) (define-values (one four seven eight)
(values (digit 2) (digit 4) (digit 3) (digit 7))) (values (digit 2) (digit 4) (digit 3) (digit 7)))
(define-values (fives sixes) (define fives (apply set-intersect (digits 5)))
(values (digits 5) (digits 6))) (define sixes (apply set-intersect (digits 6)))
(define top (define three (set-union one fives))
(set-first (set-subtract seven one))) (define nine (set-union four sixes))
(define bottom (define five (set-union (set-subtract four one) sixes))
(set-first (set-subtract (apply set-intersect fives) four `(,top)))) (define six (set-union (set-subtract eight one) sixes))
(define middle (define zero (set-union (set-subtract eight three) (set-subtract eight four) one))
(set-first (set-subtract (apply set-intersect fives) `(,top ,bottom)))) (define two (set-union (set-subtract zero sixes) fives))
(define top-left (hash (sort zero char<?) #\0
(set-first (set-subtract four one `(,middle)))) (sort one char<?) #\1
(define bottom-right (sort two char<?) #\2
(set-first (set-subtract (apply set-intersect sixes) `(,top ,bottom ,top-left)))) (sort three char<?) #\3
(define top-right
(set-first (set-remove one bottom-right)))
(define bottom-left
(set-first (set-subtract eight `(,top ,bottom ,top-left ,top-right ,bottom-right ,middle))))
(hash (sort one char<?) #\1
(sort four char<?) #\4 (sort four char<?) #\4
(sort five char<?) #\5
(sort six char<?) #\6
(sort seven char<?) #\7 (sort seven char<?) #\7
(sort eight char<?) #\8 (sort eight char<?) #\8
(sort (set-subtract eight `(,top-left ,bottom-right)) char<?) #\2 (sort nine char<?) #\9))
(sort (set-subtract eight `(,top-left ,bottom-left)) char<?) #\3
(sort (set-subtract eight `(,top-right ,bottom-left)) char<?) #\5
(sort (set-remove eight middle) char<?) #\0
(sort (set-remove eight top-right) char<?) #\6
(sort (set-remove eight bottom-left) char<?) #\9))
(define part2 (define part2
(for/sum ([entry input]) (for/sum ([entry input])