Guest User

Untitled

a guest
Dec 16th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 41.71 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. class LoopUnrollBenchmark {
  4. public static void main(String[] args) {
  5. //int[] input = {0, 1, 0, 1, 1, 0, 0, 1};
  6. int[] input = {1,0,0,1,0,0,1,0};
  7. int N = 1000000000;
  8. int benchmarkIters = 10;
  9. long[] u0 = new long[benchmarkIters];
  10. long[] u10 = new long[benchmarkIters];
  11. long[] u100 = new long[benchmarkIters];
  12.  
  13. long start, fin;
  14.  
  15. for (int i = 0; i < benchmarkIters; i++) {
  16. start = System.nanoTime();
  17. noUnroll(input, N);
  18. fin = System.nanoTime() - start;
  19. u0[i] = fin;
  20.  
  21. start = System.nanoTime();
  22. unroll10(input, N);
  23. fin = System.nanoTime() - start;
  24. u10[i] = fin;
  25.  
  26. start = System.nanoTime();
  27. unroll100(input, N);
  28. fin = System.nanoTime() - start;
  29. u100[i] = fin;
  30.  
  31. System.out.println("Finished iteration " + i + " out of " + benchmarkIters);
  32. }
  33.  
  34. System.out.println("Averages over " + benchmarkIters + " iterations");
  35. System.out.println("No Unroll: " + Arrays.stream(u0).sum()/benchmarkIters/1000 + " ms");
  36. System.out.println("10 Unroll: " + Arrays.stream(u10).sum()/benchmarkIters/1000 + " ms");
  37. System.out.println("100 Unroll: " + Arrays.stream(u100).sum()/benchmarkIters/1000 + " ms");
  38.  
  39. //Arrays.stream(cells).forEach(System.out::print);
  40. }
  41.  
  42. public static int[] noUnroll(int[] cells, int N) {
  43. int[] newCells = new int[8];
  44. newCells[0] = 0;
  45. newCells[7] = 0;
  46.  
  47. while (N > 0) {
  48. for (int i = 1; i < 7; i++) {
  49. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  50. newCells[i] = 1;
  51. } else {
  52. newCells[i] = 0;
  53. }
  54. }
  55. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  56. cells[0] = 0;
  57. cells[7] = 0;
  58. N--;
  59. }
  60. return cells;
  61. }
  62.  
  63. public static int[] unroll10(int[] cells, int N) {
  64. //if (N == 0) return cells;
  65. int[] newCells = new int[8];
  66. newCells[0] = 0;
  67. newCells[7] = 0;
  68.  
  69. while (N > 10) {
  70. // ----------------------------------
  71. for (int i = 1; i < 7; i++) {
  72. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  73. newCells[i] = 1;
  74. } else {
  75. newCells[i] = 0;
  76. }
  77. }
  78. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  79. cells[0] = 0;
  80. cells[7] = 0;
  81. // ----------------------------------
  82. for (int i = 1; i < 7; i++) {
  83. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  84. newCells[i] = 1;
  85. } else {
  86. newCells[i] = 0;
  87. }
  88. }
  89. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  90. // ----------------------------------
  91. for (int i = 1; i < 7; i++) {
  92. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  93. newCells[i] = 1;
  94. } else {
  95. newCells[i] = 0;
  96. }
  97. }
  98. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  99. // ----------------------------------
  100. for (int i = 1; i < 7; i++) {
  101. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  102. newCells[i] = 1;
  103. } else {
  104. newCells[i] = 0;
  105. }
  106. }
  107. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  108. // ----------------------------------
  109. for (int i = 1; i < 7; i++) {
  110. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  111. newCells[i] = 1;
  112. } else {
  113. newCells[i] = 0;
  114. }
  115. }
  116. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  117. // ----------------------------------
  118.  
  119. //copy another 5
  120.  
  121. // ----------------------------------
  122. for (int i = 1; i < 7; i++) {
  123. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  124. newCells[i] = 1;
  125. } else {
  126. newCells[i] = 0;
  127. }
  128. }
  129. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  130. // ----------------------------------
  131. for (int i = 1; i < 7; i++) {
  132. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  133. newCells[i] = 1;
  134. } else {
  135. newCells[i] = 0;
  136. }
  137. }
  138. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  139. // ----------------------------------
  140. for (int i = 1; i < 7; i++) {
  141. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  142. newCells[i] = 1;
  143. } else {
  144. newCells[i] = 0;
  145. }
  146. }
  147. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  148. // ----------------------------------
  149. for (int i = 1; i < 7; i++) {
  150. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  151. newCells[i] = 1;
  152. } else {
  153. newCells[i] = 0;
  154. }
  155. }
  156. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  157. // ----------------------------------
  158. for (int i = 1; i < 7; i++) {
  159. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  160. newCells[i] = 1;
  161. } else {
  162. newCells[i] = 0;
  163. }
  164. }
  165. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  166. // ----------------------------------
  167.  
  168. N-=10;
  169. }
  170.  
  171. while (N > 0) {
  172. // ----------------------------------
  173. for (int i = 1; i < 7; i++) {
  174. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  175. newCells[i] = 1;
  176. } else {
  177. newCells[i] = 0;
  178. }
  179. }
  180. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  181. cells[0] = 0;
  182. cells[7] = 0;
  183. // ----------------------------------
  184. N--;
  185. }
  186. return cells;
  187. // return prisonAfterNDays(newCells, N-1);
  188. }
  189.  
  190. public static int[] unroll100(int[] cells, int N) {
  191. //if (N == 0) return cells;
  192. int[] newCells = new int[8];
  193. newCells[0] = 0;
  194. newCells[7] = 0;
  195.  
  196. while (N > 100){
  197. // 0
  198. for (int i = 1; i < 7; i++) {
  199. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  200. newCells[i] = 1;
  201. } else {
  202. newCells[i] = 0;
  203. }
  204. }
  205. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  206. cells[0] = 0;
  207. cells[7] = 0;
  208. for (int i = 1; i < 7; i++) {
  209. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  210. newCells[i] = 1;
  211. } else {
  212. newCells[i] = 0;
  213. }
  214. }
  215. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  216. for (int i = 1; i < 7; i++) {
  217. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  218. newCells[i] = 1;
  219. } else {
  220. newCells[i] = 0;
  221. }
  222. }
  223. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  224. for (int i = 1; i < 7; i++) {
  225. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  226. newCells[i] = 1;
  227. } else {
  228. newCells[i] = 0;
  229. }
  230. }
  231. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  232. for (int i = 1; i < 7; i++) {
  233. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  234. newCells[i] = 1;
  235. } else {
  236. newCells[i] = 0;
  237. }
  238. }
  239. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  240. for (int i = 1; i < 7; i++) {
  241. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  242. newCells[i] = 1;
  243. } else {
  244. newCells[i] = 0;
  245. }
  246. }
  247. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  248. for (int i = 1; i < 7; i++) {
  249. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  250. newCells[i] = 1;
  251. } else {
  252. newCells[i] = 0;
  253. }
  254. }
  255. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  256. for (int i = 1; i < 7; i++) {
  257. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  258. newCells[i] = 1;
  259. } else {
  260. newCells[i] = 0;
  261. }
  262. }
  263. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  264. for (int i = 1; i < 7; i++) {
  265. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  266. newCells[i] = 1;
  267. } else {
  268. newCells[i] = 0;
  269. }
  270. }
  271. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  272. for (int i = 1; i < 7; i++) {
  273. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  274. newCells[i] = 1;
  275. } else {
  276. newCells[i] = 0;
  277. }
  278. }
  279. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  280. // 10
  281. for (int i = 1; i < 7; i++) {
  282. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  283. newCells[i] = 1;
  284. } else {
  285. newCells[i] = 0;
  286. }
  287. }
  288. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  289. cells[0] = 0;
  290. cells[7] = 0;
  291. for (int i = 1; i < 7; i++) {
  292. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  293. newCells[i] = 1;
  294. } else {
  295. newCells[i] = 0;
  296. }
  297. }
  298. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  299. for (int i = 1; i < 7; i++) {
  300. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  301. newCells[i] = 1;
  302. } else {
  303. newCells[i] = 0;
  304. }
  305. }
  306. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  307. for (int i = 1; i < 7; i++) {
  308. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  309. newCells[i] = 1;
  310. } else {
  311. newCells[i] = 0;
  312. }
  313. }
  314. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  315. for (int i = 1; i < 7; i++) {
  316. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  317. newCells[i] = 1;
  318. } else {
  319. newCells[i] = 0;
  320. }
  321. }
  322. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  323. for (int i = 1; i < 7; i++) {
  324. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  325. newCells[i] = 1;
  326. } else {
  327. newCells[i] = 0;
  328. }
  329. }
  330. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  331. for (int i = 1; i < 7; i++) {
  332. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  333. newCells[i] = 1;
  334. } else {
  335. newCells[i] = 0;
  336. }
  337. }
  338. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  339. for (int i = 1; i < 7; i++) {
  340. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  341. newCells[i] = 1;
  342. } else {
  343. newCells[i] = 0;
  344. }
  345. }
  346. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  347. for (int i = 1; i < 7; i++) {
  348. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  349. newCells[i] = 1;
  350. } else {
  351. newCells[i] = 0;
  352. }
  353. }
  354. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  355. for (int i = 1; i < 7; i++) {
  356. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  357. newCells[i] = 1;
  358. } else {
  359. newCells[i] = 0;
  360. }
  361. }
  362. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  363. // 20
  364. for (int i = 1; i < 7; i++) {
  365. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  366. newCells[i] = 1;
  367. } else {
  368. newCells[i] = 0;
  369. }
  370. }
  371. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  372. cells[0] = 0;
  373. cells[7] = 0;
  374. for (int i = 1; i < 7; i++) {
  375. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  376. newCells[i] = 1;
  377. } else {
  378. newCells[i] = 0;
  379. }
  380. }
  381. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  382. for (int i = 1; i < 7; i++) {
  383. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  384. newCells[i] = 1;
  385. } else {
  386. newCells[i] = 0;
  387. }
  388. }
  389. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  390. for (int i = 1; i < 7; i++) {
  391. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  392. newCells[i] = 1;
  393. } else {
  394. newCells[i] = 0;
  395. }
  396. }
  397. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  398. for (int i = 1; i < 7; i++) {
  399. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  400. newCells[i] = 1;
  401. } else {
  402. newCells[i] = 0;
  403. }
  404. }
  405. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  406. for (int i = 1; i < 7; i++) {
  407. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  408. newCells[i] = 1;
  409. } else {
  410. newCells[i] = 0;
  411. }
  412. }
  413. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  414. for (int i = 1; i < 7; i++) {
  415. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  416. newCells[i] = 1;
  417. } else {
  418. newCells[i] = 0;
  419. }
  420. }
  421. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  422. for (int i = 1; i < 7; i++) {
  423. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  424. newCells[i] = 1;
  425. } else {
  426. newCells[i] = 0;
  427. }
  428. }
  429. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  430. for (int i = 1; i < 7; i++) {
  431. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  432. newCells[i] = 1;
  433. } else {
  434. newCells[i] = 0;
  435. }
  436. }
  437. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  438. for (int i = 1; i < 7; i++) {
  439. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  440. newCells[i] = 1;
  441. } else {
  442. newCells[i] = 0;
  443. }
  444. }
  445. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  446. // 30
  447. for (int i = 1; i < 7; i++) {
  448. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  449. newCells[i] = 1;
  450. } else {
  451. newCells[i] = 0;
  452. }
  453. }
  454. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  455. cells[0] = 0;
  456. cells[7] = 0;
  457. for (int i = 1; i < 7; i++) {
  458. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  459. newCells[i] = 1;
  460. } else {
  461. newCells[i] = 0;
  462. }
  463. }
  464. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  465. for (int i = 1; i < 7; i++) {
  466. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  467. newCells[i] = 1;
  468. } else {
  469. newCells[i] = 0;
  470. }
  471. }
  472. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  473. for (int i = 1; i < 7; i++) {
  474. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  475. newCells[i] = 1;
  476. } else {
  477. newCells[i] = 0;
  478. }
  479. }
  480. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  481. for (int i = 1; i < 7; i++) {
  482. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  483. newCells[i] = 1;
  484. } else {
  485. newCells[i] = 0;
  486. }
  487. }
  488. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  489. for (int i = 1; i < 7; i++) {
  490. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  491. newCells[i] = 1;
  492. } else {
  493. newCells[i] = 0;
  494. }
  495. }
  496. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  497. for (int i = 1; i < 7; i++) {
  498. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  499. newCells[i] = 1;
  500. } else {
  501. newCells[i] = 0;
  502. }
  503. }
  504. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  505. for (int i = 1; i < 7; i++) {
  506. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  507. newCells[i] = 1;
  508. } else {
  509. newCells[i] = 0;
  510. }
  511. }
  512. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  513. for (int i = 1; i < 7; i++) {
  514. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  515. newCells[i] = 1;
  516. } else {
  517. newCells[i] = 0;
  518. }
  519. }
  520. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  521. for (int i = 1; i < 7; i++) {
  522. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  523. newCells[i] = 1;
  524. } else {
  525. newCells[i] = 0;
  526. }
  527. }
  528. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  529. // 40
  530. for (int i = 1; i < 7; i++) {
  531. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  532. newCells[i] = 1;
  533. } else {
  534. newCells[i] = 0;
  535. }
  536. }
  537. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  538. cells[0] = 0;
  539. cells[7] = 0;
  540. for (int i = 1; i < 7; i++) {
  541. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  542. newCells[i] = 1;
  543. } else {
  544. newCells[i] = 0;
  545. }
  546. }
  547. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  548. for (int i = 1; i < 7; i++) {
  549. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  550. newCells[i] = 1;
  551. } else {
  552. newCells[i] = 0;
  553. }
  554. }
  555. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  556. for (int i = 1; i < 7; i++) {
  557. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  558. newCells[i] = 1;
  559. } else {
  560. newCells[i] = 0;
  561. }
  562. }
  563. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  564. for (int i = 1; i < 7; i++) {
  565. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  566. newCells[i] = 1;
  567. } else {
  568. newCells[i] = 0;
  569. }
  570. }
  571. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  572. for (int i = 1; i < 7; i++) {
  573. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  574. newCells[i] = 1;
  575. } else {
  576. newCells[i] = 0;
  577. }
  578. }
  579. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  580. for (int i = 1; i < 7; i++) {
  581. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  582. newCells[i] = 1;
  583. } else {
  584. newCells[i] = 0;
  585. }
  586. }
  587. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  588. for (int i = 1; i < 7; i++) {
  589. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  590. newCells[i] = 1;
  591. } else {
  592. newCells[i] = 0;
  593. }
  594. }
  595. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  596. for (int i = 1; i < 7; i++) {
  597. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  598. newCells[i] = 1;
  599. } else {
  600. newCells[i] = 0;
  601. }
  602. }
  603. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  604. for (int i = 1; i < 7; i++) {
  605. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  606. newCells[i] = 1;
  607. } else {
  608. newCells[i] = 0;
  609. }
  610. }
  611. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  612. // 50
  613. for (int i = 1; i < 7; i++) {
  614. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  615. newCells[i] = 1;
  616. } else {
  617. newCells[i] = 0;
  618. }
  619. }
  620. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  621. cells[0] = 0;
  622. cells[7] = 0;
  623. for (int i = 1; i < 7; i++) {
  624. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  625. newCells[i] = 1;
  626. } else {
  627. newCells[i] = 0;
  628. }
  629. }
  630. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  631. for (int i = 1; i < 7; i++) {
  632. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  633. newCells[i] = 1;
  634. } else {
  635. newCells[i] = 0;
  636. }
  637. }
  638. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  639. for (int i = 1; i < 7; i++) {
  640. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  641. newCells[i] = 1;
  642. } else {
  643. newCells[i] = 0;
  644. }
  645. }
  646. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  647. for (int i = 1; i < 7; i++) {
  648. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  649. newCells[i] = 1;
  650. } else {
  651. newCells[i] = 0;
  652. }
  653. }
  654. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  655. for (int i = 1; i < 7; i++) {
  656. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  657. newCells[i] = 1;
  658. } else {
  659. newCells[i] = 0;
  660. }
  661. }
  662. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  663. for (int i = 1; i < 7; i++) {
  664. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  665. newCells[i] = 1;
  666. } else {
  667. newCells[i] = 0;
  668. }
  669. }
  670. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  671. for (int i = 1; i < 7; i++) {
  672. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  673. newCells[i] = 1;
  674. } else {
  675. newCells[i] = 0;
  676. }
  677. }
  678. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  679. for (int i = 1; i < 7; i++) {
  680. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  681. newCells[i] = 1;
  682. } else {
  683. newCells[i] = 0;
  684. }
  685. }
  686. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  687. for (int i = 1; i < 7; i++) {
  688. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  689. newCells[i] = 1;
  690. } else {
  691. newCells[i] = 0;
  692. }
  693. }
  694. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  695. // 60
  696. for (int i = 1; i < 7; i++) {
  697. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  698. newCells[i] = 1;
  699. } else {
  700. newCells[i] = 0;
  701. }
  702. }
  703. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  704. cells[0] = 0;
  705. cells[7] = 0;
  706. for (int i = 1; i < 7; i++) {
  707. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  708. newCells[i] = 1;
  709. } else {
  710. newCells[i] = 0;
  711. }
  712. }
  713. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  714. for (int i = 1; i < 7; i++) {
  715. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  716. newCells[i] = 1;
  717. } else {
  718. newCells[i] = 0;
  719. }
  720. }
  721. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  722. for (int i = 1; i < 7; i++) {
  723. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  724. newCells[i] = 1;
  725. } else {
  726. newCells[i] = 0;
  727. }
  728. }
  729. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  730. for (int i = 1; i < 7; i++) {
  731. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  732. newCells[i] = 1;
  733. } else {
  734. newCells[i] = 0;
  735. }
  736. }
  737. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  738. for (int i = 1; i < 7; i++) {
  739. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  740. newCells[i] = 1;
  741. } else {
  742. newCells[i] = 0;
  743. }
  744. }
  745. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  746. for (int i = 1; i < 7; i++) {
  747. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  748. newCells[i] = 1;
  749. } else {
  750. newCells[i] = 0;
  751. }
  752. }
  753. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  754. for (int i = 1; i < 7; i++) {
  755. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  756. newCells[i] = 1;
  757. } else {
  758. newCells[i] = 0;
  759. }
  760. }
  761. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  762. for (int i = 1; i < 7; i++) {
  763. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  764. newCells[i] = 1;
  765. } else {
  766. newCells[i] = 0;
  767. }
  768. }
  769. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  770. for (int i = 1; i < 7; i++) {
  771. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  772. newCells[i] = 1;
  773. } else {
  774. newCells[i] = 0;
  775. }
  776. }
  777. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  778. //70
  779. for (int i = 1; i < 7; i++) {
  780. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  781. newCells[i] = 1;
  782. } else {
  783. newCells[i] = 0;
  784. }
  785. }
  786. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  787. cells[0] = 0;
  788. cells[7] = 0;
  789. for (int i = 1; i < 7; i++) {
  790. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  791. newCells[i] = 1;
  792. } else {
  793. newCells[i] = 0;
  794. }
  795. }
  796. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  797. for (int i = 1; i < 7; i++) {
  798. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  799. newCells[i] = 1;
  800. } else {
  801. newCells[i] = 0;
  802. }
  803. }
  804. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  805. for (int i = 1; i < 7; i++) {
  806. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  807. newCells[i] = 1;
  808. } else {
  809. newCells[i] = 0;
  810. }
  811. }
  812. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  813. for (int i = 1; i < 7; i++) {
  814. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  815. newCells[i] = 1;
  816. } else {
  817. newCells[i] = 0;
  818. }
  819. }
  820. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  821. for (int i = 1; i < 7; i++) {
  822. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  823. newCells[i] = 1;
  824. } else {
  825. newCells[i] = 0;
  826. }
  827. }
  828. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  829. for (int i = 1; i < 7; i++) {
  830. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  831. newCells[i] = 1;
  832. } else {
  833. newCells[i] = 0;
  834. }
  835. }
  836. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  837. for (int i = 1; i < 7; i++) {
  838. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  839. newCells[i] = 1;
  840. } else {
  841. newCells[i] = 0;
  842. }
  843. }
  844. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  845. for (int i = 1; i < 7; i++) {
  846. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  847. newCells[i] = 1;
  848. } else {
  849. newCells[i] = 0;
  850. }
  851. }
  852. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  853. for (int i = 1; i < 7; i++) {
  854. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  855. newCells[i] = 1;
  856. } else {
  857. newCells[i] = 0;
  858. }
  859. }
  860. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  861. // 80
  862. for (int i = 1; i < 7; i++) {
  863. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  864. newCells[i] = 1;
  865. } else {
  866. newCells[i] = 0;
  867. }
  868. }
  869. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  870. cells[0] = 0;
  871. cells[7] = 0;
  872. for (int i = 1; i < 7; i++) {
  873. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  874. newCells[i] = 1;
  875. } else {
  876. newCells[i] = 0;
  877. }
  878. }
  879. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  880. for (int i = 1; i < 7; i++) {
  881. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  882. newCells[i] = 1;
  883. } else {
  884. newCells[i] = 0;
  885. }
  886. }
  887. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  888. for (int i = 1; i < 7; i++) {
  889. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  890. newCells[i] = 1;
  891. } else {
  892. newCells[i] = 0;
  893. }
  894. }
  895. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  896. for (int i = 1; i < 7; i++) {
  897. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  898. newCells[i] = 1;
  899. } else {
  900. newCells[i] = 0;
  901. }
  902. }
  903. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  904. for (int i = 1; i < 7; i++) {
  905. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  906. newCells[i] = 1;
  907. } else {
  908. newCells[i] = 0;
  909. }
  910. }
  911. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  912. for (int i = 1; i < 7; i++) {
  913. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  914. newCells[i] = 1;
  915. } else {
  916. newCells[i] = 0;
  917. }
  918. }
  919. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  920. for (int i = 1; i < 7; i++) {
  921. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  922. newCells[i] = 1;
  923. } else {
  924. newCells[i] = 0;
  925. }
  926. }
  927. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  928. for (int i = 1; i < 7; i++) {
  929. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  930. newCells[i] = 1;
  931. } else {
  932. newCells[i] = 0;
  933. }
  934. }
  935. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  936. for (int i = 1; i < 7; i++) {
  937. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  938. newCells[i] = 1;
  939. } else {
  940. newCells[i] = 0;
  941. }
  942. }
  943. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  944. // 90
  945. for (int i = 1; i < 7; i++) {
  946. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  947. newCells[i] = 1;
  948. } else {
  949. newCells[i] = 0;
  950. }
  951. }
  952. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  953. cells[0] = 0;
  954. cells[7] = 0;
  955. for (int i = 1; i < 7; i++) {
  956. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  957. newCells[i] = 1;
  958. } else {
  959. newCells[i] = 0;
  960. }
  961. }
  962. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  963. for (int i = 1; i < 7; i++) {
  964. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  965. newCells[i] = 1;
  966. } else {
  967. newCells[i] = 0;
  968. }
  969. }
  970. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  971. for (int i = 1; i < 7; i++) {
  972. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  973. newCells[i] = 1;
  974. } else {
  975. newCells[i] = 0;
  976. }
  977. }
  978. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  979. for (int i = 1; i < 7; i++) {
  980. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  981. newCells[i] = 1;
  982. } else {
  983. newCells[i] = 0;
  984. }
  985. }
  986. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  987. for (int i = 1; i < 7; i++) {
  988. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  989. newCells[i] = 1;
  990. } else {
  991. newCells[i] = 0;
  992. }
  993. }
  994. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  995. for (int i = 1; i < 7; i++) {
  996. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  997. newCells[i] = 1;
  998. } else {
  999. newCells[i] = 0;
  1000. }
  1001. }
  1002. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1003. for (int i = 1; i < 7; i++) {
  1004. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1005. newCells[i] = 1;
  1006. } else {
  1007. newCells[i] = 0;
  1008. }
  1009. }
  1010. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1011. for (int i = 1; i < 7; i++) {
  1012. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1013. newCells[i] = 1;
  1014. } else {
  1015. newCells[i] = 0;
  1016. }
  1017. }
  1018. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1019. for (int i = 1; i < 7; i++) {
  1020. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1021. newCells[i] = 1;
  1022. } else {
  1023. newCells[i] = 0;
  1024. }
  1025. }
  1026. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1027.  
  1028. //100
  1029. N-=100;
  1030. }
  1031. while (N > 10) {
  1032. for (int i = 1; i < 7; i++) {
  1033. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1034. newCells[i] = 1;
  1035. } else {
  1036. newCells[i] = 0;
  1037. }
  1038. }
  1039. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1040. cells[0] = 0;
  1041. cells[7] = 0;
  1042. for (int i = 1; i < 7; i++) {
  1043. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1044. newCells[i] = 1;
  1045. } else {
  1046. newCells[i] = 0;
  1047. }
  1048. }
  1049. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1050. for (int i = 1; i < 7; i++) {
  1051. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1052. newCells[i] = 1;
  1053. } else {
  1054. newCells[i] = 0;
  1055. }
  1056. }
  1057. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1058. for (int i = 1; i < 7; i++) {
  1059. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1060. newCells[i] = 1;
  1061. } else {
  1062. newCells[i] = 0;
  1063. }
  1064. }
  1065. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1066. for (int i = 1; i < 7; i++) {
  1067. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1068. newCells[i] = 1;
  1069. } else {
  1070. newCells[i] = 0;
  1071. }
  1072. }
  1073. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1074. for (int i = 1; i < 7; i++) {
  1075. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1076. newCells[i] = 1;
  1077. } else {
  1078. newCells[i] = 0;
  1079. }
  1080. }
  1081. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1082. for (int i = 1; i < 7; i++) {
  1083. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1084. newCells[i] = 1;
  1085. } else {
  1086. newCells[i] = 0;
  1087. }
  1088. }
  1089. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1090. for (int i = 1; i < 7; i++) {
  1091. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1092. newCells[i] = 1;
  1093. } else {
  1094. newCells[i] = 0;
  1095. }
  1096. }
  1097. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1098. for (int i = 1; i < 7; i++) {
  1099. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1100. newCells[i] = 1;
  1101. } else {
  1102. newCells[i] = 0;
  1103. }
  1104. }
  1105. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1106. for (int i = 1; i < 7; i++) {
  1107. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1108. newCells[i] = 1;
  1109. } else {
  1110. newCells[i] = 0;
  1111. }
  1112. }
  1113. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1114. N-=10;
  1115. }
  1116. while (N > 0) {
  1117. // ----------------------------------
  1118. for (int i = 1; i < 7; i++) {
  1119. if (cells[i - 1] == cells[i + 1]) { // both empty or vacant
  1120. newCells[i] = 1;
  1121. } else {
  1122. newCells[i] = 0;
  1123. }
  1124. }
  1125. for (int i = 1; i < 7; i++) cells[i] = newCells[i];
  1126. cells[0] = 0;
  1127. cells[7] = 0;
  1128. // ----------------------------------
  1129. N--;
  1130. }
  1131. return cells;
  1132. }
  1133. }
Add Comment
Please, Sign In to add comment