Compare commits
3 Commits
e062633074
...
3abfbb53ca
Author | SHA1 | Date |
---|---|---|
|
3abfbb53ca | |
|
f6799dd691 | |
|
c2acd2f75a |
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,135 @@
|
|||
;; // https://adventofcode.com/2022/day/21
|
||||
;; well, this seems like this could be done with (eval)
|
||||
;; would it be possible for me to also translate
|
||||
;; xxx + yyy into (call xxx) + (call yyy)
|
||||
;; that would be neat
|
||||
|
||||
(in-package :day-21)
|
||||
|
||||
;; so. have mashmap from symbol name into operation
|
||||
;; and have (my-eval)?
|
||||
;; it will take symbol name, and either return number directly from the map
|
||||
;; or will wrap the names into my-eval and run operation on them?
|
||||
|
||||
;; is there a way to have less code and reuse existing lisp things?
|
||||
;; if there's direct number, i could just run (defparameter lfqf 4)
|
||||
;; and then execute root directly, i think
|
||||
;; or what, i'd need to also define all the intermediate symbols,
|
||||
;; (setq a (+ c d)) ; complains that C is unbound
|
||||
;; how could this be a lazy eval?
|
||||
;; well. hm
|
||||
;; i could define all of these as macroses?
|
||||
(defmacro *test-monkey-hmdt* () 32)
|
||||
(+ 3 (*test-monkey-hmdt*))
|
||||
|
||||
;; yup, i guess
|
||||
|
||||
(defparameter *test-monkey-sllz* 4)
|
||||
(eval *test-monkey-sllz*)
|
||||
(defparameter *test-monkey-ljgn* 2)
|
||||
(defparameter *my-operation* '(+ *test-monkey-sllz* *test-monkey-ljgn*))
|
||||
*my-operation*
|
||||
(eval *my-operation*) ; well, that works ok
|
||||
;; so, read all of these things as parameters.
|
||||
|
||||
;; "root: pppw + sjmn "
|
||||
|
||||
(mapcar #'intern (ppcre:split " " "root: pppw + sjmn "))
|
||||
(mapcar #'parse-integer-or-symbol (ppcre:split " " "root: 44"))
|
||||
|
||||
(ppcre:regex-replace ":" "root: pppw + sjmn " "")
|
||||
|
||||
(line-to-quoted-operation "root: pppw + sjmn")
|
||||
(eval (line-to-quoted-operation "dbpl: 5"))
|
||||
|
||||
;; well, i'd want to put quoted expression into parameter?
|
||||
;; and now i can remove quoting of the defparameter?
|
||||
;; and actually define all of the parameters?
|
||||
|
||||
(loop
|
||||
for line in (uiop:read-file-lines "day21-test.txt")
|
||||
for definition = (line-to-quoted-operation line)
|
||||
do (eval definition))
|
||||
|
||||
(eval root)
|
||||
|
||||
;; (load-file-defs "day21-test.txt")
|
||||
;; (eval root)
|
||||
|
||||
;; (load-file-defs "day21-input.txt")
|
||||
;; (print (eval root))
|
||||
|
||||
;; now. things are different.
|
||||
;; HUMN is my input
|
||||
;; and ROOT is "comparison of two numbers"
|
||||
;; and change in root can influence results in a significant way
|
||||
;; so, how'd i do that?
|
||||
;;
|
||||
;; what could be done then?
|
||||
;; i could potentially somehow modify the operations?
|
||||
;; so that (eval humn) would return
|
||||
;;
|
||||
;; root: pppw + sjmn
|
||||
;; so. root should mean
|
||||
;; pppw == sjmn
|
||||
;; don't know which has HUMN yet.
|
||||
;; should i then just eval those that have HUMN into a operation?
|
||||
;; and others into a number?
|
||||
;; well, i suppose i'd need to do pass down somehow
|
||||
;; eval both parts of the root.
|
||||
;; 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
|
|
@ -0,0 +1,15 @@
|
|||
root: pppw + sjmn
|
||||
dbpl: 5
|
||||
cczh: sllz + lgvd
|
||||
zczc: 2
|
||||
ptdq: humn - dvpt
|
||||
dvpt: 3
|
||||
lfqf: 4
|
||||
humn: 5
|
||||
ljgn: 2
|
||||
sjmn: drzm * dbpl
|
||||
sllz: 4
|
||||
pppw: cczh / lfqf
|
||||
lgvd: ljgn * ptdq
|
||||
drzm: hmdt - zczc
|
||||
hmdt: 32
|
|
@ -0,0 +1,98 @@
|
|||
;; // https://adventofcode.com/2022/day/21
|
||||
(defpackage :day-21
|
||||
(:use :cl))
|
||||
(in-package :day-21)
|
||||
|
||||
(ql:quickload 'cl-ppcre)
|
||||
|
||||
(defun parse-integer-or-symbol (str)
|
||||
(let ((maybe-int (parse-integer str :junk-allowed t)))
|
||||
(if maybe-int
|
||||
maybe-int
|
||||
(intern (string-upcase str)))))
|
||||
|
||||
|
||||
(defun line-to-quoted-operation (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
|
||||
`(defparameter ,name '(,op (eval ,operand1) (eval ,operand2)))))
|
||||
|
||||
((= 2 (length symbols))
|
||||
;; just number
|
||||
(destructuring-bind (name value)
|
||||
symbols
|
||||
`(defparameter ,name ,value))))))
|
||||
|
||||
(defun load-file-defs (filename)
|
||||
(loop
|
||||
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)))
|
Loading…
Reference in New Issue