Guest User

Untitled

a guest
Dec 16th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. (defun find-idx-of-max (list)
  2. (let ((max_i 0) (val) (max))
  3. (dotimes (i (length list) max_i)
  4. (setq val (nth i list))
  5. (setq max (nth max_i list))
  6. (when (> val max) (setq max_i i))
  7. )
  8. )
  9. )
  10.  
  11. (defun cycle-list (list start_i v)
  12. (let ((i start_i) (val v))
  13. (setf (nth i list) 0)
  14. (setq i (1+ i))
  15. (when (>= i (length list)) (setq i 0))
  16. (while (> val 0)
  17. (setf (nth i list) (1+ (nth i list)))
  18. (setq val (1- val))
  19. (setq i (1+ i))
  20. (when (>= i (length list)) (setq i 0))
  21. )
  22. list
  23. )
  24. )
  25.  
  26. (defun reallocate-step (list)
  27. (let ((n (length list)) i val rem)
  28. (setq i (find-idx-of-max list))
  29. (cycle-list list i (nth i list))
  30. )
  31. list
  32. )
  33.  
  34. (defun get-pos (element list)
  35. (let (idxs val)
  36. (catch 'break
  37. (dotimes (i (length list))
  38. (setq val (nth i list))
  39. (when (equal element val) (throw 'break i))
  40. )
  41. )
  42. )
  43. )
  44.  
  45. (get-pos 2 '(1 1 2)) ;; 2
  46.  
  47. (defun reallocate (list)
  48. (let ((s 1) (results))
  49. (reallocate-step list)
  50. (while (and (not (member list results)) (< s 10000))
  51. (push (copy-sequence list) results)
  52. (reallocate-step list)
  53. (setq s (1+ s))
  54. )
  55. (print results)
  56. (print (format "Part #1: %d" s))
  57. (print (format "Part #2: %d" (- (length results) (get-pos list (reverse results)))))
  58. )
  59. )
  60.  
  61. (reallocate '(0 2 7 0)) ;; 5
  62. (reallocate-step '(0 2 7 0)) ;; 2 4 1 2
  63. (reallocate-step '(3 1 2 3)) ;; 0 2 3 4
  64. (reallocate-step '(0 2 3 4)) ;; 1 3 1 4
  65. (reallocate-step '(1 3 1 4)) ;; 1 3 1 4
  66. (reallocate-step '(4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3)) ;; 5 2 0 13 1 10 10 6 6 9 8 4 15 6 13 4
  67. (reallocate '(4 1 15 12 0 9 9 5 5 8 7 3 14 5 12 3))
  68.  
  69. (find-idx-of-max '(7 3 2 19 20)) ;; 4
  70.  
  71. (cycle-list '(0 2 7 0) 2 2 1) ;; 2 4 1 2
Add Comment
Please, Sign In to add comment