puzzle_8.scm (1213B)
1 (load "read_lines.scm") 2 (load "tokenize.scm") 3 4 (define (modify-retry i full) 5 (let ((t (find-tail (lambda (x) (or (string=? (car x) "jmp") (string=? (car x) "nop"))) (list-tail i 1)))) 6 (let ((s (string=? (caar t) "jmp"))) 7 (let ((ins (cons (list (if s "nop" "jmp") (cadar t)) (list-tail t 1)))) 8 (execute ins (append (take full (- (length full) (length ins))) ins) full 0 0 '()))))) 9 10 (define (execute i instructions full ptr acc used) 11 (if (< ptr (length instructions)) 12 (let ((ins (list-ref instructions ptr)) 13 (u (append used (list ptr)))) 14 (if (null? (lset-intersection eq? (list ptr) used)) 15 (cond 16 ((string=? (car ins) "acc") 17 (execute i instructions full (+ ptr 1) (+ acc (string->number (cadr ins))) u)) 18 ((string=? (car ins) "jmp") 19 (execute i instructions full (+ ptr (string->number (cadr ins))) acc u)) 20 ((string=? (car ins) "nop") 21 (execute i instructions full (+ ptr 1) acc u))) 22 (modify-retry i full))) 23 ;acc)) 24 acc)) 25 26 (define instructions (read-lines "files/8.txt" (lambda (x) (tokenize x #space)))) 27 28 (display (execute instructions instructions instructions 0 0 '())) (newline)