From eff835145de31c77f4d444c4548c1c0f170dc09f Mon Sep 17 00:00:00 2001 From: David Thompson Date: Thu, 1 Oct 2015 05:00:42 -0400 Subject: Add Problem 15 solution. --- problem-15.scm | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 problem-15.scm 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) -- cgit v1.2.3