day 1 part 1
This commit is contained in:
commit
af70982d4b
3 changed files with 48 additions and 0 deletions
37
01a.lisp
Normal file
37
01a.lisp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
(require :uiop)
|
||||
|
||||
(defun split-instruction (s)
|
||||
"Splits a string S into two parts at the given INDEX."
|
||||
(cons (subseq s 0 1)
|
||||
(subseq s 1)))
|
||||
|
||||
(defun turn-dial (dial instruction)
|
||||
(let ((cons-instruction (split-instruction instruction)))
|
||||
(let ((op (intern (car cons-instruction)))
|
||||
(amount (parse-integer (cdr cons-instruction))))
|
||||
(mod
|
||||
(funcall
|
||||
(cond
|
||||
((eql 'L op) #'-)
|
||||
((eql 'R op) #'+)
|
||||
(t (error "unknown op")))
|
||||
dial amount)
|
||||
100)
|
||||
)
|
||||
))
|
||||
|
||||
;; (turn-dial 50 "L68")
|
||||
;; (turn-dial 82 "L30")
|
||||
;; (turn-dial 52 "R48")
|
||||
|
||||
(defun rec-dial-vm (accum dial lines)
|
||||
(let ((instruction (pop lines)))
|
||||
(if instruction
|
||||
(let ((new-dial (turn-dial dial instruction)))
|
||||
(if (= new-dial 0)
|
||||
(rec-dial-vm accum new-dial lines)
|
||||
(rec-dial-vm (+ 1 accum) new-dial lines)))
|
||||
accum)))
|
||||
|
||||
(rec-dial-vm 0 50 (uiop:read-file-lines "01example.txt"))
|
||||
(rec-dial-vm 0 50 (uiop:read-file-lines "01data.txt"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue