Quest 1: Whispers in the Shell

  • 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/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 4 points 1 year ago* (last edited 1 year ago)

I'm still learning Scheme, but I wrote a solution in Guile:

(use-modules (ice-9 rdelim ))
(use-modules (srfi srfi-1))

(define (parse-step s) (*
    (if (eq? (string-ref s 0) #\L) -1 1)
    (string->number (substring s 1))
))
(define (parse-file file-name) (begin
  (define p (open-input-file file-name))
  (define names-str (read-line p))
  (read-line p)
  (define steps-str (read-line p))
  (cons
    (string-split names-str #\,)
    (map parse-step (string-split steps-str #\,))
  )
))

(let* ((parsed-file (parse-file "notes/everybody_codes_e2025_q01_p1.txt"))
       (names (car parsed-file))
       (steps (cdr parsed-file)))
  (format #t "names: ~a\nsteps: ~a\n" names steps)
  (define name-count (length names))
  (define chosen-name-idx (reduce
    (lambda (pos change) (max 0 (min (- name-count 1) (+ pos change))))
    0 steps))
  (format #t "P1: Chosen name(~a): ~a\n\n" chosen-name-idx (list-ref names chosen-name-idx))
)

(let* ((parsed-file (parse-file "notes/everybody_codes_e2025_q01_p2.txt"))
       (names (car parsed-file))
       (steps (cdr parsed-file)))
  (format #t "names: ~a\nsteps: ~a\n" names steps)
  (define name-count (length names))
  (define chosen-name-idx (modulo (reduce + 0 steps) name-count))
  (format #t "P2: Chosen name(~a): ~a\n\n" chosen-name-idx (list-ref names chosen-name-idx))
)

(let* ((parsed-file (parse-file "notes/everybody_codes_e2025_q01_p3.txt"))
       (names (list->vector (car parsed-file)))
       (steps (cdr parsed-file)))
  (format #t "names: ~a\nsteps: ~a\n" names steps)
  (define name-count (vector-length names))
  (for-each (lambda (s)
      (define head (vector-ref names 0))
      (define swap-pos (modulo s name-count))
      (vector-set! names 0 (vector-ref names swap-pos))
      (vector-set! names swap-pos head)
    ) steps)
  (format #t "P3: Chosen name: ~a\n" (vector-ref names 0))
)
  • source