From 3abfbb53ca31c4918a713930133e83d68fe41479 Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 25 Dec 2022 14:54:34 +0000 Subject: [PATCH] day 21, part 2 backward computation. that's hot --- day21-scratch.lisp | 61 +++++++++++++++++++++++++++++++++++++++++--- day21.lisp | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 4 deletions(-) diff --git a/day21-scratch.lisp b/day21-scratch.lisp index 2758f97..05bc6ba 100644 --- a/day21-scratch.lisp +++ b/day21-scratch.lisp @@ -53,11 +53,11 @@ (eval root) -(load-file-defs "day21-test.txt") -(eval root) +;; (load-file-defs "day21-test.txt") +;; (eval root) -(load-file-defs "day21-input.txt") -(print (eval root)) +;; (load-file-defs "day21-input.txt") +;; (print (eval root)) ;; now. things are different. ;; HUMN is my input @@ -80,3 +80,56 @@ ;; and then what? ;; ;; 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 diff --git a/day21.lisp b/day21.lisp index f653d35..96864a0 100644 --- a/day21.lisp +++ b/day21.lisp @@ -33,3 +33,66 @@ for line in (uiop:read-file-lines filename) for definition = (line-to-quoted-operation line) 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)))