PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/test/bench/meteor-contest.go

https://bitbucket.org/xenolinguist/go
Go | 665 lines | 458 code | 51 blank | 156 comment | 198 complexity | afd143afe94e1b5166d8538950faef24 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. Redistribution and use in source and binary forms, with or without
  3. modification, are permitted provided that the following conditions are met:
  4. * Redistributions of source code must retain the above copyright
  5. notice, this list of conditions and the following disclaimer.
  6. * Redistributions in binary form must reproduce the above copyright
  7. notice, this list of conditions and the following disclaimer in the
  8. documentation and/or other materials provided with the distribution.
  9. * Neither the name of "The Computer Language Benchmarks Game" nor the
  10. name of "The Computer Language Shootout Benchmarks" nor the names of
  11. its contributors may be used to endorse or promote products derived
  12. from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /* The Computer Language Benchmarks Game
  26. * http://shootout.alioth.debian.org/
  27. *
  28. * contributed by The Go Authors.
  29. * based on meteor-contest.c by Christian Vosteen
  30. */
  31. package main
  32. import (
  33. "flag"
  34. "fmt"
  35. )
  36. var max_solutions = flag.Int("n", 2100, "maximum number of solutions")
  37. func boolInt(b bool) int8 {
  38. if b {
  39. return 1
  40. }
  41. return 0
  42. }
  43. /* The board is a 50 cell hexagonal pattern. For . . . . .
  44. * maximum speed the board will be implemented as . . . . .
  45. * 50 bits, which will fit into a 64 bit long long . . . . .
  46. * int. . . . . .
  47. * . . . . .
  48. * I will represent 0's as empty cells and 1's . . . . .
  49. * as full cells. . . . . .
  50. * . . . . .
  51. * . . . . .
  52. * . . . . .
  53. */
  54. var board uint64 = 0xFFFC000000000000
  55. /* The puzzle pieces must be specified by the path followed
  56. * from one end to the other along 12 hexagonal directions.
  57. *
  58. * Piece 0 Piece 1 Piece 2 Piece 3 Piece 4
  59. *
  60. * O O O O O O O O O O O O O O O
  61. * O O O O O O O
  62. * O O O
  63. *
  64. * Piece 5 Piece 6 Piece 7 Piece 8 Piece 9
  65. *
  66. * O O O O O O O O O O O O O
  67. * O O O O O O O O O
  68. * O O O
  69. *
  70. * I had to make it 12 directions because I wanted all of the
  71. * piece definitions to fit into the same size arrays. It is
  72. * not possible to define piece 4 in terms of the 6 cardinal
  73. * directions in 4 moves.
  74. */
  75. const (
  76. E = iota
  77. ESE
  78. SE
  79. S
  80. SW
  81. WSW
  82. W
  83. WNW
  84. NW
  85. N
  86. NE
  87. ENE
  88. PIVOT
  89. )
  90. var piece_def = [10][4]int8{
  91. [4]int8{E, E, E, SE},
  92. [4]int8{SE, E, NE, E},
  93. [4]int8{E, E, SE, SW},
  94. [4]int8{E, E, SW, SE},
  95. [4]int8{SE, E, NE, S},
  96. [4]int8{E, E, SW, E},
  97. [4]int8{E, SE, SE, NE},
  98. [4]int8{E, SE, SE, W},
  99. [4]int8{E, SE, E, E},
  100. [4]int8{E, E, E, SW},
  101. }
  102. /* To minimize the amount of work done in the recursive solve function below,
  103. * I'm going to allocate enough space for all legal rotations of each piece
  104. * at each position on the board. That's 10 pieces x 50 board positions x
  105. * 12 rotations. However, not all 12 rotations will fit on every cell, so
  106. * I'll have to keep count of the actual number that do.
  107. * The pieces are going to be unsigned long long ints just like the board so
  108. * they can be bitwise-anded with the board to determine if they fit.
  109. * I'm also going to record the next possible open cell for each piece and
  110. * location to reduce the burden on the solve function.
  111. */
  112. var (
  113. pieces [10][50][12]uint64
  114. piece_counts [10][50]int
  115. next_cell [10][50][12]int8
  116. )
  117. /* Returns the direction rotated 60 degrees clockwise */
  118. func rotate(dir int8) int8 { return (dir + 2) % PIVOT }
  119. /* Returns the direction flipped on the horizontal axis */
  120. func flip(dir int8) int8 { return (PIVOT - dir) % PIVOT }
  121. /* Returns the new cell index from the specified cell in the
  122. * specified direction. The index is only valid if the
  123. * starting cell and direction have been checked by the
  124. * out_of_bounds function first.
  125. */
  126. func shift(cell, dir int8) int8 {
  127. switch dir {
  128. case E:
  129. return cell + 1
  130. case ESE:
  131. if ((cell / 5) % 2) != 0 {
  132. return cell + 7
  133. } else {
  134. return cell + 6
  135. }
  136. case SE:
  137. if ((cell / 5) % 2) != 0 {
  138. return cell + 6
  139. } else {
  140. return cell + 5
  141. }
  142. case S:
  143. return cell + 10
  144. case SW:
  145. if ((cell / 5) % 2) != 0 {
  146. return cell + 5
  147. } else {
  148. return cell + 4
  149. }
  150. case WSW:
  151. if ((cell / 5) % 2) != 0 {
  152. return cell + 4
  153. } else {
  154. return cell + 3
  155. }
  156. case W:
  157. return cell - 1
  158. case WNW:
  159. if ((cell / 5) % 2) != 0 {
  160. return cell - 6
  161. } else {
  162. return cell - 7
  163. }
  164. case NW:
  165. if ((cell / 5) % 2) != 0 {
  166. return cell - 5
  167. } else {
  168. return cell - 6
  169. }
  170. case N:
  171. return cell - 10
  172. case NE:
  173. if ((cell / 5) % 2) != 0 {
  174. return cell - 4
  175. } else {
  176. return cell - 5
  177. }
  178. case ENE:
  179. if ((cell / 5) % 2) != 0 {
  180. return cell - 3
  181. } else {
  182. return cell - 4
  183. }
  184. }
  185. return cell
  186. }
  187. /* Returns wether the specified cell and direction will land outside
  188. * of the board. Used to determine if a piece is at a legal board
  189. * location or not.
  190. */
  191. func out_of_bounds(cell, dir int8) bool {
  192. switch dir {
  193. case E:
  194. return cell%5 == 4
  195. case ESE:
  196. i := cell % 10
  197. return i == 4 || i == 8 || i == 9 || cell >= 45
  198. case SE:
  199. return cell%10 == 9 || cell >= 45
  200. case S:
  201. return cell >= 40
  202. case SW:
  203. return cell%10 == 0 || cell >= 45
  204. case WSW:
  205. i := cell % 10
  206. return i == 0 || i == 1 || i == 5 || cell >= 45
  207. case W:
  208. return cell%5 == 0
  209. case WNW:
  210. i := cell % 10
  211. return i == 0 || i == 1 || i == 5 || cell < 5
  212. case NW:
  213. return cell%10 == 0 || cell < 5
  214. case N:
  215. return cell < 10
  216. case NE:
  217. return cell%10 == 9 || cell < 5
  218. case ENE:
  219. i := cell % 10
  220. return i == 4 || i == 8 || i == 9 || cell < 5
  221. }
  222. return false
  223. }
  224. /* Rotate a piece 60 degrees clockwise */
  225. func rotate_piece(piece int) {
  226. for i := 0; i < 4; i++ {
  227. piece_def[piece][i] = rotate(piece_def[piece][i])
  228. }
  229. }
  230. /* Flip a piece along the horizontal axis */
  231. func flip_piece(piece int) {
  232. for i := 0; i < 4; i++ {
  233. piece_def[piece][i] = flip(piece_def[piece][i])
  234. }
  235. }
  236. /* Convenience function to quickly calculate all of the indices for a piece */
  237. func calc_cell_indices(cell []int8, piece int, index int8) {
  238. cell[0] = index
  239. for i := 1; i < 5; i++ {
  240. cell[i] = shift(cell[i-1], piece_def[piece][i-1])
  241. }
  242. }
  243. /* Convenience function to quickly calculate if a piece fits on the board */
  244. func cells_fit_on_board(cell []int8, piece int) bool {
  245. return !out_of_bounds(cell[0], piece_def[piece][0]) &&
  246. !out_of_bounds(cell[1], piece_def[piece][1]) &&
  247. !out_of_bounds(cell[2], piece_def[piece][2]) &&
  248. !out_of_bounds(cell[3], piece_def[piece][3])
  249. }
  250. /* Returns the lowest index of the cells of a piece.
  251. * I use the lowest index that a piece occupies as the index for looking up
  252. * the piece in the solve function.
  253. */
  254. func minimum_of_cells(cell []int8) int8 {
  255. minimum := cell[0]
  256. for i := 1; i < 5; i++ {
  257. if cell[i] < minimum {
  258. minimum = cell[i]
  259. }
  260. }
  261. return minimum
  262. }
  263. /* Calculate the lowest possible open cell if the piece is placed on the board.
  264. * Used to later reduce the amount of time searching for open cells in the
  265. * solve function.
  266. */
  267. func first_empty_cell(cell []int8, minimum int8) int8 {
  268. first_empty := minimum
  269. for first_empty == cell[0] || first_empty == cell[1] ||
  270. first_empty == cell[2] || first_empty == cell[3] ||
  271. first_empty == cell[4] {
  272. first_empty++
  273. }
  274. return first_empty
  275. }
  276. /* Generate the unsigned long long int that will later be anded with the
  277. * board to determine if it fits.
  278. */
  279. func bitmask_from_cells(cell []int8) uint64 {
  280. var piece_mask uint64
  281. for i := 0; i < 5; i++ {
  282. piece_mask |= 1 << uint(cell[i])
  283. }
  284. return piece_mask
  285. }
  286. /* Record the piece and other important information in arrays that will
  287. * later be used by the solve function.
  288. */
  289. func record_piece(piece int, minimum int8, first_empty int8, piece_mask uint64) {
  290. pieces[piece][minimum][piece_counts[piece][minimum]] = piece_mask
  291. next_cell[piece][minimum][piece_counts[piece][minimum]] = first_empty
  292. piece_counts[piece][minimum]++
  293. }
  294. /* Fill the entire board going cell by cell. If any cells are "trapped"
  295. * they will be left alone.
  296. */
  297. func fill_contiguous_space(board []int8, index int8) {
  298. if board[index] == 1 {
  299. return
  300. }
  301. board[index] = 1
  302. if !out_of_bounds(index, E) {
  303. fill_contiguous_space(board, shift(index, E))
  304. }
  305. if !out_of_bounds(index, SE) {
  306. fill_contiguous_space(board, shift(index, SE))
  307. }
  308. if !out_of_bounds(index, SW) {
  309. fill_contiguous_space(board, shift(index, SW))
  310. }
  311. if !out_of_bounds(index, W) {
  312. fill_contiguous_space(board, shift(index, W))
  313. }
  314. if !out_of_bounds(index, NW) {
  315. fill_contiguous_space(board, shift(index, NW))
  316. }
  317. if !out_of_bounds(index, NE) {
  318. fill_contiguous_space(board, shift(index, NE))
  319. }
  320. }
  321. /* To thin the number of pieces, I calculate if any of them trap any empty
  322. * cells at the edges. There are only a handful of exceptions where the
  323. * the board can be solved with the trapped cells. For example: piece 8 can
  324. * trap 5 cells in the corner, but piece 3 can fit in those cells, or piece 0
  325. * can split the board in half where both halves are viable.
  326. */
  327. func has_island(cell []int8, piece int) bool {
  328. temp_board := make([]int8, 50)
  329. var i int
  330. for i = 0; i < 5; i++ {
  331. temp_board[cell[i]] = 1
  332. }
  333. i = 49
  334. for temp_board[i] == 1 {
  335. i--
  336. }
  337. fill_contiguous_space(temp_board, int8(i))
  338. c := 0
  339. for i = 0; i < 50; i++ {
  340. if temp_board[i] == 0 {
  341. c++
  342. }
  343. }
  344. if c == 0 || (c == 5 && piece == 8) || (c == 40 && piece == 8) ||
  345. (c%5 == 0 && piece == 0) {
  346. return false
  347. }
  348. return true
  349. }
  350. /* Calculate all six rotations of the specified piece at the specified index.
  351. * We calculate only half of piece 3's rotations. This is because any solution
  352. * found has an identical solution rotated 180 degrees. Thus we can reduce the
  353. * number of attempted pieces in the solve algorithm by not including the 180-
  354. * degree-rotated pieces of ONE of the pieces. I chose piece 3 because it gave
  355. * me the best time ;)
  356. */
  357. func calc_six_rotations(piece, index int) {
  358. cell := make([]int8, 5)
  359. for rotation := 0; rotation < 6; rotation++ {
  360. if piece != 3 || rotation < 3 {
  361. calc_cell_indices(cell, piece, int8(index))
  362. if cells_fit_on_board(cell, piece) && !has_island(cell, piece) {
  363. minimum := minimum_of_cells(cell)
  364. first_empty := first_empty_cell(cell, minimum)
  365. piece_mask := bitmask_from_cells(cell)
  366. record_piece(piece, minimum, first_empty, piece_mask)
  367. }
  368. }
  369. rotate_piece(piece)
  370. }
  371. }
  372. /* Calculate every legal rotation for each piece at each board location. */
  373. func calc_pieces() {
  374. for piece := 0; piece < 10; piece++ {
  375. for index := 0; index < 50; index++ {
  376. calc_six_rotations(piece, index)
  377. flip_piece(piece)
  378. calc_six_rotations(piece, index)
  379. }
  380. }
  381. }
  382. /* Calculate all 32 possible states for a 5-bit row and all rows that will
  383. * create islands that follow any of the 32 possible rows. These pre-
  384. * calculated 5-bit rows will be used to find islands in a partially solved
  385. * board in the solve function.
  386. */
  387. const (
  388. ROW_MASK = 0x1F
  389. TRIPLE_MASK = 0x7FFF
  390. )
  391. var (
  392. all_rows = [32]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
  393. 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  394. }
  395. bad_even_rows [32][32]int8
  396. bad_odd_rows [32][32]int8
  397. bad_even_triple [32768]int8
  398. bad_odd_triple [32768]int8
  399. )
  400. func rows_bad(row1, row2 int8, even bool) int8 {
  401. /* even is referring to row1 */
  402. var row2_shift int8
  403. /* Test for blockages at same index and shifted index */
  404. if even {
  405. row2_shift = ((row2 << 1) & ROW_MASK) | 0x01
  406. } else {
  407. row2_shift = (row2 >> 1) | 0x10
  408. }
  409. block := ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift)
  410. /* Test for groups of 0's */
  411. in_zeroes := false
  412. group_okay := false
  413. for i := uint8(0); i < 5; i++ {
  414. if row1&(1<<i) != 0 {
  415. if in_zeroes {
  416. if !group_okay {
  417. return 1
  418. }
  419. in_zeroes = false
  420. group_okay = false
  421. }
  422. } else {
  423. if !in_zeroes {
  424. in_zeroes = true
  425. }
  426. if (block & (1 << i)) == 0 {
  427. group_okay = true
  428. }
  429. }
  430. }
  431. if in_zeroes {
  432. return boolInt(!group_okay)
  433. }
  434. return 0
  435. }
  436. /* Check for cases where three rows checked sequentially cause a false
  437. * positive. One scenario is when 5 cells may be surrounded where piece 5
  438. * or 7 can fit. The other scenario is when piece 2 creates a hook shape.
  439. */
  440. func triple_is_okay(row1, row2, row3 int, even bool) bool {
  441. if even {
  442. /* There are four cases:
  443. * row1: 00011 00001 11001 10101
  444. * row2: 01011 00101 10001 10001
  445. * row3: 011?? 00110 ????? ?????
  446. */
  447. return ((row1 == 0x03) && (row2 == 0x0B) && ((row3 & 0x1C) == 0x0C)) ||
  448. ((row1 == 0x01) && (row2 == 0x05) && (row3 == 0x06)) ||
  449. ((row1 == 0x19) && (row2 == 0x11)) ||
  450. ((row1 == 0x15) && (row2 == 0x11))
  451. }
  452. /* There are two cases:
  453. * row1: 10011 10101
  454. * row2: 10001 10001
  455. * row3: ????? ?????
  456. */
  457. return ((row1 == 0x13) && (row2 == 0x11)) ||
  458. ((row1 == 0x15) && (row2 == 0x11))
  459. }
  460. func calc_rows() {
  461. for row1 := int8(0); row1 < 32; row1++ {
  462. for row2 := int8(0); row2 < 32; row2++ {
  463. bad_even_rows[row1][row2] = rows_bad(row1, row2, true)
  464. bad_odd_rows[row1][row2] = rows_bad(row1, row2, false)
  465. }
  466. }
  467. for row1 := 0; row1 < 32; row1++ {
  468. for row2 := 0; row2 < 32; row2++ {
  469. for row3 := 0; row3 < 32; row3++ {
  470. result1 := bad_even_rows[row1][row2]
  471. result2 := bad_odd_rows[row2][row3]
  472. if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, true) {
  473. bad_even_triple[row1+(row2*32)+(row3*1024)] = 0
  474. } else {
  475. bad_even_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
  476. }
  477. result1 = bad_odd_rows[row1][row2]
  478. result2 = bad_even_rows[row2][row3]
  479. if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, false) {
  480. bad_odd_triple[row1+(row2*32)+(row3*1024)] = 0
  481. } else {
  482. bad_odd_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
  483. }
  484. }
  485. }
  486. }
  487. }
  488. /* Calculate islands while solving the board.
  489. */
  490. func boardHasIslands(cell int8) int8 {
  491. /* Too low on board, don't bother checking */
  492. if cell >= 40 {
  493. return 0
  494. }
  495. current_triple := (board >> uint((cell/5)*5)) & TRIPLE_MASK
  496. if (cell/5)%2 != 0 {
  497. return bad_odd_triple[current_triple]
  498. }
  499. return bad_even_triple[current_triple]
  500. }
  501. /* The recursive solve algorithm. Try to place each permutation in the upper-
  502. * leftmost empty cell. Mark off available pieces as it goes along.
  503. * Because the board is a bit mask, the piece number and bit mask must be saved
  504. * at each successful piece placement. This data is used to create a 50 char
  505. * array if a solution is found.
  506. */
  507. var (
  508. avail uint16 = 0x03FF
  509. sol_nums [10]int8
  510. sol_masks [10]uint64
  511. solutions [2100][50]int8
  512. solution_count = 0
  513. )
  514. func record_solution() {
  515. for sol_no := 0; sol_no < 10; sol_no++ {
  516. sol_mask := sol_masks[sol_no]
  517. for index := 0; index < 50; index++ {
  518. if sol_mask&1 == 1 {
  519. solutions[solution_count][index] = sol_nums[sol_no]
  520. /* Board rotated 180 degrees is a solution too! */
  521. solutions[solution_count+1][49-index] = sol_nums[sol_no]
  522. }
  523. sol_mask = sol_mask >> 1
  524. }
  525. }
  526. solution_count += 2
  527. }
  528. func solve(depth, cell int8) {
  529. if solution_count >= *max_solutions {
  530. return
  531. }
  532. for board&(1<<uint(cell)) != 0 {
  533. cell++
  534. }
  535. for piece := int8(0); piece < 10; piece++ {
  536. var piece_no_mask uint16 = 1 << uint(piece)
  537. if avail&piece_no_mask == 0 {
  538. continue
  539. }
  540. avail ^= piece_no_mask
  541. max_rots := piece_counts[piece][cell]
  542. piece_mask := pieces[piece][cell]
  543. for rotation := 0; rotation < max_rots; rotation++ {
  544. if board&piece_mask[rotation] == 0 {
  545. sol_nums[depth] = piece
  546. sol_masks[depth] = piece_mask[rotation]
  547. if depth == 9 {
  548. /* Solution found!!!!!11!!ONE! */
  549. record_solution()
  550. avail ^= piece_no_mask
  551. return
  552. }
  553. board |= piece_mask[rotation]
  554. if boardHasIslands(next_cell[piece][cell][rotation]) == 0 {
  555. solve(depth+1, next_cell[piece][cell][rotation])
  556. }
  557. board ^= piece_mask[rotation]
  558. }
  559. }
  560. avail ^= piece_no_mask
  561. }
  562. }
  563. /* pretty print a board in the specified hexagonal format */
  564. func pretty(b *[50]int8) {
  565. for i := 0; i < 50; i += 10 {
  566. fmt.Printf("%c %c %c %c %c \n %c %c %c %c %c \n", b[i]+'0', b[i+1]+'0',
  567. b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0',
  568. b[i+7]+'0', b[i+8]+'0', b[i+9]+'0')
  569. }
  570. fmt.Printf("\n")
  571. }
  572. /* Find smallest and largest solutions */
  573. func smallest_largest() (smallest, largest *[50]int8) {
  574. smallest = &solutions[0]
  575. largest = &solutions[0]
  576. for i := 1; i < solution_count; i++ {
  577. candidate := &solutions[i]
  578. for j, s := range *smallest {
  579. c := candidate[j]
  580. if c == s {
  581. continue
  582. }
  583. if c < s {
  584. smallest = candidate
  585. }
  586. break
  587. }
  588. for j, s := range *largest {
  589. c := candidate[j]
  590. if c == s {
  591. continue
  592. }
  593. if c > s {
  594. largest = candidate
  595. }
  596. break
  597. }
  598. }
  599. return
  600. }
  601. func main() {
  602. flag.Parse()
  603. calc_pieces()
  604. calc_rows()
  605. solve(0, 0)
  606. fmt.Printf("%d solutions found\n\n", solution_count)
  607. smallest, largest := smallest_largest()
  608. pretty(smallest)
  609. pretty(largest)
  610. }