63 lines
1.7 KiB
Scheme
63 lines
1.7 KiB
Scheme
(define (load-instructions filename)
|
|
(call-with-input-file
|
|
filename
|
|
(lambda (file)
|
|
(let loop [(instructions '())]
|
|
(let [(instruction (get-datum file))]
|
|
(cond
|
|
[(eof-object? instruction)
|
|
(reverse instructions)]
|
|
[(symbol=? instruction 'inp)
|
|
(loop (cons (list instruction
|
|
(get-datum file)) instructions))]
|
|
[else
|
|
(loop (cons (list instruction
|
|
(get-datum file)
|
|
(get-datum file)) instructions))]))))))
|
|
|
|
(define (digits-from-number n)
|
|
(let loop [(n n) (digits '())]
|
|
(if (= n 0)
|
|
digits
|
|
(loop (div n 10) (cons (mod n 10) digits)))))
|
|
|
|
(define (interpret-with-input instructions input)
|
|
(let [(enviro '((x 0) (y 0) (z 0) (w 0)
|
|
(inp (lambda (n)
|
|
(let [(digit (car input))]
|
|
(set! input (cdr input))
|
|
digit)))
|
|
(add +)
|
|
(mul *)
|
|
(eql (lambda (a b) (if (= a b) 1 0)))))
|
|
(input (digits-from-number input))]
|
|
(eval (list 'let*
|
|
; base environment with input
|
|
(cons (list 'input (cons 'list input)) enviro)
|
|
; code adapted to be able to modify environment
|
|
(cons 'begin (map
|
|
(lambda (i)
|
|
(list 'set! (cadr i) i))
|
|
instructions))
|
|
; return value
|
|
'(list x y z w)))))
|
|
|
|
(define (trace instructions input)
|
|
(let loop [(already '())
|
|
(instructions instructions)]
|
|
(unless (null? instructions)
|
|
(printf "~20a: ~a~%"
|
|
(car instructions)
|
|
(interpret-with-input (cons (car instructions)
|
|
(reverse already))
|
|
input))
|
|
(loop (cons (car instructions) already)
|
|
(cdr instructions)))))
|
|
|
|
(define (biggest-valid-MONAD instructions)
|
|
0) ; no idea what x, y, z and w mean...
|
|
|
|
(let [(instructions (load-instructions "input"))]
|
|
(printf "part 1: ~% Biggest valid MONAD: ~a~%"
|
|
(biggest-valid-MONAD instructions)))
|