commit 707e673e9567cbd03dcd56bb3addb7e10bf1deeb parent 8ab8a2fd78cca3f48fb4704f68d2743ceb46c329 Author: mpizzzle <m@michaelpercival.xyz> Date: Wed, 2 Dec 2020 21:59:32 +0000 puzzle 2 part 2 complete. Diffstat:
| M | 2020/puzzle_2.scm | | | 30 | ++++++++++++++++++++---------- |
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/2020/puzzle_2.scm b/2020/puzzle_2.scm @@ -8,11 +8,11 @@ (reverse lines)) (loop (read-line p) (cons line lines)))))) -(define (string-index str tok idx) +(define (string-index str chr idx) (if (not (null? str)) - (if (char=? (car (string->list str)) tok) + (if (char=? (car (string->list str)) chr) idx - (string-index (list->string (cdr (string->list str))) tok (+ idx 1))) + (string-index (list->string (cdr (string->list str))) chr (+ idx 1))) -1)) (define (char-count str chr count) @@ -21,7 +21,7 @@ (if (char=? (car (string->list str)) chr)(+ count 1) count)) count)) -(define (valid-password entry) +(define (valid-password entry policy) (let ((idx (string-index entry #- 0)) (idx2 (string-index entry #space 0))) @@ -30,15 +30,25 @@ (j (string->number (substring entry (+ idx 1) idx2))) (c (string-ref entry (+ idx2 1))) (s (substring entry (+ idx2 4) (string-length entry)))) - (let ((count (char-count s c 0))) - (if (and (>= count i) (<= count j)) 1 0))))) + (policy s c i j)))) -(define (part-1 entries) +(define (loop entries policy) (if (not (null? entries)) - (+ (valid-password (car entries)) - (part-1 (cdr entries))) + (+ (valid-password (car entries) policy) + (loop (cdr entries) policy)) 0)) +(define policy-1 + (lambda (s c i j) + (let ((count (char-count s c 0))) + (if (and (>= count i) (<= count j)) 1 0)))) + +(define policy-2 + (lambda (s c i j) + (if (not (equal? (char=? (string-ref s (- i 1)) c) + (char=? (string-ref s (- j 1)) c))) 1 0))) + (define input (read-lines "files/2.txt")) -(part-1 input) +(loop input policy-1) +(loop input policy-2)