adventofcode

https://adventofcode.com/
Log | Files | Refs

commit de9a61b1ea83a5285f4c4cc3a44cc677f0b1f0b9
parent 5a0f4f798db2175b88afe1898e967e440821ebe7
Author: mpizzzle <m@michaelpercival.xyz>
Date:   Thu,  3 Dec 2020 19:53:29 +0000

more refactoring, less recursion

Diffstat:
M2020/puzzle_2.scm | 24+++++++++++-------------
1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/2020/puzzle_2.scm b/2020/puzzle_2.scm @@ -1,23 +1,21 @@ (define (string-idx str chr) - (- (length (string->list str)) (length (memq chr (string->list str))))) + (- (string-length str) (length (memq chr (string->list str))))) (define (char-count str chr) - (apply + (map (lambda (b) (if b 1 0))(map (lambda (c) (char=? c chr)) (string->list str))))) + (apply + (map (lambda (b) (if b 1 0)) (map (lambda (c) (char=? c chr)) (string->list str))))) (define (validate-passwords entries policy) - (if (not (null? entries)) - (+ (let - ((idx (string-idx (car entries) #-)) - (idx2 (string-idx (car entries) #space)) - (entry (car entries))) + (apply + (map (lambda (entry) + (let + ((idx (string-idx entry #-)) + (jdx (string-idx entry #space))) (let ((i (string->number (substring entry 0 idx))) - (j (string->number (substring entry (+ idx 1) idx2))) - (c (string-ref entry (+ idx2 1))) - (s (substring entry (+ idx2 4) (string-length entry)))) - (policy s c i j))) - (validate-passwords (cdr entries) policy)) - 0)) + (j (string->number (substring entry (+ idx 1) jdx))) + (c (string-ref entry (+ jdx 1))) + (s (substring entry (+ jdx 4) (string-length entry)))) + (policy s c i j)))) + entries))) (define policy-1 (lambda (s c i j)