Scheme/Guile
Part 3 was a fun little challenge.
(import (rnrs io ports (6)))
#!curly-infix
(define (parse-file file-name) (string-trim-both (call-with-input-file file-name get-string-all)))
(let* ((line (parse-file "notes/everybody_codes_e2025_q06_p1.txt"))
(line-length (string-length line)))
(let loop ((i 0) (knight-count 0) (mentor-count 0))
(if {i < line-length}
(let ((letter (string-ref line i)))
(loop (1+ i) (+ knight-count (if (eq? letter #\A) 1 0)) (+ mentor-count (if (eq? letter #\a) knight-count 0))))
(format #t "P1 Answer: ~a\n\n" mentor-count))))
(let* ((line (parse-file "notes/everybody_codes_e2025_q06_p2.txt"))
(line-length (string-length line)))
(let loop ((i 0) (knight-counts '()) (mentor-count 0))
(if {i < line-length}
(let ((letter (string-ref line i)))
(loop
(1+ i)
(if (char-upper-case? letter) (assq-set! knight-counts letter (1+ (or (assq-ref knight-counts letter) 0))) knight-counts)
(+ mentor-count (if (char-lower-case? letter) (or (assq-ref knight-counts (char-upcase letter)) 0) 0))))
(format #t "P2 Answer: ~a\n\n" mentor-count))))
(let* ((line (parse-file "notes/everybody_codes_e2025_q06_p3.txt"))
(line-length (string-length line)))
(let loop ((i 0) (mentor-count 0))
(if {i < line-length}
(let ((letter (string-ref line i)))
(loop
(1+ i)
(+ mentor-count
(if (char-lower-case? letter)
(let loop ((j (- i 1000)) (mentors-here 0))
(if {j <= (+ i 1000)}
(loop
(1+ j)
(+ mentors-here
(if {(string-ref line {j modulo line-length}) eq? (char-upcase letter)}
(if (and {0 <= j} {j < line-length}) 1000 999)
0)))
mentors-here))
0))))
(format #t "P3 Answer: ~a\n\n" mentor-count))))