[p7] better whitespace and comments

master
Jacopo De Simoi 6 months ago
parent 4e315527d7
commit dde6910392
  1. 39
      p7/p7.org

@ -17,15 +17,11 @@ Yay, another 2D problem
height (length data-char)) height (length data-char))
#+end_src #+end_src
This is for part 1; This is for part 1; record the splitting points and count them at the end
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun position-valid-p (pos)
(and (>= (car pos) 0) (< (car pos) width)
(defun position-valid-p (pos) (>= (cadr pos) 0) (< (cadr pos) height)))
(and (and (>= (car pos) 0) (< (car pos) width))
(and (>= (cadr pos) 0) (< (cadr pos) height))))
(advent/char-at '(-1 0) data-char)
(defun tachyon-step (el) (defun tachyon-step (el)
(if (not (position-valid-p el)) (list el) (if (not (position-valid-p el)) (list el)
@ -35,27 +31,40 @@ This is for part 1;
(list down) (list down)
(push down splits) (push down splits)
(--map (advent/neighbour el it) '((-1 0) (1 0))))))) (--map (advent/neighbour el it) '((-1 0) (1 0)))))))
(setq tachyons (advent/coordinates-of ?S data-char))
(setq splits nil)
(length (--fix (-distinct (-mapcat 'tachyon-step it)) tachyons)) (setq tachyons (advent/coordinates-of ?S data-char)
splits nil)
(--fix (-distinct (-mapcat 'tachyon-step it)) tachyons)
(length (-distinct splits)) (length (-distinct splits))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 1507 : 1507
This is for part 2. At each step record how many beams are at a given
position. Each beam has some multiplicity. ~compress~ merges beams at
the same position in one beam with multiplicity equal to the sum of
the multiplicities of each beam
#+begin_src emacs-lisp #+begin_src emacs-lisp
(defun tachyon-step-freq (el) (defun tachyon-step-freq (el)
(--map (cons it (cdr el)) (tachyon-step (car el)))) (--map (cons it (cdr el))
(tachyon-step (car el))))
(setq tachyons (cons (car (advent/coordinates-of ?S data-char)) 1)) (setq tachyons (cons (car (advent/coordinates-of ?S data-char)) 1))
(defun compress (li) (defun compress (li)
(--map (cons it (-sum (-map #'cdr (-filter (lambda (x) (equal it (car x))) li)))) (-distinct (-map #'car li)))) (--map (cons it (-sum (-map #'cdr
(-filter (lambda (x)
(-sum (-map #'cdr (--fix (compress (-mapcat 'tachyon-step-freq it)) (list tachyons)))) (equal it (car x)))
li))))
(-distinct (-map #'car li))))
(-sum (-map #'cdr
(--fix (compress (-mapcat 'tachyon-step-freq it))
(list tachyons))))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 1537373473728 : 1537373473728

Loading…
Cancel
Save