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

  • vole@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    20 天前

    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))
    
      • vole@lemmy.world
        link
        fedilink
        English
        arrow-up
        1
        ·
        5 小时前

        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.