Cleanup solution to p14

main
Jacopo De Simoi 11 months ago
parent c7204bf0b4
commit c50dd707ae
  1. 32
      p14/p14.org

@ -1,6 +1,7 @@
#+title: Solution to p14 #+title: Solution to p14
First parse with regex to make into a list
#+begin_src emacs-lisp First parse with a regex to make into a list
#+begin_src emacs-lisp :results none
(require 'dash) (require 'dash)
(with-temp-buffer (with-temp-buffer
(insert-file-contents "input-test") (insert-file-contents "input-test")
@ -11,35 +12,32 @@ First parse with regex to make into a list
(insert "(setq data '(") (insert "(setq data '(")
(goto-char (point-max)) (goto-char (point-max))
(insert "))") (insert "))")
(write-file "tmp"))
#+end_src #+end_src
#+RESULTS: Implement the flow on the torus
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun move (p v time) (defun move (p v time)
(list (mod (+ (car p) (* time (car v))) w) (list (mod (+ (car p) (* time (car v))) w)
(mod (+ (cadr p) (* time (cadr v))) h))) (mod (+ (cadr p) (* time (cadr v))) h)))
(setq newpos (--map (move (car it) (cadr it) 100) data)) (setq newpos (--map (move (car it) (cadr it) 100) data))
(defun quadrant (comp p)
(defun quadrant-p (comp p)
(and (funcall (car comp) (car p) (floor (* w 0.5))) (and (funcall (car comp) (car p) (floor (* w 0.5)))
(funcall (cadr comp) (cadr p) (floor (* h 0.5))))) (funcall (cadr comp) (cadr p) (floor (* h 0.5)))))
(-reduce '* (--map (length (-filter (lambda (x) (quadrant it x)) newpos)) '((< <) (< >) (> <) (> >)))))
(-reduce '* (--map (length (-filter (lambda (x) (quadrant-p it x))
newpos))
'((< <) (< >) (> <) (> >)))))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 231852216 : 231852216
the second part is... meh. try and filter frames and check them the second part is... meh. try and filter frames and check them by
by some properties some properties; for instance we could check for a long enough
#+begin_src emacs-lisp :results none horizontal line. Save the frames in plain text and then we can use
(defun flip (p) grep later
(list (- w (car p) 1) (cadr p)))
(defun symmetric-p (li)
(eq (length (-intersection li (-map #'flip li))) (length li)))
#+end_src
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun make-bg () (defun make-bg ()
(insert (apply #'concat (-repeat h (concat (concat (-repeat w ?. )) "\n"))))) (insert (apply #'concat (-repeat h (concat (concat (-repeat w ?. )) "\n")))))

Loading…
Cancel
Save