commit 64b7929c1294e58683258bb94937834b0bc097bb parent ba6290ab3646f8dd5d437ff8c7ace741a9b716ab Author: mpizzzle <m@michaelpercival.xyz> Date: Thu, 3 Dec 2020 13:22:41 +0000 simplifying arguments Diffstat:
| M | 2020/puzzle_3.scm | | | 20 | +++++++++++--------- |
1 file changed, 11 insertions(+), 9 deletions(-)
diff --git a/2020/puzzle_3.scm b/2020/puzzle_3.scm @@ -1,17 +1,19 @@ (define (check-slice slice x) (if (char=? (string-ref slice (modulo x (string-length slice))) ##) 1 0)) -(define (count-trees tree-map x y slope) - (if (not (null? tree-map)) - (let ((parity (= (modulo y (car (cdr slope))) 0))) - (+ (if parity - (+ (check-slice (car tree-map) x)) 0) - (count-trees (cdr tree-map) (+ x (if parity (car slope) 0)) (+ y 1) slope))) - 0)) +(define (count-trees tree-map slope) + (define (recurse-trees tree-map x y slope) + (if (not (null? tree-map)) + (let ((parity (= (modulo y (car (cdr slope))) 0))) + (+ (if parity + (+ (check-slice (car tree-map) x)) 0) + (recurse-trees (cdr tree-map) (+ x (if parity (car slope) 0)) (+ y 1) slope))) + 0)) + (recurse-trees tree-map 0 0 slope)) (load "read_lines.scm") (define input (read-lines "files/3.txt" (lambda (x) x))) (define slopes '((1 1) (3 1) (5 1) (7 1) (1 2))) -(display (count-trees input 0 0 (car (cdr slopes)))) (newline) -(display (apply * (map (lambda (slope) (count-trees input 0 0 slope)) slopes))) (newline) +(display (count-trees input (car (cdr slopes)))) (newline) +(display (apply * (map (lambda (slope) (count-trees input slope)) slopes))) (newline)