diff options
author | David Thompson <dthompson2@worcester.edu> | 2015-10-01 05:00:42 -0400 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2015-10-01 05:07:45 -0400 |
commit | eff835145de31c77f4d444c4548c1c0f170dc09f (patch) | |
tree | e26b3734430ce1410e0098f73b749e3cd887ab39 /problem-15.scm | |
parent | dd4cc3051689001803df35205462e31cfdbf9193 (diff) |
Add Problem 15 solution.
Diffstat (limited to 'problem-15.scm')
-rw-r--r-- | problem-15.scm | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/problem-15.scm b/problem-15.scm new file mode 100644 index 0000000..37bbfe3 --- /dev/null +++ b/problem-15.scm @@ -0,0 +1,42 @@ +;;; +;;; Problem 15: Lattice paths +;;; + +;;; https://projecteuler.net/problem=15 + +(define (memoize proc) + "Return a memoizing version of PROC." + (let ((cache (make-hash-table))) + (lambda args + (let ((results (hash-ref cache args))) + (if results + (apply values results) + (let ((results (call-with-values (lambda () + (apply proc args)) + list))) + (hash-set! cache args results) + (apply values results))))))) + +(define (lattice-path-count grid-size) + (define (end? x) + (= x grid-size)) + + (define (out-of-bounds? n) + (> n grid-size)) + + (define count + (memoize + (lambda (x y) + (cond + ((and (end? x) (end? y)) + 1) + ((or (out-of-bounds? x) + (out-of-bounds? y)) + 0) + (else + (+ (count (1+ x) y) + (count x (1+ y)))))))) + + (count 0 0)) + +(lattice-path-count 20) |