Last active
October 9, 2015 21:26
-
-
Save proxpero/221ba7c46b6f5b5a9706 to your computer and use it in GitHub Desktop.
Project Euler problem 90 in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Cube digit pairs | |
//: [Problem 90](https://projecteuler.net/problem=90) | |
//: Xcode 7.0, Swift 2.0 | |
/*: | |
Each of the six faces on a cube has a different digit (0 to 9) written on it; the same is done to a second cube. By placing the two cubes side-by-side in different positions we can form a variety of 2-digit numbers. | |
For example, the square number 64 could be formed: | |
 | |
In fact, by carefully choosing the digits on both cubes it is possible to display all of the square numbers below one-hundred: 01, 04, 09, 16, 25, 36, 49, 64, and 81. | |
For example, one way this can be achieved is by placing {0, 5, 6, 7, 8, 9} on one cube and {1, 2, 3, 4, 8, 9} on the other cube. | |
However, for this problem we shall allow the 6 or 9 to be turned upside-down so that an arrangement like {0, 5, 6, 7, 8, 9} and {1, 2, 3, 4, 6, 7} allows for all nine square numbers to be displayed; otherwise it would be impossible to obtain 09. | |
In determining a distinct arrangement we are interested in the digits on each cube, not the order. | |
{1, 2, 3, 4, 5, 6} is equivalent to {3, 6, 4, 1, 2, 5} | |
{1, 2, 3, 4, 5, 6} is distinct from {1, 2, 3, 4, 5, 9} | |
But because we are allowing 6 and 9 to be reversed, the two distinct sets in the last example both represent the extended set {1, 2, 3, 4, 5, 6, 9} for the purpose of forming 2-digit numbers. | |
How many distinct arrangements of the two cubes allow for all of the square numbers to be displayed? | |
*/ | |
import Foundation | |
extension Array { | |
func combinations(n: Int, prefix: [Element] = [], start: Index = 0) -> [[Element]] { | |
guard n > 0 else { return [prefix] } | |
guard start < self.count else { return [] } | |
let first = self[start] | |
return self.combinations(n-1, prefix: prefix + [first] , start: start+1) + self.combinations(n, prefix: prefix, start: start+1) | |
} | |
} | |
// 9 and 6 are equivalent so replace 9s with 6s and sort the tuples | |
let squareNumbers = [(0,1), (0,4), (0,6), (1,6), (2,5), (3,6), (4,6), (8,1)] | |
let combinations = [0, 1, 2, 3, 4, 5, 6, 7, 8, 6].combinations(6) | |
var distinctArrangements = 0 | |
for (n, cube1) in combinations.enumerate() { | |
c2: for cube2 in combinations[n+1..<combinations.count] { | |
var success = true | |
for (digit1, digit2) in squareNumbers { | |
if !((cube1.contains(digit1) && cube2.contains(digit2)) || | |
(cube1.contains(digit2) && cube2.contains(digit1))) { | |
success = false | |
continue c2 | |
} | |
} | |
if success { distinctArrangements++ } | |
} | |
} | |
// this result has been confirmed by projecteuler.net |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment