diff options
Diffstat (limited to 'day4.scm')
-rwxr-xr-x | day4.scm | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/day4.scm b/day4.scm new file mode 100755 index 0000000..29eefb0 --- /dev/null +++ b/day4.scm @@ -0,0 +1,35 @@ +#!/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)) |