Quest 6: Mentorship Matrix

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

  • vole@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    3 months ago

    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))))