Advertisement
Guest User

Untitled

a guest
Jan 26th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.37 KB | None | 0 0
  1. # 22mar10abu
  2. # (c) Software Lab. Alexander Burger
  3.  
  4. # ../p binary.l 10 -bye |diff - binary/output
  5. # time ../p binary.l 20 -bye
  6.  
  7. (de buildTree (Item Depth)
  8. (cons Item
  9. (and
  10. (n0 Depth)
  11. (cons
  12. (buildTree
  13. (dec (setq Item (>> -1 Item)))
  14. (dec 'Depth) )
  15. (buildTree Item Depth) ) ) ) )
  16.  
  17. (de checkNode (Node)
  18. (if2 (cadr Node) (cddr Node)
  19. (- (+ (car Node) (checkNode (cadr Node))) (checkNode @))
  20. (+ (car Node) (checkNode @))
  21. (- (car Node) (checkNode @))
  22. (car Node) ) )
  23.  
  24. (let (N (format (opt)) Min 4)
  25. (prinl
  26. "stretch tree of depth "
  27. (inc N)
  28. "^I check: "
  29. (checkNode (buildTree 0 (inc N))) )
  30. (let LongLivedTree (buildTree 0 N)
  31. (for (D Min (>= N D) (+ 2 D))
  32. (let (Sum 0 Iterations (>> (- D Min N) 1))
  33. (for I Iterations
  34. (inc 'Sum
  35. (+
  36. (checkNode (buildTree I D))
  37. (checkNode (buildTree (- I) D)) ) ) )
  38. (prinl
  39. (* 2 Iterations)
  40. "^I trees of depth "
  41. D
  42. "^I check: "
  43. Sum ) ) )
  44. (prinl
  45. "long lived tree of depth "
  46. N
  47. "^I check: "
  48. (checkNode LongLivedTree) ) ) )
  49.  
  50. # vi:et:ts=3:sw=3
  51.  
  52.  
  53. # 07nov09abu
  54. # (c) Software Lab. Alexander Burger
  55.  
  56. # ../p fannkuch2.l 7 -bye |diff - fannkuch/output
  57. # time ../p fannkuch2.l 12 -bye
  58.  
  59. (let (N (format (opt)) Lst (range N 1) L Lst)
  60. (catch NIL
  61. (recur (L) # Print the first 30 permutations
  62. (cond
  63. ((cdr L)
  64. (do (length L)
  65. (recurse (cdr L))
  66. (rot L) ) )
  67. ((ge0 (dec (30)))
  68. (prinl (reverse Lst)) )
  69. (T (throw)) ) ) )
  70. (let (Res (need N) M)
  71. (for (R Res R (cdr R))
  72. (later R
  73. (let L (cdr Lst)
  74. (recur (L) # Permute
  75. (if (cdr L)
  76. (do (length L)
  77. (recurse (cdr L))
  78. (rot L) )
  79. (let I 0 # For each permutation
  80. (for (P (copy Lst) (> (car P) 1) (flip P (car P)))
  81. (inc 'I) )
  82. (setq M (max I M)) ) ) )
  83. M ) )
  84. (rot Lst) )
  85. (wait NIL (full Res))
  86. (prinl "Pfannkuchen(" N ") = " (apply max Res)) ) )
  87.  
  88. # vi:et:ts=3:sw=3
  89.  
  90.  
  91. # 07nov09abu
  92. # (c) Software Lab. Alexander Burger
  93.  
  94. # ../p fannkuch.l 7 -bye |diff - fannkuch/output
  95. # time ../p fannkuch.l 12 -bye
  96.  
  97. (let (N (format (opt)) Lst (range N 1) L Lst M)
  98. (recur (L) # Permute
  99. (if (cdr L)
  100. (do (length L)
  101. (recurse (cdr L))
  102. (rot L) )
  103. (let I 0 # For each permutation
  104. (and (ge0 (dec (30))) (prinl (reverse Lst)))
  105. (for (P (copy Lst) (> (car P) 1) (flip P (car P)))
  106. (inc 'I) )
  107. (setq M (max I M)) ) ) )
  108. (prinl "Pfannkuchen(" N ") = " M) )
  109.  
  110. # vi:et:ts=3:sw=3
  111.  
  112.  
  113. # 02nov09abu
  114. # (c) Software Lab. Alexander Burger
  115.  
  116. # time ../p meteor.l 2098 |diff - meteor/output
  117.  
  118. (mapc def
  119. '(SW SE NW NE W E)
  120. '(caadr cdadr caaddr cdaddr cadddr cddddr) )
  121.  
  122. ### Pieces ###
  123. (de *Pieces
  124. # Blue
  125. (E E E SE SE (NW W) W W NW W W W)
  126. # Yellow
  127. (SE SW W SW NW (SE SW) W SW (NE NW) SE (SW W) SW NE E NE NW)
  128. # Magenta
  129. (E (W SW) SE SE W SW SE SE NE E (SW SW) SE NW NW NE E)
  130. # Black
  131. (SW W SE SE E NE (SW SW) SE NW NW E NE)
  132. # Blue green
  133. (SE SW (NE E) SE SE (NW W) NW (SE SW) NE NW (SE E) SE NW W NW (SE SW))
  134. # Brown
  135. (SW E SW SW NE SE SW SW NW SW SE SW NE NW E NW)
  136. # Green
  137. (E SW SE SW W SE SE SW SW (NE NW) NW E NE NW NW E)
  138. # Dark red
  139. (SE W SW SE NW SW SW SE SE (NW NE) E NW NW NE E NW)
  140. # Red
  141. (E (W SW) W W W SW W W E E NE E (W W) E (E NE) E)
  142. # Cyan
  143. (SE W W W E E NE SE NW SW W W) )
  144.  
  145. (de build (L F Lst)
  146. (mapcar
  147. '((X)
  148. (cons F
  149. (list 'setq 'B
  150. (if (atom X)
  151. (list (val X) 'B)
  152. (list (caadr X) (list (caar X) 'B)) ) )
  153. Lst ) )
  154. L ) )
  155.  
  156. (de trans (L F Lst)
  157. (for (X L X (cdr X))
  158. (if (pair (car X))
  159. (trans @ F Lst)
  160. (set X (F (memq (car X) Lst))) ) ) )
  161.  
  162. ### Board ((value (SW . SE) (NW . NE) W . E) ...) ###
  163. (de _dir Lst
  164. (or
  165. (and (pop Lst) (nth *Board (+ N @) 1))
  166. X ) )
  167.  
  168. (let X (cons T)
  169. (con X
  170. (cons (cons X X) (cons X X) X X) )
  171. (for (N . B) (setq *Board (make (do 50 (link (cons)))))
  172. (con B
  173. (cons
  174. (cons # (SW . SE)
  175. (_dir (NIL 4 4 4 4 5 5 5 5 5 .))
  176. (_dir (5 5 5 5 5 6 6 6 6 NIL .)) )
  177. (cons # (NW . NE)
  178. (_dir (NIL -6 -6 -6 -6 -5 -5 -5 -5 -5 .))
  179. (_dir (-5 -5 -5 -5 -5 -4 -4 -4 -4 NIL .)) )
  180. (_dir (NIL -1 -1 -1 -1 .)) # W
  181. (_dir (1 1 1 1 NIL .)) ) ) ) ) # E
  182.  
  183. (de display (Lst)
  184. (for B Lst
  185. (prin (at (4 . 10) " ") B " ")
  186. (at (0 . 5) (prinl)) )
  187. (prinl) )
  188.  
  189. ### Let's go ###
  190. (let
  191. (Lst
  192. (make
  193. (for (N . P) *Pieces
  194. (link
  195. (make
  196. (link (dec N))
  197. (do 2 # Permutations
  198. (do 6
  199. (for (L P (cut 4 'L))
  200. (link
  201. (cons # (testfun . placefun)
  202. (list '(B) (cons 'or (build @ 'car)))
  203. (cons '(B N) '(set B N) (build @ 'set '(N))) ) ) )
  204. (trans P cadr '(E SE SW W NW NE .)) )
  205. (trans P cadddr '(E SE NE W SW NW .)) ) ) ) ) )
  206. Arg (format (opt))
  207. Cnt 0
  208. Min T
  209. Max )
  210. (recur (Lst)
  211. (for P Lst
  212. (for X (cdr P)
  213. (unless ((car X) (car *Board))
  214. ((cdr X) (car *Board) (car P)) # Place piece
  215. (if (seek '((L) (not (caar L))) *Board) # Find free place
  216. (let (*Board @ B (car @) L)
  217. (recur (B)
  218. (set (push 'L B) T)
  219. (for F '(SW SE NW NE W E)
  220. (or (car (F B)) (recurse (F B))) ) )
  221. (mapc set L)
  222. (when (=0 (% (length L) 5)) # Prune
  223. (recurse (delete P Lst)) ) )
  224. # We found a solution
  225. (let L (mapcar car (up 0 *Board))
  226. (setq Min (min L Min) Max (max L Max)) )
  227. (when (= Arg (inc 'Cnt))
  228. (prinl Arg " solutions found")
  229. (prinl)
  230. (display Min)
  231. (display Max)
  232. (bye) ) )
  233. ((cdr X) (car *Board)) ) ) ) ) ) # Remove piece
  234.  
  235. # vi:et:ts=3:sw=3
  236.  
  237.  
  238. # 22mar10abu
  239. # (c) Software Lab. Alexander Burger
  240.  
  241. # ../p taskring.l 1000 |diff - threadring/output
  242. # time ../p taskring.l 50000000
  243.  
  244. (for N 503 # Start all 503 tasks
  245. (let Recv (tmp N)
  246. (call 'mkfifo Recv)
  247. (task (open Recv)
  248. N N
  249. Send (tmp (if (= 503 N) 1 (inc N)))
  250. (in @
  251. (if (gt0 (rd))
  252. (out Send (pr (dec @)))
  253. (println N) # Print result to stdout
  254. (bye) ) ) ) ) ) # Exit
  255.  
  256. (out (tmp 1) # Inject first token into the ring
  257. (pr (format (opt))) ) # as given by command line argument
  258. (wait) # Wait to exit
  259.  
  260. # vi:et:ts=3:sw=3
  261.  
  262.  
  263. # 22mar10abu
  264. # (c) Software Lab. Alexander Burger
  265.  
  266. # ../p threadring.l 1000 |diff - threadring/output
  267. # time ../p threadring.l 50000000
  268.  
  269. (for N 503 # Start all 503 threads
  270. (let (Recv (tmp N) Send (tmp (if (= 503 N) 1 (inc N))))
  271. (call 'mkfifo Recv)
  272. (unless (fork) # In child process:
  273. (in Recv
  274. (out Send
  275. (loop
  276. (NIL (rd))
  277. (NIL (gt0 @) # Done
  278. (out NIL (println N)) # Print result to stdout
  279. (kill *PPid) ) # Stop parent
  280. (pr (dec @))
  281. (flush) ) ) )
  282. (bye) ) ) )
  283.  
  284. (out (tmp 1) # Inject first token into the ring
  285. (pr (format (opt))) # as given by command line argument
  286. (flush)
  287. (wait) ) # Wait for signal
  288.  
  289. # vi:et:ts=3:sw=3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement