#!/usr/bin/env gosh (use scheme.set) (define max-num 746325) (define min-num 264360) (define num-range (iota (- max-num min-num -1) min-num)) (define (digit-list number) (map digit->integer (string->list (number->string number)))) (define (valid-pw? number) (letrec ((num-list (digit-list number)) (sorted-num-list (sort num-list)) (num-set (list->set eq-comparator num-list))) (and (> (length num-list) (set-size num-set)) (equal? sorted-num-list num-list)))) (define part1 (length (filter valid-pw? num-range))) (define (valid-count? number) (letrec ((num-list (digit-list number)) (counts (fold (lambda (element hashtable) (hash-table-update! hashtable element (cut + 1 <>) 0) hashtable) (make-hash-table eq-comparator) num-list))) (any (lambda (x) (= x 2)) (hash-table-values counts)))) (define part2 (length (filter (lambda (x) (and (valid-pw? x) (valid-count? x))) num-range))) (define (main args) (print part1) (print part2))