• Home
  • ·
  • Artwork
  • ·
  • Notes
  • ·
  • Projects
  • Monochrome etching by Alphonse Legros showing a rustic thatched-roof shed surrounded by trees, with a lone figure bending on a path in the foreground and a village in the distance

    The Little Shed (Le Petit Hangar) (c. 1857-1911) by Alphone Legros. Public Domain.

    Day 1: Dusty Shed

    Dec 1, 2026


    This page is still a draft

    #!/usr/bin/env -S guile -s
    !#
    
    (use-modules (ice-9 rdelim)
                 (srfi srfi-1)
                 (srfi srfi-13))
    
    ;;; Read input
    
    (define (parse-num line)
      (let ((trimmed (string-trim-both line)))
        (if (string-null? trimmed)
            #f
            (let ((num (string->number trimmed)))
              (and num (integer? num) num)))))
    
    (define (read-file path)
      (call-with-input-file path
        (lambda (port)
          (let loop ((line (read-line port))
                     (numbers '()))
            (if (eof-object? line)
                numbers
                (let ((num (parse-num line)))
                  (if num
                      (loop (read-line port) (cons num numbers))
                      (loop (read-line port) numbers))))))))
    
    ;;; Solution
    
    (define (group-by-count lst)
      (let ((counts (make-hash-table)))
        (let loop ((items lst))
          (unless (null? items)
            (let ((n (car items)))
              (hashv-set! counts n (1+ (hashv-ref counts n 0))))
            (loop (cdr items))))
        (let ((pairs (hash-fold (lambda (k v acc)
                                  (cons (cons k v) acc))
                                '()
                                counts)))
          (sort pairs (lambda (a b) (> (cdr a) (cdr b)))))))
    
    (define (get-product lst n)
      (let loop ((items lst) (i 0) (product 1))
        (if (or (null? items) (>= i n))
            product
            (let ((num (car (car items))))
              (loop (cdr items)
                    (1+ i)
                    (* product num))))))
    
    (define arr (group-by-count (read-file "input.txt")))
    
    (format #t "Top 3:  ~a~%" (get-product arr 3))
    (format #t "Last 3: ~a~%" (get-product (reverse arr) 3))