aboutsummaryrefslogtreecommitdiff
path: root/2022/Day05/part-two.cl
blob: 26225043fe69cf1ca2b2a27a179749328efca7cb (plain) (blame)
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
51
52
(defvar *prog-input*)

;;; After running file, if you enter a blank line, code will work on each
;;; line of input, until "end" is typed.
;;; Otherwise, the input is taken as a filename and code will be executed
;;; over each line in the file until EOF.
(let ((ui (read-line)))
  (if (equal ui "")
    (setq *prog-input* *standard-input*)
    (setq *prog-input* (open ui))))

(defun first-n (lis n)
  (if (= n 0)
    NIL
    (cons (car lis) (first-n (cdr lis) (- n 1)))))

(let
  ((inpt (read-line *prog-input* NIL)) (i 1) (stacks '(())) (si 0) (x 0))

  (loop until (char= (char inpt 1) #\1) do
        (loop until (>= i (length inpt)) do
              (if (<= (length stacks) si)
                (setq stacks (append stacks (list NIL NIL) ))) ; ZERO idea why I can't just append '(())

              (if (not (char= (char inpt i) #\Space))
                (setf (nth si stacks) (append (nth si stacks) (list (char inpt i)))))

              (setq si (+ si 1) i (+ i 4)))

        (setq i 1 si 0)
        (setq inpt (read-line *prog-input* NIL)))

  (read-line *prog-input* NIL)

  (setq inpt (read-line *prog-input* NIL))
  (loop until (or (string= inpt "end") (not inpt)) do
        (setq si (- (parse-integer inpt :junk-allowed T :start (position #\Space inpt :start (position #\f inpt))) 1))
        (setq  i (- (parse-integer inpt :junk-allowed T :start (position #\Space inpt :start (position #\t inpt))) 1))

        (setq x (parse-integer inpt :junk-allowed T :start (position #\Space inpt)))
        (setf (nth i stacks) (append (first-n (nth si stacks) x) (nth i stacks)))
        (dotimes (m x)
          (pop (nth si stacks)))

        (setq inpt (read-line *prog-input* NIL)))

  (loop for x in stacks do
        (prin1 (car x))))

(if (not (eq *prog-input* *standard-input*))
  (close *prog-input*))