- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
(asdf:load-system "cl-ppcre")
(defmacro recase (match submatches string &rest c)
(let ((%string (gensym))
(%b (gensym)))
`(let ((,%string ,string))
(block ,%b
,@(loop :for i :in c
:collect `(multiple-value-bind (,match ,submatches) (cl-ppcre:scan-to-strings ,(caar i) ,%string)
(when ,match
(return-from ,%b (progn ,@(cdr i))))))))))
(with-open-file (input "input5.txt")
(let ((b (cl-ppcre:split " " (read-line input)))
(rs (make-hash-table :test #'equal)))
(labels ((n
(x b)
(recase
m q b
(("^\\[([0-9]+)\\]$")
(princ-to-string (parse-integer (concatenate 'string x (aref q 0)))))
(("^\\[\\*([0-9]+)\\]$")
(princ-to-string (* (or (parse-integer x :junk-allowed t) 0)
(parse-integer (aref q 0)))))
(("^\\[/([0-9]+)\\]$")
(princ-to-string (floor (or (parse-integer x :junk-allowed t) 0)
(parse-integer (aref q 0)))))
(("^\\[([0-9]+)->([0-9]+)\\]$")
(cl-ppcre:regex-replace-all (aref q 0) x (aref q 1)))
(("\\[oct\\]")
(loop :with y := (or (parse-integer x :junk-allowed t) 0)
:with s := (make-array 0 :element-type 'character :adjustable t :fill-pointer t)
:until (zerop y)
:do (vector-push-extend (digit-char (mod y 8)) s)
(setf y (floor y 8))
:finally (when (zerop (length s)) (return "0"))(return (nreverse s))))
(("\\[bksp\\]")
(princ-to-string (floor (or (parse-integer x :junk-allowed t) 0) 10)))))
(g (x z)
(setf (gethash x rs) z)
(unless (zerop z)
(loop :for i :in b :do
(g (n x i) (1- z)))))
(g2 (x)
(unless (gethash x rs)
(setf (gethash x rs) t)
(loop :for i :in b
:as c := (n x i)
:when (<= (length c) 4)
:do (g2 c)))))
; (loop :for i :from 0 :to 100 :do
; (loop :for j :in b :do
; (format t "~A ~A => ~A~%" i j (n (princ-to-string i) j))))
(g2 "0")
(loop :for i :being :the :hash-keys :of rs
:sum (parse-integer i) :into r
:finally (format t "~D~%" r))
; (loop :for i :being :the :hash-keys :of rs
; :do (format t "~A~%" i))
; (format t "~D~%" (hash-table-count rs))
)))
Комментарии (3) RSS
Добавить комментарий