summary refs log tree commit diff stats
path: root/day4.scm
blob: 29eefb0cac902631dcfa00e9b02f35927f351ce0 (plain) (blame)
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
30
31
32
33
34
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))