aboutsummaryrefslogtreecommitdiff
path: root/02/1.scm
blob: 2c5c9e0ce3b5979bba6396b3b88b5a6b80b3fd0b (plain)
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
36
37
38
39
40
41
(import (chicken string))
(import (chicken io))
(import (chicken sort))
(import matchable)
(import srfi-1)

(define input
  (with-input-from-file "input"
    (lambda ()
      (let loop ((ret '()))
        (let ((line (read-line)))
          (if (eof-object? line)
              (reverse ret)
              (loop (cons line ret))))))))

(set! input (map string-split input))
(set! input (map (lambda (lst) (map string->number lst)) input))

(define (diff in)
  (let loop ((lst in) (ret '()))
    (match lst
      ((a b . r) (loop (cdr lst) (cons (- a b) ret)))
      ((a) (reverse ret)))))

(define ladder (map diff input))

(define (same-sign in)
  (define prod (map (lambda (x) (* x (car in))) in))
  (foldl (lambda (a b) (and a b)) #t (map (lambda (x) (> x 0)) prod)))

(define (safe in)
  (not (any (lambda (x) (> (abs x) 3)) in)))

(define result
  (map (lambda (a b) (and a b))
       (map same-sign ladder)
       (map safe ladder)))

(display (length (filter (lambda (x) x) result)))
(newline)