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/2018/02.rkt

37 lines
968 B
Racket

6 years ago
#lang debug br
(define fp (open-input-file "02.txt"))
(define ids
(map string-trim (port->lines fp)))
(define ((cluster len) chars)
(for/or ([group (in-list (group-by values chars char=?))])
(= len (length group))))
(define ()
(define charss (map string->list ids))
(define twos (length (filter (cluster 2) charss)))
(define threes (length (filter (cluster 3) charss)))
(* twos threes))
(define (differ-by-one? id id2)
(= (length (common-chars id id2)) (sub1 (string-length id))))
(define (common-chars id id2)
(for/list ([c (in-string id)]
[c2 (in-string id2)]
#:when (char=? c c2))
c))
(define (★★)
(list->string
(for*/first ([id (in-list ids)]
[id2 (in-list (cdr ids))]
#:when (differ-by-one? id id2))
(common-chars id id2))))
6 years ago
(module+ test
6 years ago
(require rackunit)
(check-equal? (time ()) 6696)
(check-equal? (time (★★)) "bvnfawcnyoeyudzrpgslimtkj"))