;;; 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) (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) (message "%S" (list stacks-len (split-string (buffer-substring-no-properties (point) (line-end-position))))) (while (setq next (search-backward "]" nil t)) (push (intern (string (char-before))) (aref stacks (/ (- next (line-beginning-position) 1) 4)))) stacks)) (defun solver-apply-moves (stacks) (while (re-search-forward (rx bol "move " (group (+ digit)) " from " (group (+ digit)) " to " (group (+ digit))) nil t) (let ((from (1- (string-to-number (match-string 2)))) (to (1- (string-to-number (match-string 3))))) (dotimes (_ (string-to-number (match-string 1))) (push (pop (aref stacks from)) (aref stacks to))))) stacks) '(T P G V Q P F D H) (with-temp-buffer (insert-file-contents "input") (re-search-forward "^ 1") (thread-last (solver-build-stacks) (solver-apply-moves) (seq-map #'car))) (provide 'solver) ;;; solver.el ends here