-
Notifications
You must be signed in to change notification settings - Fork 1
/
32.rkt
29 lines (24 loc) · 1023 Bytes
/
32.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#lang racket
; Hey Emacs, this is -*-scheme-*- code!
(require "digits.ss"
"next-permutation.rkt")
(define products (mutable-set))
(define (try-permutation digits)
(for ([left-index (range 1 (sub1 (vector-length digits)))])
(let-values ([(left-v rest) (vector-split-at digits left-index)])
(for ([middle-index (range 1 (sub1 (vector-length rest)))])
(let-values ([(middle-v right-v) (vector-split-at rest middle-index)])
(define left (digits->number left-v))
(define middle (digits->number middle-v))
(define right (digits->number right-v))
(define is-solution? (= right (* left middle )))
(when is-solution?
(printf "~a * ~a = ~a!!~%" left middle right)
(set-add! products right)))
))))
(let loop ([digits (for/vector ([x(in-range 1 10)]) x)])
(when digits
(try-permutation digits)
(loop (next-permutation digits)))
)
(printf "Submit this as the answer: ~a~%" (for/sum ([p products]) p))