Skip to content

Instantly share code, notes, and snippets.

@qookei
Created December 6, 2023 15:01
Show Gist options
  • Save qookei/3dd9330043b1229f93b9fe63c96581d2 to your computer and use it in GitHub Desktop.
Save qookei/3dd9330043b1229f93b9fe63c96581d2 to your computer and use it in GitHub Desktop.
(use-modules (srfi srfi-1) (ice-9 textual-ports) (ice-9 format) (ice-9 pretty-print))
(define (read-input)
(let ([line (get-line (current-input-port))])
(if (eof-object? line)
'()
(cons (map string->number (string-tokenize line char-set:digit))
(read-input)))))
(define (count-wins race-pair)
(fold + 0 (map (λ (time)
(if (>= (* time (- (car race-pair) time))
(cadr race-pair))
1 0))
(iota (1- (car race-pair)) 1))))
(define (part1 input)
(fold * 1 (map count-wins (zip (car input)
(cadr input)))))
(define (%combine-numbers lst)
(string->number (string-concatenate (map number->string lst))))
(define (part2 input)
(count-wins (list (%combine-numbers (car input))
(%combine-numbers (cadr input)))))
(let* ([input (read-input)])
(format #t "Part 1: ~a~%" (part1 input))
(format #t "Part 2: ~a~%" (part2 input)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment