Skip to content

Instantly share code, notes, and snippets.

@sotolf2
Created January 7, 2020 13:59
Show Gist options
  • Save sotolf2/38b81c3ae17667103770fd0f55df830c to your computer and use it in GitHub Desktop.
Save sotolf2/38b81c3ae17667103770fd0f55df830c to your computer and use it in GitHub Desktop.
#lang racket
(require threading)
(define input "1321131112")
(define (parse str)
(~>> str
(string->list)
(map char->integer)
(map (curry + -48))))
(define (partition lst)
(define (part-rec lst cur acc res)
(if (empty? lst)
(reverse (cons acc res))
(if (equal? (car lst) cur)
(part-rec (cdr lst) cur (cons cur acc) res)
(part-rec lst (car lst) empty (if (empty? acc) res (cons acc res))))))
(part-rec lst #f empty empty))
(define (look-and-say lst)
(define (ls-rec grps res)
(if (empty? grps)
(reverse res)
(ls-rec (cdr grps) (cons (caar grps) (cons (length (car grps)) res)))))
(ls-rec (partition lst) empty))
(define (numlist->string lst)
(~>> lst
(map number->string)
(string-join _ "")))
(define (ls-times str tms)
(define (run lst tms cur)
(if (= cur tms)
lst
(run (look-and-say lst) tms (+ 1 cur))))
(~>
(parse str)
(run _ tms 0)
(numlist->string)))
(define (part1)
(~>
(ls-times input 40)
(string-length)))
(define (part2)
(~>
(ls-times input 50)
(string-length)))
(module+ test
(require rackunit)
(require rackunit/text-ui)
(define part-1
(test-suite
"Part 1"
(check-equal? 1 1 "basic")
(check-equal? (ls-times "1" 1) "11" "ls 1")
(check-equal? (ls-times "11" 1) "21" "ls 11")
(check-equal? (ls-times "21" 1) "1211" "ls 21")
(check-equal? (ls-times "1211" 1) "111221" "ls 1211")
(check-equal? (ls-times "111221" 1) "312211" "ls 111221")
(check-equal? (ls-times "1" 5) "312211" "ls * 5 1")
))
(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