B. Pym
2024-06-11 08:51:08 UTC
VIER and NEUN represent 4-digit squares, each letter denoting a
distinct digit. You are asked to find the value of each, given the
further requirement that each uniquely determines the other.
The "further requirement" means that of the numerous pairs of
answers, choose the one in which each number only appears once
in all of the pairs.
Gauche Scheme
(use srfi-13) ;; string-map
(use srfi-42) ;; list-ec
(define squares4d
(map (lambda (n) (number->string (square n)))
(lrange 32 100)))
(define alphabet (map integer->char (lrange 65 91)))
(define (->pattern str)
(let ((table
(map cons
(delete-duplicates (string->list str))
alphabet)))
(string-map (cut assoc-ref table <>) str)))
(define possibles
(let ((pat (->pattern "NEUNVIER")))
(list-ec
(:list n squares4d)
(:list v squares4d)
(if (equal? pat (->pattern (string-append n v))))
(list n v))))
(define (count* f xs)
(count (lambda(ys) (equal? (f xs) (f ys))) possibles))
(find
(lambda(nv) (= 1 (count* car nv) (count* cadr nv)))
possibles)
===>
("9409" "6241")
distinct digit. You are asked to find the value of each, given the
further requirement that each uniquely determines the other.
The "further requirement" means that of the numerous pairs of
answers, choose the one in which each number only appears once
in all of the pairs.
Gauche Scheme
(use srfi-13) ;; string-map
(use srfi-42) ;; list-ec
(define squares4d
(map (lambda (n) (number->string (square n)))
(lrange 32 100)))
(define alphabet (map integer->char (lrange 65 91)))
(define (->pattern str)
(let ((table
(map cons
(delete-duplicates (string->list str))
alphabet)))
(string-map (cut assoc-ref table <>) str)))
(define possibles
(let ((pat (->pattern "NEUNVIER")))
(list-ec
(:list n squares4d)
(:list v squares4d)
(if (equal? pat (->pattern (string-append n v))))
(list n v))))
(define (count* f xs)
(count (lambda(ys) (equal? (f xs) (f ys))) possibles))
(find
(lambda(nv) (= 1 (count* car nv) (count* cadr nv)))
possibles)
===>
("9409" "6241")