day 5 part 1 in elisp
This commit is contained in:
parent
8ff58bfa0d
commit
024470574b
1
elisp/day-5/advent-of-code.el
Symbolic link
1
elisp/day-5/advent-of-code.el
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../advent-of-code.el
|
1
elisp/day-5/example.txt
Symbolic link
1
elisp/day-5/example.txt
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../../input/day-5-example.txt
|
1
elisp/day-5/input.txt
Symbolic link
1
elisp/day-5/input.txt
Symbolic link
|
@ -0,0 +1 @@
|
||||||
|
../../input/day-5.txt
|
120
elisp/day-5/solution.el
Executable file
120
elisp/day-5/solution.el
Executable file
|
@ -0,0 +1,120 @@
|
||||||
|
#!/usr/bin/env -S emacs --script
|
||||||
|
|
||||||
|
;; -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
|
;; Copyright (C) 2022 notroot
|
||||||
|
|
||||||
|
;; This program is free software: you can redistribute it and/or modify
|
||||||
|
;; it under the terms of the GNU General Public License as published by
|
||||||
|
;; the Free Software Foundation, either version 3 of the License, or
|
||||||
|
;; (at your option) any later version.
|
||||||
|
|
||||||
|
;; This program is distributed in the hope that it will be useful,
|
||||||
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
;; GNU General Public License for more details.
|
||||||
|
|
||||||
|
;; You should have received a copy of the GNU General Public License
|
||||||
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
(add-to-list 'load-path default-directory)
|
||||||
|
|
||||||
|
(require 'cl-lib)
|
||||||
|
(require 'eieio)
|
||||||
|
(require 'ert)
|
||||||
|
(require 'advent-of-code)
|
||||||
|
|
||||||
|
(defmacro -make-crates (&rest exprs)
|
||||||
|
`(make-advent-of-code-day-5-crates ,@exprs))
|
||||||
|
|
||||||
|
(cl-defstruct -crates
|
||||||
|
stacks)
|
||||||
|
|
||||||
|
(defun -crates-pop (crates n)
|
||||||
|
(with-slots (stacks) crates
|
||||||
|
(when-let ((stack (assoc n stacks)))
|
||||||
|
(pop (cdr stack)))))
|
||||||
|
|
||||||
|
(defun -crates-push (crates crate n)
|
||||||
|
(with-slots (stacks) crates
|
||||||
|
(if-let ((stack (assoc n stacks)))
|
||||||
|
(push crate (cdr stack))
|
||||||
|
(push (cons n (list crate)) stacks))))
|
||||||
|
|
||||||
|
(defun -crates-append (crates crate n)
|
||||||
|
(with-slots (stacks) crates
|
||||||
|
(if-let ((stack (assoc n stacks)))
|
||||||
|
(setf (cdr stack) (append (cdr stack) (list crate)))
|
||||||
|
(push (cons n (list crate)) stacks))))
|
||||||
|
|
||||||
|
(defun -crates-process-move (crates move)
|
||||||
|
(with-slots (n from to) move
|
||||||
|
(cl-loop repeat n
|
||||||
|
do (-crates-push crates (-crates-pop crates (- from 1)) (- to 1)))))
|
||||||
|
|
||||||
|
(defun -crates-from-list (input)
|
||||||
|
(cl-loop for (crate . n) in input
|
||||||
|
with crates = (-make-crates :stacks nil)
|
||||||
|
do (-crates-append crates crate n)
|
||||||
|
finally (return crates)))
|
||||||
|
|
||||||
|
(defmacro -make-move (&rest exprs)
|
||||||
|
`(make-advent-of-code-day-5-move ,@exprs))
|
||||||
|
|
||||||
|
(cl-defstruct -move
|
||||||
|
n from to)
|
||||||
|
|
||||||
|
(defun -parse-input-into-parts (input)
|
||||||
|
(let* ((delimiter (seq-position input "" 'string=))
|
||||||
|
(crates (seq-subseq input 0 (- delimiter 1)))
|
||||||
|
(moves (seq-subseq input (+ delimiter 1))))
|
||||||
|
(list crates moves)))
|
||||||
|
|
||||||
|
(defun -parse-crates-from-line (line)
|
||||||
|
(cl-loop for i from 0 to (- (length line) 3) by 4
|
||||||
|
for n from 0
|
||||||
|
for crate = (substring line i (+ i 3))
|
||||||
|
for crate-id = (substring crate 1 2)
|
||||||
|
for empty? = (not (string-match-p "[^\s\n]" crate))
|
||||||
|
if (not empty?)
|
||||||
|
collect (cons crate-id n)))
|
||||||
|
|
||||||
|
(defun -parse-move-from-line (line)
|
||||||
|
(save-match-data
|
||||||
|
(string-match (rx "move" space (group (one-or-more digit)) space
|
||||||
|
"from" space (group (one-or-more digit)) space
|
||||||
|
"to" space (group (one-or-more digit)))
|
||||||
|
line)
|
||||||
|
(list (string-to-number (match-string 1 line))
|
||||||
|
(string-to-number (match-string 2 line))
|
||||||
|
(string-to-number (match-string 3 line)))))
|
||||||
|
|
||||||
|
(defun -part-1 (input)
|
||||||
|
(pcase-let* ((`(,input-crates ,input-moves) (-parse-input-into-parts input))
|
||||||
|
(parsed-crates-list (cl-loop for line in input-crates
|
||||||
|
append (-parse-crates-from-line line)))
|
||||||
|
(crates (-crates-from-list parsed-crates-list))
|
||||||
|
(moves (cl-loop for line in input-moves
|
||||||
|
for move = (pcase-let ((`(,n ,from ,to) (-parse-move-from-line line)))
|
||||||
|
(-make-move :n n :from from :to to))
|
||||||
|
collect move)))
|
||||||
|
(seq-do (lambda (move)
|
||||||
|
(-crates-process-move crates move))
|
||||||
|
moves)
|
||||||
|
(cl-loop for n from 0 to (length (-crates-stacks crates))
|
||||||
|
for top-crate = (cadr (assoc n (-crates-stacks crates)))
|
||||||
|
for top-crates = top-crate then (concat top-crates top-crate)
|
||||||
|
finally (return top-crates))))
|
||||||
|
|
||||||
|
(ert-deftest -test-part-1-example ()
|
||||||
|
(should (equal (-part-1 (aoc-read-file-lines "example.txt")) "CMZ")))
|
||||||
|
|
||||||
|
(ert-deftest -test-part-1 ()
|
||||||
|
(should (equal (-part-1 (aoc-read-file-lines "input.txt")) "CFFHVVHNC")))
|
||||||
|
|
||||||
|
(when noninteractive
|
||||||
|
(ert-run-tests-batch))
|
||||||
|
|
||||||
|
;; Local Variables:
|
||||||
|
;; read-symbol-shorthands: (("-" . "advent-of-code-day-5-") ("aoc-" . "advent-of-code-"))
|
||||||
|
;; End:
|
Loading…
Reference in a new issue