parent
cc8928d457
commit
c4ef3f5c79
1 changed files with 55 additions and 0 deletions
@ -0,0 +1,55 @@ |
||||
#+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 (append '(50) data))))) |
||||
#+end_src |
||||
|
||||
#+RESULTS: |
||||
: 962 |
||||
|
||||
For part 2, This ain't pretty, but it works: |
||||
#+begin_src emacs-lisp |
||||
(defun advent/consecutive-pairs (list) |
||||
(-zip-pair list (cdr list))) |
||||
|
||||
(+ |
||||
(-sum (--map (abs (- (car it) (cdr it))) |
||||
(advent/consecutive-pairs |
||||
(--map (floor it 100) (-running-sum (append '(50) data)))))) |
||||
|
||||
(length |
||||
(--filter (and (eq (car it) 0) (< (cdr it) 0)) |
||||
(-zip-pair |
||||
(cdr (--map (mod it 100) (-running-sum (append '(50) data)))) |
||||
data))) |
||||
|
||||
(* -1 (length |
||||
(--filter (and (eq (car it) 0) (< (cdr it) 0)) |
||||
(-zip-pair |
||||
(cdr (--map (mod it 100) (-running-sum (append '(50) data)))) |
||||
(cdr data)))))) |
||||
#+end_src |
||||
|
||||
#+RESULTS: |
||||
: 5782 |
||||
|
||||
#+begin_src emacs-lisp |
||||
(floor -5 100) |
||||
#+end_src |
||||
* |
||||
Loading…
Reference in new issue