summaryrefslogtreecommitdiff
path: root/6.scm
blob: 9a440c4caaabac28ff963fc13525cef330f49dc1 (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
#!/usr/bin/env -S guile -s
!#
(use-modules (ice-9 regex)
             (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 (distance total-time hold-time)
  (* (- total-time hold-time) hold-time))

(define (all-distances time)
  (let ((distances '()))
    (do ((i 0 (1+ i))) ((> i time))
      (set! distances (append distances `(,(distance time i)))))
    distances))

(let* ((port (open-input-file "6.txt"))
       (lines (read-lines port))
       (races (map (lambda (x y) (list x y))
                   (split-up (match:substring (string-match ": ([0-9| ]+)" (car lines)) 1))
                   (split-up (match:substring (string-match ": ([0-9| ]+)" (cadr lines)) 1))))
       )
  (close-port port)
  (display (fold * 1 (map (lambda (x) (length (filter (lambda (y) (> y (cadr x))) (all-distances (car x))))) races))))