1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- (import
- (except (rnrs base) let-values map error)
- (only (guile)
- lambda* λ
- command-line
- make-typed-array)
- (srfi srfi-1)
- (fileio))
- (define println
- (λ (thing)
- (simple-format (current-output-port) "~a\n" thing)))
- (define lines->landscape
- (λ (lines)
- (list->array 2 (map string->list lines))))
- (define count-trees
- (lambda* (landscape #:key (rows-step 1) (cols-step 1))
- (let* ([dim (array-dimensions landscape)]
- [height (first dim)]
- [width (second dim)])
- (let next-step ([cur-row-ind 0]
- [cur-col-ind 0]
- [encountered-trees 0])
- (cond
- [(>= cur-row-ind height) encountered-trees]
- [(char=? (array-ref landscape cur-row-ind cur-col-ind) #\#)
- (next-step (+ cur-row-ind rows-step)
- (remainder (+ cur-col-ind cols-step) width)
- (+ encountered-trees 1))]
- [else
- (next-step (+ cur-row-ind rows-step)
- (remainder (+ cur-col-ind cols-step) width)
- encountered-trees)])))))
- (define main
- (λ (cmd-line-args)
- (let* ([lines (get-lines-from-file (second cmd-line-args))]
- [landscape (lines->landscape lines)])
- (apply *
- (map (λ (step-pair)
- (count-trees landscape
- #:rows-step (car step-pair)
- #:cols-step (cdr step-pair)))
- '((1 . 1)
- (1 . 3)
- (1 . 5)
- (1 . 7)
- (2 . 1)))))))
- (simple-format (current-output-port)
- "~a\n"
- (main (command-line)))
|