; skyline puzzle
(define (heights buildings)
(let* ((len (apply max (map caddr buildings)))
(hites (make-vector (+ len 1) 0)))
(do ((buildings buildings (cdr buildings)))
((null? buildings) (vector->list hites))
(let ((building (car buildings)))
(do ((x (car building) (+ x 1))) ((= x (caddr building)))
(vector-set! hites x
(max (vector-ref hites x) (cadr building))))))))
(define (skyline buildings)
(let loop ((x 0) (prev 0) (hites (heights buildings)) (skys (list)))
(cond ((null? hites) (reverse skys))
((= (car hites) prev) (loop (+ x 1) prev (cdr hites) skys))
(else (loop (+ x 1) (car hites) (cdr hites) (cons (car hites) (cons x skys)))))))
(display (skyline '((1 11 5) (2 6 7) (3 13 9) (12 7 16) (14 3 25) (19 18 22) (23 13 29) (24 4 28))))