fork download
  1. F=
  2. -> input {
  3. state = [0]*8
  4.  
  5. instructions = input.chars.flat_map do |c|
  6. value = c.ord
  7. to_print = (0..7).map{|i| value[i] }
  8.  
  9. moves, idx = (0..7).map{|i|
  10. to_print_shifted = to_print.rotate i
  11. [(0..7).select{|j| state[j] != to_print_shifted[j]}, i]
  12. }.min_by{|x|x[0].size}
  13.  
  14. moves.map{|m| state[m]=1-state[m] }
  15. moves.map{|m|"t #{7-m}"} + ["p #{idx}"]
  16. end
  17.  
  18. instructions * "\n"
  19. }
  20.  
  21. require 'minitest/autorun'
  22.  
  23. describe F do
  24. def test_case_1
  25. assert_equal <<-EOS.chomp, F['?']
  26. t 7
  27. t 6
  28. t 5
  29. t 4
  30. t 3
  31. t 2
  32. p 0
  33. EOS
  34. end
  35.  
  36. def test_case_2
  37. assert_equal <<-EOS.chomp, F[':=']
  38. t 6
  39. t 4
  40. t 3
  41. t 2
  42. p 0
  43. t 1
  44. p 7
  45. EOS
  46. end
  47.  
  48. def test_case_3
  49. assert_equal <<-EOS.chomp, F['0123456789']
  50. t 3
  51. t 2
  52. p 0
  53. t 7
  54. p 0
  55. t 7
  56. t 6
  57. p 0
  58. t 7
  59. p 0
  60. t 7
  61. t 6
  62. t 5
  63. p 0
  64. t 7
  65. p 0
  66. t 7
  67. t 6
  68. p 0
  69. t 7
  70. p 0
  71. t 3
  72. t 2
  73. p 3
  74. t 2
  75. p 3
  76. EOS
  77. end
  78.  
  79.  
  80. def test_case_4
  81. assert_equal <<-EOS.chomp, F['9876543210']
  82. t 7
  83. t 4
  84. t 3
  85. t 2
  86. p 0
  87. t 7
  88. p 0
  89. t 7
  90. t 0
  91. p 5
  92. t 4
  93. p 5
  94. t 5
  95. t 0
  96. p 0
  97. t 7
  98. p 0
  99. t 7
  100. t 6
  101. t 5
  102. p 0
  103. t 7
  104. p 0
  105. t 7
  106. t 6
  107. p 0
  108. t 7
  109. p 0
  110. EOS
  111. end
  112.  
  113. def test_case_5
  114. assert_equal <<-EOS.chomp, F['Hello, World!']
  115. t 4
  116. t 1
  117. p 0
  118. t 7
  119. t 3
  120. p 2
  121. t 6
  122. t 1
  123. p 2
  124. p 2
  125. t 1
  126. t 0
  127. p 2
  128. t 3
  129. t 1
  130. t 0
  131. p 2
  132. t 7
  133. t 6
  134. p 2
  135. t 7
  136. t 6
  137. t 2
  138. t 0
  139. p 1
  140. t 5
  141. t 1
  142. t 0
  143. p 0
  144. t 7
  145. t 2
  146. p 3
  147. t 6
  148. t 2
  149. p 0
  150. t 4
  151. p 0
  152. t 1
  153. p 3
  154. EOS
  155. end
  156.  
  157. def test_case_6
  158. assert_equal <<-EOS.chomp, F['The quick brown fox jumps over the lazy dog.']
  159. t 5
  160. t 3
  161. t 1
  162. p 0
  163. t 2
  164. t 1
  165. p 1
  166. t 7
  167. t 3
  168. t 1
  169. p 0
  170. t 7
  171. t 5
  172. t 1
  173. p 0
  174. t 7
  175. t 3
  176. t 1
  177. p 0
  178. t 5
  179. p 0
  180. t 5
  181. t 4
  182. t 3
  183. p 0
  184. t 6
  185. t 4
  186. p 0
  187. t 4
  188. p 0
  189. t 7
  190. t 6
  191. t 4
  192. t 1
  193. p 0
  194. t 6
  195. t 1
  196. p 0
  197. t 3
  198. p 0
  199. t 5
  200. t 0
  201. p 4
  202. t 7
  203. t 0
  204. p 0
  205. t 1
  206. p 1
  207. t 7
  208. t 6
  209. t 5
  210. t 3
  211. p 0
  212. t 6
  213. t 5
  214. t 1
  215. p 0
  216. t 7
  217. t 4
  218. p 0
  219. t 2
  220. t 1
  221. p 3
  222. t 7
  223. t 6
  224. t 5
  225. p 2
  226. t 6
  227. t 2
  228. t 1
  229. p 0
  230. t 0
  231. p 7
  232. t 7
  233. t 0
  234. p 5
  235. t 7
  236. t 6
  237. t 4
  238. t 3
  239. p 0
  240. t 7
  241. t 6
  242. p 0
  243. t 7
  244. t 6
  245. t 3
  246. t 1
  247. p 0
  248. t 7
  249. t 6
  250. t 5
  251. t 4
  252. t 1
  253. p 0
  254. t 4
  255. p 4
  256. t 6
  257. p 0
  258. t 6
  259. t 1
  260. p 4
  261. t 7
  262. t 6
  263. t 5
  264. p 0
  265. t 5
  266. t 3
  267. t 1
  268. p 0
  269. t 1
  270. p 1
  271. t 7
  272. t 3
  273. t 1
  274. p 0
  275. t 7
  276. t 5
  277. t 1
  278. p 0
  279. t 5
  280. t 4
  281. t 1
  282. p 0
  283. t 1
  284. p 3
  285. t 7
  286. t 3
  287. p 1
  288. t 5
  289. t 1
  290. p 0
  291. t 7
  292. t 4
  293. t 3
  294. t 1
  295. p 0
  296. t 5
  297. t 1
  298. p 0
  299. t 7
  300. t 6
  301. t 4
  302. p 0
  303. t 4
  304. p 0
  305. t 7
  306. t 4
  307. t 1
  308. p 0
  309. EOS
  310. end
  311. end
  312.  
Success #stdin #stdout 0.09s 10744KB
stdin
Standard input is empty
stdout
Run options: --seed 63574

# Running tests:

......

Finished tests in 0.004606s, 1302.6764 tests/s, 1302.6764 assertions/s.

6 tests, 6 assertions, 0 failures, 0 errors, 0 skips