Break a sweat. p15 done

main
Jacopo De Simoi 11 months ago
parent 92103cee42
commit 84f317740b
  1. 41
      p15/p15.part2.org

@ -5,7 +5,7 @@ Process the input file to create the maps and the sequence of moves
#+begin_src emacs-lisp :results none #+begin_src emacs-lisp :results none
(require 'dash) (require 'dash)
(with-temp-buffer (with-temp-buffer
(insert-file-contents "input-fuffa") (insert-file-contents "input")
(goto-char (point-min)) (goto-char (point-min))
(replace-regexp "^\\(#.*#\\)$" "\"\\1\"") (replace-regexp "^\\(#.*#\\)$" "\"\\1\"")
(goto-char (point-min)) (goto-char (point-min))
@ -50,12 +50,18 @@ elements
(-map-indexed (lambda (y l) (-map-indexed (lambda (y l)
(-map-indexed (lambda (x el) (list el (list x y))) l)) (-map-indexed (lambda (x el) (list el (list x y))) l))
10map-things))))) map-things)))))
(setq walls (coordinates-of ?#) (setq walls (coordinates-of ?#)
l-crates (coordinates-of ?[) l-crates (coordinates-of ?[)
r-crates (coordinates-of ?]) r-crates (coordinates-of ?])
robot (car (coordinates-of ?@))) robot (car (coordinates-of ?@)))
(setq buddies-alist '((l-crate . (1 0))
(r-crate . (-1 0))
(robot . nil)))
#+end_src #+end_src
Implement the move logic. The key function is try-move: it tries to Implement the move logic. The key function is try-move: it tries to
@ -81,23 +87,23 @@ be pushed.
"Updates the content of the warehouse by moving whatever is at "Updates the content of the warehouse by moving whatever is at
FROM to the position TO" FROM to the position TO"
(cond (cond
((eq (thing-at from) 'crate) (setq crates (-replace from to crates))) ((eq (thing-at from) 'l-crate) (setq l-crates (-replace from to l-crates)))
((eq (thing-at from) 'r-crate) (setq r-crates (-replace from to r-crates)))
(t (setq robot to)))) (t (setq robot to))))
(defun try-move (p dir &optional pretend) (defun try-move (p dir &optional pretend no-buddy-check)
"Try to move the object at P in the direction DIR. This moves "Try to move the object at P in the direction DIR. This moves
both halves of a crate together. just pretend to move if PRETEND is non-nil" both halves of a crate together. just pretend to move if PRETEND is non-nil"
(let ((thing (thing-at p))) (let ((thing (thing-at p)))
(cond (cond
((not thing) t) ((not thing) t)
((eq thing 'wall) nil) ((eq thing 'wall) nil)
((eq thing 'r-crate) (t (let* ((dest (neighbour p dir))
(let ((dest (neighbour p dir)) (buddy-dir (when (eq 0 (car dir))
(buddy (neighbour p ' (-1 0))) ) ;got here (cdr (assoc thing buddies-alist))))
(when (try-move dest dir pretend) (buddy (when buddy-dir (neighbour p buddy-dir))))
(or pretend (do-move p dest))))) (when (and (try-move dest dir pretend)
(t (let ((dest (neighbour p dir))) (or no-buddy-check (try-move buddy dir pretend t)))
(when (try-move dest dir pretend)
(or pretend (do-move p dest)))))))) (or pretend (do-move p dest))))))))
#+end_src #+end_src
@ -116,17 +122,18 @@ This is just to make pretty pictures of the current state
(with-temp-buffer (with-temp-buffer
(make-bg) (make-bg)
(--map (plot it "#") walls); should use --each (--map (plot it "#") walls); should use --each
(--map (plot it "O") crates) (--map (plot it "[") l-crates)
(--map (plot it "]") r-crates)
(plot robot "@") (plot robot "@")
(write-file "test-out"))) (write-file "test-out")))
#+end_src #+end_src
Finally, implement the moves and compute the result for part 1 Finally, implement the moves and compute the result for part 2
#+begin_src emacs-lisp #+begin_src emacs-lisp
(--map (try-move robot it) commands) (--map (and (try-move robot it t) (try-move robot it)) commands)
(save-pic) (-reduce #'+ (--map (+ (car it) (* 100 (cadr it))) l-crates))
(-reduce #'+ (--map (+ (car it) (* 100 (cadr it))) crates))
#+end_src #+end_src
#+RESULTS: #+RESULTS:
: 1371036 : 1392847

Loading…
Cancel
Save