You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
aoc-racket/2019/04.rkt

44 lines
1.2 KiB
Racket

5 years ago
#lang br
(require racket/file rackunit)
(match-define (list low high)
(map string->number (string-split (file->string "04.rktd") "-")))
5 years ago
(define (find-doubles digits)
(match (for/list ([d0 (in-list digits)]
[d1 (in-list (cdr digits))]
#:when (= d0 d1))
d0)
5 years ago
[(? null?) #false]
5 years ago
[dds (remove-duplicates dds)]))
5 years ago
(define (num->digits num)
(for/list ([c (in-string (number->string num))])
5 years ago
(string->number (string c))))
5 years ago
5 years ago
(define (increasing? digits) (apply <= digits))
5 years ago
;; 1
(check-eq?
5 years ago
(for/sum ([digits (in-list (map num->digits (range low (add1 high))))])
(match digits
[(and (? find-doubles) (? increasing?)) 1]
[_ 0]))
5 years ago
1079)
5 years ago
(define (triple-digit? ds d)
(and (pair? ds)
(or (list-prefix? (list d d d) ds)
(triple-digit? (cdr ds) d))))
5 years ago
5 years ago
;; 2
5 years ago
(check-eq?
5 years ago
(for/sum ([digits (in-list (map num->digits (range low (add1 high))))])
(match digits
[(and (app find-doubles (? list? doubled-digits)) (? increasing?))
#:when (for/or ([dd (in-list doubled-digits)])
(not (triple-digit? digits dd))) 1]
[_ 0]))
5 years ago
699)