Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//puzzling.stackexchange.com/posts/54652/
struct Square {
var up: UInt8 = 0
var left: UInt8 = 0
var down: UInt8 = 0
var right: UInt8 = 0
var rotation: Int = 0
}
var n = 0
var shouldBreak = false
let square1 = Square(up: 2, left: 1, down:4, right: 3, rotation: 0)
let square2 = Square(up: 1, left: 2, down:5, right: 4, rotation: 0)
let square3 = Square(up: 6, left: 3, down:2, right: 1, rotation: 0)
let square4 = Square(up: 1, left: 4, down:1, right: 5, rotation: 0)
let square5 = Square(up: 2, left: 6, down:5, right: 3, rotation: 0)
let square6 = Square(up: 6, left: 5, down:2, right: 5, rotation: 0)
var array: [Square] = [square1,square2,square3,square4,square5,square6]
var arrayPermutes: [[Square]] = []
func iterate() {
if array[0].rotation < 3 {
array[0].rotation += 1
} else {
var notFound: Bool = true
for i in 1 ... 5 {
if array[i].rotation < 3 {
notFound = false
array[i].rotation += 1
for z in 0 ... i - 1 {
array[z].rotation = 0
}
break
}
}
if notFound {
changePlaces()
}
}
}
func changePlaces() {
if n < 719 {
n += 1
array.removeAll(keepingCapacity: true)
for i in 0 ... 5 {
array.append(arrayPermutes[n][i])
}
//array = arrayPermutes[n]
} else {
shouldBreak = true
}
print(n)
}
func swapper(_ a: [Square], _ b: Int, _ c: Int) -> [Square] {
var a1 = a
let d = a[b]
a1[b] = a[c]
a1[c] = d
return a1
}
// Citation: https://stackoverflow.com/a/14444037
func permute(_ arr: [Square],_ k: Int) {
var arr1 = arr
for i in k ..< arr1.count {
arr1 = swapper(arr, i, k)
permute(arr1, k + 1);
arr1 = swapper(arr, k, i)
}
if k == arr1.count - 1 {
arrayPermutes.append(arr1)
}
}
permute(array, 0)
func getRotatedSideColor(_ a: Square, _ b: Int) -> UInt8 {
// 0 UP
// 1 LEFT
// 2 DOWN
// 3 RIGHT
let x = (b - a.rotation + 4) % 4
if x == 0 {
return a.up
} else if x == 1 {
return a.left
} else if x == 2 {
return a.down
} else if x == 3 {
return a.right
} else {
print(x)
print("error!")
return 0
}
// Rotation: CounterClockwise
}
var solutionArrays: [[Square]] = []
while !shouldBreak {
if getRotatedSideColor(array[0], 3) == getRotatedSideColor(array[1], 1)
&& getRotatedSideColor(array[1], 3) == getRotatedSideColor(array[2], 1)
&& getRotatedSideColor(array[3], 3) == getRotatedSideColor(array[4], 1)
&& getRotatedSideColor(array[4], 3) == getRotatedSideColor(array[5], 1)
&& getRotatedSideColor(array[0], 2) == getRotatedSideColor(array[3], 0)
&& getRotatedSideColor(array[1], 2) == getRotatedSideColor(array[4], 0)
&& getRotatedSideColor(array[2], 2) == getRotatedSideColor(array[5], 0) {
solutionArrays.append(array)
print(array)
for e in array {
print(e)
}
}
iterate()
}
//print(arrayPermutes)
print("The end!")
print(arrayPermutes.count)
print(solutionArrays)
print(solutionArrays.count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.