day 21 part 1, fun

This commit is contained in:
efim 2022-12-25 10:43:57 +00:00
parent e062633074
commit c2acd2f75a
4 changed files with 3086 additions and 0 deletions

2979
day21-input.txt Normal file

File diff suppressed because it is too large Load Diff

57
day21-scratch.lisp Normal file
View File

@ -0,0 +1,57 @@
;; // 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)

15
day21-test.txt Normal file
View File

@ -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

35
day21.lisp Normal file
View File

@ -0,0 +1,35 @@
;; // 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)))