;;; solver.el --- Day 05 -*- lexical-binding: t; -*- ;; ;; Copyright (C) 2022 Óscar Nájera ;; ;; Author: Óscar Nájera ;; Maintainer: Óscar Nájera ;; Created: December 05, 2022 ;; Modified: December 05, 2022 ;; ;; This file is not part of GNU Emacs. ;; ;;; Commentary: ;; ;; Day 05 ;; ;;; Code: (require 'subr-x) (require 'cl-lib) (defun solver-build-stacks () (let* ((stacks-len (length (split-string (buffer-substring-no-properties (line-beginning-position) (line-end-position))))) (stacks (make-vector stacks-len nil)) next) (while (setq next (search-backward "]" nil t)) (push (char-before) (aref stacks (/ (- next (line-beginning-position) 1) 4)))) stacks)) (defun solver-apply-moves! (mover-f stacks) (while (re-search-forward (rx bol "move " (group (+ digit)) " from " (group (+ digit)) " to " (group (+ digit))) nil t) (let ((amount (string-to-number (match-string 1))) (from (1- (string-to-number (match-string 2)))) (to (1- (string-to-number (match-string 3))))) (setf (aref stacks to) (append (funcall mover-f (seq-take (aref stacks from) amount)) (aref stacks to))) (setf (aref stacks from) (seq-drop (aref stacks from) amount)))) stacks) (defun solver (bulkp) (with-temp-buffer (insert-file-contents "input") (re-search-forward "^ 1") (thread-last (solver-build-stacks) (solver-apply-moves! (if bulkp #'identity #'reverse)) (cl-map 'string #'car)))) (ert-deftest solver-solutions () (should (equal "TPGVQPFDH" (solver nil))) (should (equal "DMRDFRHHH" (solver t)))) (provide 'solver) ;;; solver.el ends here