puzzle-01.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. (import
  2. (except (rnrs base) let-values map error)
  3. (only (guile)
  4. lambda* λ
  5. command-line
  6. make-typed-array)
  7. (srfi srfi-1)
  8. (fileio))
  9. (define println
  10. (λ (thing)
  11. (simple-format (current-output-port) "~a\n" thing)))
  12. (define lines->landscape
  13. (λ (lines)
  14. (list->array 2 (map string->list lines))))
  15. (define main
  16. (λ (cmd-line-args)
  17. (let* ([lines (get-lines-from-file (second cmd-line-args))]
  18. [rows-step (string->number (third cmd-line-args))]
  19. [cols-step (string->number (fourth cmd-line-args))]
  20. [landscape (lines->landscape lines)])
  21. ;; assuming all lines have the same length
  22. (let* ([dim (array-dimensions landscape)]
  23. [height (first dim)]
  24. [width (second dim)])
  25. (let next-step ([cur-row-ind 0]
  26. [cur-col-ind 0]
  27. [encountered-trees 0])
  28. (cond
  29. [(>= cur-row-ind height) encountered-trees]
  30. [(char=? (array-ref landscape cur-row-ind cur-col-ind) #\#)
  31. (next-step (+ cur-row-ind rows-step)
  32. (remainder (+ cur-col-ind cols-step) width)
  33. (+ encountered-trees 1))]
  34. [else
  35. (next-step (+ cur-row-ind rows-step)
  36. (remainder (+ cur-col-ind cols-step) width)
  37. encountered-trees)]))))))
  38. (simple-format (current-output-port)
  39. "~a\n"
  40. (main (command-line)))