p099.scm 816 B

123456789101112131415161718192021222324252627282930
  1. ;; Largest exponential
  2. ;; Pretty easy since guile can compare large numbers effectively
  3. (define-module (solved p099))
  4. (use-modules (euler utils)
  5. (rnrs io ports))
  6. (define (line-of-largest-exponent-in-file file)
  7. (1+ (list-maximum-index >
  8. (expt-log-pairs (get-pairs file)))))
  9. ;; What I did originally, but takes much longer
  10. (define (exponentiate-pairs pairs)
  11. (map (λ (pair idx) (apply expt pair))
  12. pairs
  13. (iota (length pairs))))
  14. (define (expt-log-pairs pairs)
  15. (map (λ (pair) (* (cadr pair) (log (car pair)))) pairs))
  16. (define (get-pairs file)
  17. (let ([port (open-input-file file)])
  18. (let lp ([line (get-line port)] [acc '()] )
  19. (if (eof-object? line) (reverse acc)
  20. (lp (get-line port)
  21. (cons
  22. (map string->number (string-split line #\,))
  23. acc))))))