p24 musings

main
Jacopo De Simoi 6 months ago
parent 77837dc8f8
commit 21f968db8d
  1. 98
      p24/p24.org

@ -1,50 +1,70 @@
#+title: Solution to p24 #+title: Solution to p24
#+begin_src emacs-lisp :results none #+begin_src emacs-lisp :results none
(require 'dash) (require 'dash)
(setq zzz (--map (intern (format "z%02d" it)) (-iterate #'1+ 0 100))) (org-babel-lob-ingest "../lib/advent-lib.org")
(-each zzz #'unintern) (setq zzz (--map (intern (format "z%02d" it)) (-iterate #'1+ 0 100)))
(with-temp-buffer (-each zzz #'unintern)
(insert-file-contents "input") (with-temp-buffer
(goto-char (point-min)) (insert-file-contents "input-test")
(advent/replace-multiple-regex-buffer (goto-char (point-min))
'(("^\\(.*\\):\\(.*\\)$" . "\\1 \\2") (advent/replace-multiple-regex-buffer
("^$" . ")\n (defun step ()\n (setq ") '(("^\\(.*\\):\\(.*\\)$" . "\\1 \\2")
("^\\(.*\\) \\(.*\\) \\(.*\\) -> \\(.*\\)$" . ("^$" . ")\n (defun step ()\n (setq ")
"\\4 (\\2 '\\1 '\\3)"))) ("^\\(.*\\) \\(.*\\) \\(.*\\) -> \\(.*\\)$" .
(insert "(setq ") "\\4 (\\2 '\\1 '\\3)")))
(goto-char (point-max)) (insert "(setq ")
(insert "))") (goto-char (point-max))
(write-file "tmp") (insert "))")
(eval-buffer)) (write-file "tmp")
(eval-buffer))
(defun good-vars (a b)
(and (boundp a) (eval a) (defun good-vars (a b)
(boundp b) (eval b))) (and (boundp a) (eval a)
(boundp b) (eval b)))
(defun XOR (a b)
(when (good-vars a b) (defun XOR (a b)
(logxor (eval a) (eval b)))) (when (good-vars a b)
(logxor (eval a) (eval b))))
(defun OR (a b)
(when (good-vars a b) (defun OR (a b)
(logior (eval a) (eval b)))) (when (good-vars a b)
(logior (eval a) (eval b))))
(defun AND (a b)
(when (good-vars a b) (defun AND (a b)
(logand (eval a) (eval b)))) (when (good-vars a b)
#+end_src (logand (eval a) (eval b))))
#+end_src
#+begin_src emacs-lisp #+begin_src emacs-lisp
(step) (step)
(setq
used-xxx (-filter #'boundp (--map (intern (format "x%02d" it)) (-iterate #'1+ 0 100)))
used-yyy (-filter #'boundp (--map (intern (format "y%02d" it)) (-iterate #'1+ 0 100)))
used-zzz (-filter #'boundp (--map (intern (format "z%02d" it)) (-iterate #'1+ 0 100))))
(setq used-zzz (-filter #'boundp (--map (intern (format "z%02d" it)) (-iterate #'1+ 0 100))))
(while (not (-every #'eval used-zzz)) (defun run ()
(step)) (while (not (-every #'eval used-zzz))
(step)))
(apply #'+ (--map-indexed (* (eval it) (ash 1 it-index)) used-zzz)) (defun reset-var (li)
(--each li (setq it nil)))
(defun set-var (li a)
(--each-indexed li (setq it (logand 1 (ash a (* -1 it-index)))))
)
(defun var-value (li)
(apply #'+ (--map-indexed (* (eval it) (ash 1 it-index)) li)))
(reset-var used-zzz)
(set-var used-xxx 0)
(set-var used-yyy 0)
(run)
(var-value used-zzz)
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 36035961805936 : 2024
(length used-zzz)

Loading…
Cancel
Save