Skip to content

Instantly share code, notes, and snippets.

@qookei
Last active December 9, 2023 10:52
Show Gist options
  • Save qookei/f31aa0d2786b77136957502c10390c82 to your computer and use it in GitHub Desktop.
Save qookei/f31aa0d2786b77136957502c10390c82 to your computer and use it in GitHub Desktop.
(use-modules (ice-9 textual-ports) (srfi srfi-1) (srfi srfi-26) (ice-9 format))
(define (read-input)
(let ([line (get-line (current-input-port))])
(if (eof-object? line)
'()
(cons (map string->number (string-split line #\space))
(read-input)))))
(define (list-of-differences lst)
(map (cut apply - <>) (zip (cdr lst) lst)))
(define (%part-common op getter lst)
(if (eqv? (length (delete-duplicates lst)) 1)
(car lst)
(op (getter lst)
(%part-common op getter (list-of-differences lst)))))
(define (part-common op getter input)
(apply + (map (cut %part-common op getter <>) input)))
(let ([input (read-input)])
(format #t "Part 1: ~a~%" (part-common + last input))
(format #t "Part 2: ~a~%" (part-common - first input)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment