1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
(import (chicken process-context)
(chicken io))
; there's not a good way to make this consistent across schemes or interpreted/compiled programs
; so this bit might mess up on your system
(if (< (length (command-line-arguments)) 2)
(error "usage: 1.scm datafile"))
; if it doesn't work, replace (cadr (command-line-arguments)) with the filename
(define txtlines (call-with-input-file (cadr (command-line-arguments)) (lambda (port) (read-lines port))))
(define lines (map string->number txtlines))
(define (num-larger array)
(+ (if (< (car array) (cadr array)) 1 0)
(if (< (length array) 3) 0 (num-larger (cdr array)))))
; part 1
(print (num-larger lines))
; part 2
(print (num-larger (map + lines (cdr lines) (cddr lines))))
|