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
#+begin_src emacs-lisp :results none
(require 'dash)
(setq zzz (--map (intern (format "z%02d" it)) (-iterate #'1+ 0 100)))
(-each zzz #'unintern)
(with-temp-buffer
(insert-file-contents "input")
(goto-char (point-min))
(advent/replace-multiple-regex-buffer
'(("^\\(.*\\):\\(.*\\)$" . "\\1 \\2")
("^$" . ")\n (defun step ()\n (setq ")
("^\\(.*\\) \\(.*\\) \\(.*\\) -> \\(.*\\)$" .
"\\4 (\\2 '\\1 '\\3)")))
(insert "(setq ")
(goto-char (point-max))
(insert "))")
(write-file "tmp")
(eval-buffer))
(defun good-vars (a b)
(and (boundp a) (eval a)
(boundp b) (eval b)))
(defun XOR (a b)
(when (good-vars a b)
(logxor (eval a) (eval b))))
(defun OR (a b)
(when (good-vars a b)
(logior (eval a) (eval b))))
(defun AND (a b)
(when (good-vars a b)
(logand (eval a) (eval b))))
#+end_src
(require 'dash)
(org-babel-lob-ingest "../lib/advent-lib.org")
(setq zzz (--map (intern (format "z%02d" it)) (-iterate #'1+ 0 100)))
(-each zzz #'unintern)
(with-temp-buffer
(insert-file-contents "input-test")
(goto-char (point-min))
(advent/replace-multiple-regex-buffer
'(("^\\(.*\\):\\(.*\\)$" . "\\1 \\2")
("^$" . ")\n (defun step ()\n (setq ")
("^\\(.*\\) \\(.*\\) \\(.*\\) -> \\(.*\\)$" .
"\\4 (\\2 '\\1 '\\3)")))
(insert "(setq ")
(goto-char (point-max))
(insert "))")
(write-file "tmp")
(eval-buffer))
(defun good-vars (a b)
(and (boundp a) (eval a)
(boundp b) (eval b)))
(defun XOR (a b)
(when (good-vars a b)
(logxor (eval a) (eval b))))
(defun OR (a b)
(when (good-vars a b)
(logior (eval a) (eval b))))
(defun AND (a b)
(when (good-vars a b)
(logand (eval a) (eval b))))
#+end_src
#+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))
(step))
(defun run ()
(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
#+RESULTS:
: 36035961805936
: 2024
(length used-zzz)

Loading…
Cancel
Save