Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created January 8, 2020 15:42
Show Gist options
  • Save sotolf2/d367feab5c0992c147cc1ef47004610b to your computer and use it in GitHub Desktop.
Save sotolf2/d367feab5c0992c147cc1ef47004610b to your computer and use it in GitHub Desktop.
#lang racket
(require threading)
(define input "cqjxjnds")
(define (has-increasing? str)
(define (inc-rec lst)
(cond
[(> 3 (length lst)) #f]
[(and (= 1 (- (third lst) (second lst)))
(= 1 (- (second lst) (first lst)))) #t]
[else (inc-rec (cdr lst))]))
(inc-rec
(~>> str
(string->list)
(map char->integer))))
(define (without-confusing? str)
(not (regexp-match? #px"[iol]" str)))
(define (has-two-pairs? str)
(define pairs (regexp-match* #px"(.)\\1" str))
(<= 2 (set-count (list->set pairs))))
(define (pass-valid? str)
(and (has-increasing? str)
(without-confusing? str)
(has-two-pairs? str)))
(define (next-pass str)
(define (next-rec lst res)
(define cur (+ 1 (car lst)))
(define wraps? (> cur (char->integer #\z)))
(define new (if wraps? (char->integer #\a) cur))
(if (not wraps?)
(append (reverse (cdr lst)) (cons new res))
(next-rec (cdr lst) (cons new res))))
(~>> (next-rec
(~>> str
(string->list)
(map char->integer)
(reverse))
empty)
(map integer->char)
(list->string)))
(define (next-valid str)
(define next (next-pass str))
(if (pass-valid? next)
next
(next-valid next)))
(define (part1)
(next-valid input))
(define (part2)
(next-valid
(next-valid input)))
(module+ test
(require rackunit)
(require rackunit/text-ui)
(define part-1
(test-suite
"Part 1"
(check-equal? 1 1 "basic")
))
(define part-2
(test-suite
"Part 2"
(check-equal? 1 1 "basic")
))
(run-tests part-1)
(run-tests part-2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment