blob: fe3f0733b832be4568982e5d8ac8164b6abf4f43 (
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
|
#!/usr/bin/env -S guile -s
!#
(use-modules (ice-9 textual-ports)
(srfi srfi-1))
(define (read-lines port)
(letrec ((loop (lambda (l ls)
(if (eof-object? l)
ls
(loop (get-line port) (cons l ls))))))
(reverse (loop (get-line port) '()))))
(define (split-up nums)
(map string->number (filter (lambda (x) (not (string-null? x))) (string-split nums char-set:whitespace))))
(define (seq-diff seq)
(map (lambda (x y) (- y x)) (drop-right seq 1) (cdr seq)))
(define (all-seq-diffs seq)
(letrec ((loop (lambda (l ls)
(if (every zero? l) ls
(loop (seq-diff l) (cons l ls))))))
(loop seq '())))
(define (predict seq-diffs)
(fold - 0 (map car seq-diffs)))
(let* ((port (open-input-file "9.txt"))
(lines (map split-up (read-lines port))))
(close-port port)
(display (fold + 0 (map (lambda (x) (predict (all-seq-diffs x))) lines))))
|