; three in a row
(define (unique eql? xs)
(cond ((null? xs) '())
((null? (cdr xs)) xs)
((eql? (car xs) (cadr xs))
(unique eql? (cdr xs)))
(else (cons (car xs) (unique eql? (cdr xs))))))
(define (julian year month day)
(let* ((a (quotient (- 14 month) 12))
(y (+ year 4800 (- a)))
(m (+ month (* 12 a) -3)))
(+ day
(quotient (+ (* 153 m) 2) 5)
(* 365 y)
(quotient y 4)
(- (quotient y 100))
(quotient y 400)
(- 32045))))
(define sample-input '(
("01/11/2018" . "0003")
("01/12/2018" . "0003")
("01/13/2018" . "0004")
("01/13/2018" . "0003")))
(define (first-three-in-a-row xs)
(and (pair? xs) (pair? (cdr xs)) (pair? (cddr xs))
(= (+ (car xs) 1) (cadr xs))
(= (+ (car xs) 2) (caddr xs))))
(define (cdrs xs)
(do ((ys xs (cdr ys)) (yss (list) (cons (cdr ys) yss)))
((null? ys) (cons xs (reverse yss)))))
(define (signature x)
(+ (* (string->number (cdr x)) 10000000)
(julian (string->number (substring (car x) 6 10))
(string->number (substring (car x) 0 2))
(string->number (substring (car x) 3 5)))))
(define (three-in-a-row xs)
(unique = (map (lambda (n) (quotient n 10000000))
(map car (filter first-three-in-a-row
(cdrs (unique = (sort (map signature xs) <))))))))
(display (three-in-a-row sample-input)) (newline)