#lang racket (define input (string-split (string-trim (file->string "input.txt")))) (define (parse-button button) (match button [(pregexp #px"^\\[(\\d+)\\]$" (list _ a)) (list 'app (string->number a))] [(pregexp #px"^\\[\\*(\\d+)\\]$" (list _ a)) (list 'mul (string->number a))] [(pregexp #px"^\\[/(\\d+)\\]$" (list _ a)) (list 'div (string->number a))] [(pregexp #px"^\\[(\\d+)->(\\d+)\\]$" (list _ a b)) (list 'rep a b)] ["[oct]" (list 'oct)] ["[bksp]" (list 'bksp)])) (define buttons (for/list ([button input]) (parse-button button))) (define (press button number) (match button [(list 'app a) (+ (* number 10) a)] [(list 'mul a) (* number a)] [(list 'div a) (truncate (/ number a))] [(list 'rep a b) (string->number (string-replace (number->string number) a b))] [(list 'oct) (string->number (number->string number 8))] [(list 'bksp) (truncate (/ number 10))])) (define (find-all-numbers found depth) (cond [(>= depth 3) found] [else (define new-found (for/fold ([acc (set)]) ([number found]) (set-union acc (for/set ([button buttons]) (press button number))))) (find-all-numbers new-found (+ depth 1))])) (apply + (set->list (find-all-numbers (set 0) 0)))