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.
53 lines
1.6 KiB
53 lines
1.6 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 (append '(50) data))))) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
: 962 |
|
|
|
For part 2, This ain't pretty, but it works: |
|
- we count the number of full turns that have been done in each |
|
rotation |
|
- we add the number of times we landed at 0 by turning left and then |
|
right immediately after; these have not been counted above |
|
#+begin_src emacs-lisp |
|
(defun advent/consecutive-pairs (list) |
|
(-zip-pair list (cdr list))) |
|
|
|
(let ((number-list (-running-sum (append '(50) data)))) |
|
(+ |
|
(-sum (--map (abs (- (car it) (cdr it))) |
|
(advent/consecutive-pairs |
|
(--map (floor it 100) number-list)))) |
|
|
|
(length |
|
(--filter (and (eq (car it) 0) (< (cdr it) 0)) |
|
(-zip-pair |
|
(cdr (--map (mod it 100) number-list)) data))) |
|
|
|
(* -1 (length |
|
(--filter (and (eq (car it) 0) (< (cdr it) 0)) |
|
(-zip-pair |
|
(cdr (--map (mod it 100) number-list)) (cdr data))))))) |
|
#+end_src |
|
|
|
#+RESULTS: |
|
: 5782
|
|
|