Advertisement
badunius

tile combinations

Jul 21st, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var list = new Set()
  2.  
  3. /* Variant:
  4.   0--1
  5.   |  |
  6.   3--2
  7. */
  8.  
  9. function flipHor(variant) {
  10.   return [
  11.     variant[1],
  12.     variant[0],
  13.     variant[3],
  14.     variant[2],
  15.   ]
  16. }
  17.  
  18. function flipVer(variant) {
  19.   return [
  20.     variant[3],
  21.     variant[2],
  22.     variant[1],
  23.     variant[0],
  24.   ]
  25. }
  26.  
  27. function rotVar(variant, rot = 0) {
  28.   return variant.map((e, i) => variant[(i + rot) % variant.length])
  29. }
  30.  
  31. function hash(variant) {
  32.   return variant.join('-')
  33. }
  34.  
  35. function inList(variant, {flipH = false, flipV = false, rotate = false}) {
  36.   const orig =  [variant]
  37.  
  38.   const rot = rotate
  39.     ? orig.reduce((prev, curr) => ([
  40.         ...prev,
  41.         curr,
  42.         rotVar(curr, 1),
  43.         rotVar(curr, 2),
  44.         rotVar(curr, 3),
  45.       ]), [])
  46.     : [...orig]
  47.  
  48.   const flip = flipH
  49.     ? rot.reduce((prev, curr) => ([...prev, curr, flipHor(curr)]), [])
  50.     : [...rot]
  51.  
  52.   const opts = flipV
  53.     ? flip.reduce((prev, curr) => ([...prev, curr, flipVer(curr)]), [])
  54.     : [...flip]
  55.    
  56.   return opts.map(e => hash(e)).some(e => list.has(e))
  57. }
  58.  
  59. function combine(src = [], vars = [], depth = 0) {
  60.   depth = depth - 1
  61.   const res = [...src]
  62.     let out = []
  63.     vars.forEach(v => {
  64.     const upd = res.map(r => [...r, v])
  65.         out = res.length
  66.       ? [...out, ...upd]
  67.       : [...out, [v]]
  68.     })
  69.   return (depth > 0)
  70.     ? combine(out, vars, depth)
  71.     : out
  72. }
  73.  
  74. function iterate(depth = 4, vars = [], options = {}) {
  75.   list = new Set()
  76.   const res = combine([], vars, depth)
  77.   res.map(v => v.reverse()).forEach(v => {
  78.     if (!inList(v, options)) {
  79.       list.add(hash(v))
  80.     }  
  81.   })
  82.  
  83.   return list
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement