Created
July 28, 2020 00:02
-
-
Save bolivarbryan/cf9e87898d566a7073ca97caf49039b9 to your computer and use it in GitHub Desktop.
This file contains 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
func countSteps(from value: String, _ target: String) -> Int? { | |
guard | |
value.count == target.count | |
else { return nil } | |
var count = 0 | |
var newValue = value | |
var index = 0 | |
var found = false | |
while found == false { | |
var found2 = false | |
for n in Transformation.allCases { | |
let range = newValue.range(of: n.rawValue) | |
if range != nil { | |
newValue.replaceSubrange(range!, with: n.newValue) | |
count += 1 | |
print(newValue, target, range!.lowerBound.hashValue, range!.upperBound.hashValue, count) | |
if newValue == target { | |
return count | |
} | |
found2 = true | |
} | |
} | |
if found2 == false { | |
found = true | |
} | |
} | |
return count | |
} | |
enum Transformation: String, CaseIterable { | |
case RX | |
case XL | |
var newValue: String { | |
switch self { | |
case .RX: | |
return "RX" | |
case .XL: | |
return "LX" | |
} | |
} | |
} | |
countSteps(from: "XRX", "XXR") | |
countSteps(from: "XLXXXRXX", "XXXRLXXX") | |
countSteps(from: "RXXL", "XRLX") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment