[p1] better solution

master
Jacopo De Simoi 3 months ago
parent f6232959c2
commit f8f498fd25
  1. 34
      p1/p1.org

@ -23,31 +23,27 @@ number of 0's mod 100; that is what we need for part 1
#+RESULTS:
: 962
For part 2, This ain't pretty, but it works:
For part 2, This 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
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)))
(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)))))))
(defun average (li)
(/ (-sum li) (length li)))
(let ((number-lists (--map (-running-sum (append (list 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

Loading…
Cancel
Save