From d26f7d5ffb36368216a59b62771ee531a695b396 Mon Sep 17 00:00:00 2001 From: lamp Date: Fri, 8 Dec 2023 14:23:12 +0000 Subject: fix day 5 part 2 solution --- 5-2.scm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) (limited to '5-2.scm') diff --git a/5-2.scm b/5-2.scm index d3aebc2..0664348 100755 --- a/5-2.scm +++ b/5-2.scm @@ -16,7 +16,7 @@ (define (perform-mapping val map) (let* ((mapping (filter (lambda (x) (and (>= val (cadr x)) - (<= val (+ (cadr x) (caddr x))))) map))) + (< val (+ (cadr x) (caddr x))))) map))) (if (null? mapping) val (+ val (- (caar mapping) (cadar mapping)))))) @@ -40,23 +40,26 @@ (set! lines (cdr lines))) `(,current-map ,lines))) +(define (in-ranges ranges val) + (let ((found #f)) + (while (and (not (null? ranges)) (not found)) + (if (and (>= val (car ranges)) + (< val (+ (car ranges) (cadr ranges)))) + (set! found #t) + (set! ranges (cddr ranges)))) + found)) + (let* ((port (open-input-file "5.txt")) (lines (read-lines port)) (seeds (split-up (match:substring (string-match ": ([0-9| ]+)" (car lines)) 1))) (lines (cdr lines)) (maps '()) - (current #nil)) + (index 0)) (close-port port) (do ((i 0 (1+ i))) ((= i 7)) (let ((res (parse-next-map lines))) (set! maps (append maps `(,(car res)))) (set! lines (cadr res)))) - (set! current (perform-all-mappings (car seeds) maps)) - (while (not (null? seeds)) - (display "Starting Iteration...\n") - (do ((j 0 (1+ j))) ((= j (cadr seeds))) - (let ((val (perform-all-mappings (+ (car seeds) j) maps))) - (when (> current val) - (set! current val)))) - (set! seeds (cddr seeds))) - (display current)) + (set! maps (reverse (map (lambda (x) (map (lambda (y) `(,(cadr y) ,(car y) ,(caddr y))) x)) maps))) + (while (not (in-ranges seeds (perform-all-mappings index maps))) (set! index (1+ index))) + (display index)) -- cgit v1.2.3