1. Си / Говнокод #29303

    0

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 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))
          )))

    Запостил: anime-girl, 05 Декабря 2026

    Комментарии (3) RSS

    Добавить комментарий