summaryrefslogtreecommitdiff
path: root/5-2.scm
diff options
context:
space:
mode:
authorlamp2023-12-08 14:23:12 +0000
committerlamp2023-12-08 14:23:12 +0000
commitd26f7d5ffb36368216a59b62771ee531a695b396 (patch)
tree455b74bba780638a092c574e6b61d571742e94df /5-2.scm
parente6af1cefde4c50b1138ca8559a329eb0a8925d4b (diff)
fix day 5 part 2 solution
Diffstat (limited to '5-2.scm')
-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))