summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlamp2023-12-14 14:18:47 +0000
committerlamp2023-12-14 14:18:47 +0000
commit9afb406b25e00aa7a0a190598320f00f9ca86109 (patch)
tree421bc320f19a5e566564cd6ea94d3f397ea5dff6
parentf87b9321de3774b14b7fb6187666b47007a4bd66 (diff)
finish day 10 part 1main
-rwxr-xr-x10.scm23
1 files changed, 17 insertions, 6 deletions
diff --git a/10.scm b/10.scm
index 15c4b1c..b6b50b6 100755
--- a/10.scm
+++ b/10.scm
@@ -31,12 +31,23 @@
,(cons (1- x) y)))
((equal? type #\.) '()))))
-(let* ((port (open-input-file "10s.txt"))
+(let* ((port (open-input-file "10.txt"))
(lines (read-lines port))
- (candidates '((0 . 2)))
- (seen '()))
+ (candidates '(((0 . 2) . 0)))
+ (seen '())
+ (last #nil)
+ (start 0))
(close-port port)
+ (do ((i 0 (1+ i))) ((string-contains (list-ref lines i) "S")) (set! start i))
+ (set! candidates `(,(cons (cons (string-index (list-ref lines (1+ start)) #\S) (1+ start)) 0)))
(while (not (null? candidates))
- (set! candidates (append! (cdr candidates) (filter (lambda (x) (member x seen)) (candidates-for (string-ref (list-ref lines (cdar candidates)) (caar candidates)) (car candidates)))))
- (display candidates)
- (newline)))
+ (set! seen (append! seen `(,(caar candidates))))
+ (let* ((cands (candidates-for (string-ref (list-ref lines (cdaar candidates)) (caaar candidates)) (caar candidates)))
+ (cands (filter (lambda (x) (and (not (member x seen))
+ (not (negative? (car x)))
+ (not (negative? (cdr x)))
+ (member (caar candidates) (candidates-for (string-ref (list-ref lines (cdr x)) (car x)) x)))) cands))
+ (cands (map (lambda (x) (cons (cons (car x) (cdr x)) (1+ (cdar candidates)))) cands)))
+ (set! last (car candidates))
+ (set! candidates (append! (cdr candidates) cands))))
+ (display (cdr last)))