summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x5-2.scm25
1 files changed, 14 insertions, 11 deletions
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))