This is my solution for Day 1 of this year’s Advent of Code.
You may also enjoy browsing the Day 1 solutions megathread on Reddit.
PROBLEM
The captcha requires you to review a sequence of digits (your puzzle input) and find the sum of all digits that match the next digit in the list. The list is circular, so the digit after the last digit is the first digit in the list.
For example:

1122 produces a sum of 3 (1 + 2) because the first digit (1) matches the second digit and the third digit (2) matches the fourth digit.

1111 produces 4 because each digit (all 1) matches the next.

1234 produces 0 because no digit matches the next.

91212129 produces 9 because the only digit that matches the next one is the last digit, 9.
SOLUTION
(define captchainput "5994521226795838") '(set! captchainput "1111") '(set! captchainput "1122") '(set! captchainput "1234") '(set! captchainput "91212129") (define (gathermatches s) ;; String > List (let ((inport (makestringinputport s)) (count 0) (head #f) (vals '())) (let loop ((cur (readchar inport)) (next (peekchar inport)) (count count) (vals vals)) (if (eofobject? next) (if (char=? cur head) (cons cur vals) vals) (cond ((= count 0) (begin (set! head cur) (loop cur next (+ 1 count) vals))) ((char=? cur next) (loop (readchar inport) (peekchar inport) (+ 1 count) (cons cur vals))) (else (loop (readchar inport) (peekchar inport) (+ 1 count) vals))))))) (define (main prog+args) (let* ((matches (gathermatches captchainput)) (matches* (map (lambda (c) (string>number (string c))) matches)) (sum (apply + matches*))) (begin (format #t "MATCHES*: ~A~%" matches*) (format #t "SUM: ~A~%" sum))))
Advertisements