#lang curly-fn racket (require racket/set "../lib.rkt") (define input (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> input (map second) (map #{count #{(or/c 2 3 4 7) (length %)} %}) sum)) (define (config patterns) (define (digit segments) (findf #{= (length %) segments} patterns)) (define (digits segments) (filter #{= (length %) segments} patterns)) (define-values (one four seven eight) (values (digit 2) (digit 4) (digit 3) (digit 7))) (define-values (fives sixes) (values (digits 5) (digits 6))) (define top (set-first (set-subtract seven one))) (define bottom (set-first (set-subtract (apply set-intersect fives) four `(,top)))) (define middle (set-first (set-subtract (apply set-intersect fives) `(,top ,bottom)))) (define top-left (set-first (set-subtract four one `(,middle)))) (define bottom-right (set-first (set-subtract (apply set-intersect sixes) `(,top ,bottom ,top-left)))) (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> (second entry) (map #{hash-ref mapping %}) list->string string->number)))) (show-solution part1 part2)