aboutsummaryrefslogtreecommitdiffstats
path: root/AoC2022/05/solver.el
blob: 59faf65e64ac5285cb2a6d569092da5b4e1404f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;; solver.el --- Day 05 -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2022 Óscar Nájera
;;
;; Author: Óscar Nájera <hi@oscarnajera.com>
;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
;; 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