day 21, part 2 backward computation. that's hot
This commit is contained in:
parent
f6799dd691
commit
3abfbb53ca
|
@ -53,11 +53,11 @@
|
||||||
|
|
||||||
(eval root)
|
(eval root)
|
||||||
|
|
||||||
(load-file-defs "day21-test.txt")
|
;; (load-file-defs "day21-test.txt")
|
||||||
(eval root)
|
;; (eval root)
|
||||||
|
|
||||||
(load-file-defs "day21-input.txt")
|
;; (load-file-defs "day21-input.txt")
|
||||||
(print (eval root))
|
;; (print (eval root))
|
||||||
|
|
||||||
;; now. things are different.
|
;; now. things are different.
|
||||||
;; HUMN is my input
|
;; HUMN is my input
|
||||||
|
@ -80,3 +80,56 @@
|
||||||
;; and then what?
|
;; and then what?
|
||||||
;;
|
;;
|
||||||
;; let's skip for now?
|
;; let's skip for now?
|
||||||
|
|
||||||
|
;; or, alternatively. i could save HUMN num disregard
|
||||||
|
;; defparameter with HUMN to be used as HUMN =
|
||||||
|
;; and then what? root
|
||||||
|
;; but then i'd need to reverse all the other operations, hm
|
||||||
|
;; or, maybe i could join into flat expression?
|
||||||
|
;; and then what? i macroexpand?
|
||||||
|
;;
|
||||||
|
;; now. i had an idea. i could have additional symbols,
|
||||||
|
;; also pointing to quoted computations
|
||||||
|
;; but these would be `back-ptdq`
|
||||||
|
;; for line 'lgvd: ljgn * ptdq'
|
||||||
|
;; make two? for each
|
||||||
|
;;
|
||||||
|
;; ptdq: humn - dvpt
|
||||||
|
;; back-humn = ptdq + dvpt
|
||||||
|
;; but one of these wouldn't be possible to forward eval, so it would be back-ptdq + dvpt
|
||||||
|
;; and on
|
||||||
|
;; root: pppw + sjmn
|
||||||
|
;; two would be created back-pppw = - eval sjmn and vice versa
|
||||||
|
;; this should work?
|
||||||
|
|
||||||
|
;; sure, let's try?
|
||||||
|
;; so we'd do what?
|
||||||
|
;; same for forward eval?
|
||||||
|
;; and two for back-monkey
|
||||||
|
|
||||||
|
(back-symbol 'hello)
|
||||||
|
|
||||||
|
|
||||||
|
(intern (format nil "BACK-~a" 'yo))
|
||||||
|
;; and i need reverse of the operands?
|
||||||
|
|
||||||
|
(reverse-operation '+)
|
||||||
|
(line-to-quoted-operation-2 "dbpl: 5")
|
||||||
|
(line-to-quoted-operation-2 "pppw: cczh / lfqf")
|
||||||
|
(line-to-quoted-operation-2 "lgvd: ljgn * ptdq")
|
||||||
|
(line-to-quoted-operation-2 "drzm: hmdt - zczc")
|
||||||
|
(line-to-quoted-operation-2 "cczh: sllz + lgvd")
|
||||||
|
(line-to-quoted-operation-2 "root: pppw + sjmn")
|
||||||
|
|
||||||
|
|
||||||
|
;; i think it's done? let's try to eval all of this?
|
||||||
|
;; but i also need root back-computation.
|
||||||
|
;; for each operant that they are equal to forward calculation of another
|
||||||
|
|
||||||
|
;; (load-file-defs-2 "day21-test.txt")
|
||||||
|
;; (eval root)
|
||||||
|
;; (eval back-humn) ; 301. cool
|
||||||
|
|
||||||
|
;; (load-file-defs-2 "day21-input.txt")
|
||||||
|
;; (print (eval back-humn))
|
||||||
|
;; 3715799488132
|
||||||
|
|
63
day21.lisp
63
day21.lisp
|
@ -33,3 +33,66 @@
|
||||||
for line in (uiop:read-file-lines filename)
|
for line in (uiop:read-file-lines filename)
|
||||||
for definition = (line-to-quoted-operation line)
|
for definition = (line-to-quoted-operation line)
|
||||||
do (eval definition)))
|
do (eval definition)))
|
||||||
|
|
||||||
|
(defun reverse-operation (op)
|
||||||
|
(case op
|
||||||
|
('* '/)
|
||||||
|
('/ '*)
|
||||||
|
('+ '-)
|
||||||
|
('- '+)))
|
||||||
|
|
||||||
|
(defun back-symbol (symb)
|
||||||
|
(intern (format nil "BACK-~a" symb)))
|
||||||
|
|
||||||
|
;; adding BACK-symbol computations.
|
||||||
|
(defun line-to-quoted-operation-2 (line)
|
||||||
|
(let* ((words (ppcre:split " " (ppcre:regex-replace ":" line "")))
|
||||||
|
(symbols (mapcar #'parse-integer-or-symbol words)))
|
||||||
|
(cond
|
||||||
|
((= 4 (length symbols))
|
||||||
|
;; with operation
|
||||||
|
(destructuring-bind (name operand1 op operand2)
|
||||||
|
symbols
|
||||||
|
(if (eq name 'ROOT)
|
||||||
|
`(progn (defparameter ,(back-symbol operand1) '(eval ,operand2))
|
||||||
|
(defparameter ,(back-symbol operand2) '(eval ,operand1)))
|
||||||
|
(let ((forward-calc
|
||||||
|
`(defparameter ,name '(,op (eval ,operand1) (eval ,operand2))))
|
||||||
|
(backward-calc
|
||||||
|
(case op
|
||||||
|
('+ `((defparameter
|
||||||
|
,(back-symbol operand1)
|
||||||
|
'(- (eval ,(back-symbol name)) (eval ,operand2)))
|
||||||
|
(defparameter
|
||||||
|
,(back-symbol operand2)
|
||||||
|
'(- (eval ,(back-symbol name)) (eval ,operand1)))))
|
||||||
|
('- `((defparameter
|
||||||
|
,(back-symbol operand1)
|
||||||
|
'(+ (eval ,(back-symbol name)) (eval ,operand2)))
|
||||||
|
(defparameter
|
||||||
|
,(back-symbol operand2)
|
||||||
|
'(- (eval ,operand1) (eval ,(back-symbol name))))))
|
||||||
|
(t `((defparameter
|
||||||
|
,(back-symbol operand1)
|
||||||
|
'(,(reverse-operation op) (eval ,(back-symbol name)) (eval ,operand2)))
|
||||||
|
(defparameter
|
||||||
|
,(back-symbol operand2)
|
||||||
|
'(,(reverse-operation op) (eval ,(back-symbol name)) (eval ,operand1)))))
|
||||||
|
)))
|
||||||
|
`(progn
|
||||||
|
,forward-calc
|
||||||
|
,@backward-calc
|
||||||
|
;; (defparameter ,(innern (format t "BACK~a" name) ))
|
||||||
|
)))))
|
||||||
|
|
||||||
|
((= 2 (length symbols))
|
||||||
|
;; just number
|
||||||
|
(destructuring-bind (name value)
|
||||||
|
symbols
|
||||||
|
`(defparameter ,name ,value))))))
|
||||||
|
|
||||||
|
(defun load-file-defs-2 (filename)
|
||||||
|
(loop
|
||||||
|
for line in (uiop:read-file-lines filename)
|
||||||
|
for definitions = (line-to-quoted-operation-2 line)
|
||||||
|
do (eval definitions)))
|
||||||
|
|
Loading…
Reference in New Issue