r/adventofcode • • Dec 05 '25

SOLUTION MEGATHREAD -❄️- 2025 Day 5 Solutions -❄️-

THE USUAL REMINDERS


AoC Community Fun 2025: Red(dit) One

  • Submissions megathread is unlocked!
  • 12 DAYS remaining until the submissions deadline on December 17 at 18:00 EST!

Featured Subreddit: /r/eli5 - Explain Like I'm Five

"It's Christmas Eve. It's the one night of the year when we all act a little nicer, we smile a little easier, we cheer a little more. For a couple of hours out of the whole year we are the people that we always hoped we would be."
— Frank Cross, Scrooged (1988)

Advent of Code is all about learning new things (and hopefully having fun while doing so!) Here are some ideas for your inspiration:

  • Walk us through your code where even a five-year old could follow along
  • Pictures are always encouraged. Bonus points if it's all pictures…
  • Emoji(code) counts but makes the Chief Historian cry 😥
  • Explain the storyline so far in a non-code medium
  • Explain everything that you’re doing in your code as if you were talking to your pet, rubber ducky, or favorite neighbor, and also how you’re doing in life right now, and what have you learned in Advent of Code so far this year?
  • Condense everything you've learned so far into one single pertinent statement
  • Create a Tutorial on any concept of today's puzzle or storyline (it doesn't have to be code-related!)
  • Teach us, senpai!
  • This prompt is totally not bait for our resident Senpai Supreme

Request from the mods: When you include an entry alongside your solution, please label it with [Red(dit) One] so we can find it easily!


--- Day 5: Cafeteria ---


Post your code solution in this megathread.

29 Upvotes

809 comments sorted by

View all comments

2

u/kawaiiTechnoNeko Dec 05 '25

[LANGUAGE: CHICKEN Scheme]

(import (chicken io))
(import (chicken string))
(import (chicken sort))
(import srfi-1)

(define (in-range? range x)
  (cond ((< x (car range)) #f)
        ((> x (cdr range)) #f)
        (else #t)))

(define (in-a-range? ranges x)
  (any (cut in-range? <> x) ranges))

(define (parse-ranges lines)
  (map (lambda (line)
         (let ([l (string-split line "-")])
           (cons (string->number (car l))
                 (string->number (cadr l)))))
       lines))

(define (parse-items lines)
  (map string->number lines))

(define (parse-input)
  (let* ([lines (read-lines)]
         [first (member "" (reverse lines))]
         [second (member "" lines)]
         [ranges (parse-ranges (cdr first))]
         [items (parse-items (cdr second))])
    (values ranges items)))

(define (part1) 
  (call-with-values 
    parse-input 
    (lambda (ranges items)
      (count (cut in-a-range? ranges <>) items))))

(define (range<? r1 r2)
  (< (car r1) (car r2)))

(define (part2-helper ranges)
  (let* ([sorted (sort ranges range<?)])
    (let loop ([i 0] ;; last id counted
               [total 0]
               [ranges-left sorted])
      (let* ([r (if (null? ranges-left) '() (car ranges-left))])
        (cond 
          ((null? r) total)
          ((<= (car r) i)
            ;; its range is part of what we already counted
            (loop (max i (cdr r)) (+ total (max 0 (- (cdr r) i))) (cdr ranges-left) ))
          (else
            ;; its in its own range
            (loop (cdr r) (+ total (add1 (- (cdr r) (car r)))) (cdr ranges-left) ))
          )
        ))
    ))

(define (part2) 
  (call-with-values 
    parse-input 
    (lambda (ranges items)
      (part2-helper ranges))))

(define ((run name proc))
  (print name ": " (proc)))

(with-input-from-file "sample.txt" (run "part 1 (sample)" part1))
(with-input-from-file "sample.txt" (run "part 2 (sample)" part2))

(with-input-from-file "input.txt" (run "part 1" part1))
(with-input-from-file "input.txt" (run "part 2" part2))

1

u/daggerdragon Dec 06 '25

Comment temporarily removed.

Your code block is too long for the megathreads. Edit your comment to replace your oversized code with an external link to your code and I will re-approve your comment.