Skip to content

Instantly share code, notes, and snippets.

@fabienhinault
Last active November 26, 2017 19:08
Show Gist options
  • Save fabienhinault/84a2f1ddc54f2f233a212d4d8231d401 to your computer and use it in GitHub Desktop.
Save fabienhinault/84a2f1ddc54f2f233a212d4d8231d401 to your computer and use it in GitHub Desktop.
#lang racket
(define-syntax-rule (let1 a b body ...)
(let ((a b)) body ...))
(require rackunit)
; convert a number n to the list of boolean of its binary writing
(define (number->bools n size)
(define (iter n size result)
(if (eq? 0 size)
result
(iter (quotient n 2) (- size 1) (cons (odd? n) result))))
(iter n size '()))
(check-equal? (number->bools 1 3) '(#f #f #t))
; convert a number n to the a-list of its automate. To each input of size mask-size, its boolean result.
; mask-size is likely to be 3.
(define (number->table n mask-size)
(let1 index-size (expt 2 mask-size)
(define (iter bools i-input mask-size result)
(if (eq? -1 i-input)
result
(iter bools (- i-input 1) mask-size
(cons (cons (number->bools i-input mask-size)
(list-ref bools (- index-size 1 i-input)))
result))))
(iter (number->bools n index-size) (- index-size 1) mask-size '())))
(check-equal?
(number->table 0 3)
'(((#f #f #f) . #f)
((#f #f #t) . #f)
((#f #t #f) . #f)
((#f #t #t) . #f)
((#t #f #f) . #f)
((#t #f #t) . #f)
((#t #t #f) . #f)
((#t #t #t) . #f)))
(check-equal?
(number->table 1 3)
'(((#f #f #f) . #t)
((#f #f #t) . #f)
((#f #t #f) . #f)
((#f #t #t) . #f)
((#t #f #f) . #f)
((#t #f #t) . #f)
((#t #t #f) . #f)
((#t #t #t) . #f)))
(check-equal?
(number->table 3 3)
'(((#f #f #f) . #t)
((#f #f #t) . #t)
((#f #t #f) . #f)
((#f #t #t) . #f)
((#t #f #f) . #f)
((#t #f #t) . #f)
((#t #t #f) . #f)
((#t #t #t) . #f)))
(check-equal?
(number->table 255 3)
'(((#f #f #f) . #t)
((#f #f #t) . #t)
((#f #t #f) . #t)
((#f #t #t) . #t)
((#t #f #f) . #t)
((#t #f #t) . #t)
((#t #t #f) . #t)
((#t #t #t) . #t)))
(check-equal?
(number->table 254 3)
'(((#f #f #f) . #f)
((#f #f #t) . #t)
((#f #t #f) . #t)
((#f #t #t) . #t)
((#t #f #f) . #t)
((#t #f #t) . #t)
((#t #t #f) . #t)
((#t #t #t) . #t)))
(define (all-n-sublists l n)
(if (< (length l) n)
'()
(cons (take l n) (all-n-sublists (cdr l) n))))
(check-equal?
(all-n-sublists
'(#f #f #f #t #f #t #t #t #f #f) 3)
'((#f #f #f) (#f #f #t) (#f #t #f) (#t #f #t) (#f #t #t) (#t #t #t) (#t #t #f) (#t #f #f)))
(define (apply-table table l default offset)
(let ((res-default (cdr (assoc (make-list 3 default) table)))
(result (map
(λ (_) (cdr (assoc _ table)))
(all-n-sublists (append (list default default) l (list default default))
(length (caar table)))))
(res-offset (+ offset 1)))
(when (eq? res-default (car result))
(set! result (cdr result))
(set! res-offset offset))
(when (eq? res-default (last result))
(set! result (drop-right result 1)))
(list res-offset res-default result)))
(check-equal?
(apply-table
'(((#f #f #f) . #f)
((#f #f #t) . #t)
((#f #t #f) . #t)
((#f #t #t) . #t)
((#t #f #f) . #t)
((#t #f #t) . #t)
((#t #t #f) . #t)
((#t #t #t) . #t))
'(#f #f #f #t #f #t #t #t #f #f) #f 0)
'(0 #f (#f #f #t #t #t #t #t #t #t #f)))
(check-equal?
(apply-table
'(((#f #f #f) . #f)
((#f #f #t) . #t)
((#f #t #f) . #f)
((#f #t #t) . #t)
((#t #f #f) . #f)
((#t #f #t) . #t)
((#t #t #f) . #f)
((#t #t #t) . #t))
'(#f #f #f #t #f #t #t #t #f #f) #f 0)
'(0 #f (#f #f #t #f #t #t #t #f #f #f)))
(define (print-line booleans offset)
(displayln (substring (booleans->string booleans) offset)))
(define (booleans->string booleans)
(list->string
(map
(λ (_) (cond ((eq? _ #f) #\space) ((eq? _ #t) #\u2588)))
booleans)))
(check-equal?
(booleans->string '(#f #f #t #f #t #t #t #f #f #f))
" █ ███ ")
(define (automate n-automate start-list default n-iterations)
(let1 mate (number->table n-automate 3)
(define (iter mate start-list default n-iterations offset)
(when (> n-iterations 0)
(print-line start-list offset)
(let* ((result (apply-table mate start-list default offset))
(res-offset (car result))
(res-default (cadr result))
(res-list (caddr result)))
(iter mate res-list res-default
(- n-iterations 1)
res-offset))))
(iter mate start-list default n-iterations 0)))
@fabienhinault
Copy link
Author

  • offset

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment