You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.3 KiB

#+title: Solution to p1
First load the data as a list of positive or negative numbers
#+begin_src emacs-lisp :results none
(with-temp-buffer
(insert-file-contents "input")
(advent/replace-multiple-regex-buffer
'(("L" . "-")
("R" . "")))
(goto-char (point-min))
(insert "(setq data '(")
(goto-char (point-max))
(insert "))")
(eval-buffer))
#+end_src
Then add the rotations starting from the initial offset and count the
number of 0's mod 100; that is what we need for part 1
#+begin_src emacs-lisp
(length (--filter (= 0 it)
(--map (mod it 100)
(-running-sum (cons 50 data)))))
#+end_src
#+RESULTS:
: 962
For part 2, This works:
- we count the number of full turns that have been done in each
rotation. Since there are corner cases we need to average over two
offsets of ½
#+begin_src emacs-lisp
(defun advent/consecutive-pairs (list)
(-zip-pair list (cdr list)))
(defun average (li)
(/ (-sum li) (length li)))
(let ((number-lists (--map (-running-sum (cons it data))
(list 50.5 49.5))))
(average (--map
(-sum (--map (abs (- (car it) (cdr it)))
(advent/consecutive-pairs
(--map (floor it 100) it))))
number-lists)))
#+end_src
#+RESULTS:
: 5782