Browse Source

refactor DP

Mistivia 1 year ago
parent
commit
9d68d71e81
1 changed files with 10 additions and 8 deletions
  1. 10 8
      12/2.rkt

+ 10 - 8
12/2.rkt

@@ -54,7 +54,7 @@
 
 (define (possible-arr-num instance)
   (define conds (cdr instance))
-  (define (impl recur scanned unscanned remain)
+  (define (impl scanned unscanned remain)
     (define can-match (can-match? scanned remain))
     (define (gen-next-scanned c lst)
       (if (and (null? lst) (char=? c #\.))
@@ -75,19 +75,21 @@
                     remain))
               (define head (car unscanned))
               (if (not (char=? head #\?))
-                  (recur recur (gen-next-scanned head next-scanned) (cdr unscanned) next-remain)
-                  (+ (recur recur (gen-next-scanned #\. next-scanned) (cdr unscanned) next-remain)
-                     (recur recur (gen-next-scanned #\# next-scanned) (cdr unscanned) next-remain)))))))
+                  (impl (gen-next-scanned head next-scanned) (cdr unscanned) next-remain)
+                  (+ (impl (gen-next-scanned #\. next-scanned) (cdr unscanned) next-remain)
+                     (impl (gen-next-scanned #\# next-scanned) (cdr unscanned) next-remain)))))))
   (define memo (make-hash))
-  (define (memo-impl recur scanned unscanned remain)
+  (define original-impl impl)
+  (define (memo-impl scanned unscanned remain)
     (if (null? scanned)
       (if (hash-has-key? memo (list unscanned remain))
         (hash-ref memo (list unscanned remain))
         (let ()
-          (define ret (impl recur scanned unscanned remain))
+          (define ret (original-impl scanned unscanned remain))
           (hash-set! memo (list unscanned remain) ret)
           ret))
-      (impl recur scanned unscanned remain)))
-  (memo-impl memo-impl '() (car instance) conds))
+      (original-impl scanned unscanned remain)))
+  (set! impl memo-impl)
+  (impl '() (car instance) conds))
 
 (apply + (map possible-arr-num (map parse-line lines)))