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.

1.6 KiB

Solution to p1

First load the data as a list of positive or negative numbers

  (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))

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

  (length (--filter (= 0 it) (--map (mod it 100) (-running-sum (append '(50) data)))))
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
  (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)))))))
5782