Quest 3: The Deepest Fit

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

Also, don't wait for me to make these posts, feel free to post yourself :)

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

Scheme/Guile

Guile doesn't seem to come with a bag implementation :(. Not a big deal, a linear scan works about as well.

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

(define (parse-file file-name)
  (let* ((p (open-input-file file-name))
        (comma-split (string-split (read-line p) #\,))
        (number-list (map string->number comma-split)))
    number-list))

(let* ((crates (parse-file "notes/everybody_codes_e2025_q03_p1.txt"))
       (dedup-crates (delete-duplicates crates)))
  (format #t "P1 Answer: ~a\n\n" (apply + dedup-crates)))


(let* ((crates (parse-file "notes/everybody_codes_e2025_q03_p2.txt"))
       (dedup-crates (delete-duplicates crates))
       (sorted-crates (sort dedup-crates <)))
  (format #t "P2 Answer: ~a\n\n" (apply + (take sorted-crates 20))))


(let* ((crates (parse-file "notes/everybody_codes_e2025_q03_p3.txt"))
       (sorted-crates (sort crates <))
       (largest-set-size (let loop ((count 0) (l sorted-crates) (c #f) (max-count 0))
         (if (nil? l)
             max-count
             (let* ((new-c (car l))
                    (new-count (if (equal? new-c c) (+ count 1) 1)))
               (loop new-count (cdr l) new-c (max new-count max-count)))))))
  (format #t "P3 Answer: ~a\n\n" largest-set-size))
  • source
  • hideshow 2 child comments
  • I'm so far behind on these, but it's rare that I see a lanugage I've never heard before. I've heard of scheme. How did you come to using this language for the challenges?

  • source
  • parent
  • hideshow 1 child comment
  • [โ€“] 2 points 11 months ago

    I wanted to learn scheme and perhaps work through SICP. Guille seemed like a reasonable scheme to choose because I've also been considering learning Guix. Guix is a package manager similar to Nix, and it uses Guille as its configuration language.

  • source
  • parent