Password Solver Swift Program

Password Solver


//: Playground - noun: a place where people can play

import UIKit

//These are the all possible words in an String array
var words = ["about", "after", "again", "below", "could", "every", "first", "found", "great", "house", "large", "learn", "never", "other", "place", "plant", "point", "right", "small", "sound", "spell", "still", "study", "their", "there", "these", "thing", "think", "three", "water", "where", "which", "world", "would", "write"]

//Put the letters from the collumns here in an String array
let collumns = ["axxxxx", "bxxxxxx", "xxxxxx", "xxxxxx", "xxxxxx"];

//Function that does all the magic. It only requires the position as an Input, because it has access to both String arrays.
func findMatches(position: Int) {
    //Just skip the function if there are no letters in current collumns. Or if is is my placeholder
    if collumns[position].characters.count == 0 || collumns[position] == "xxxxxx" {
        return
    }
    //Create a new String array to save all possible words for the runtime of the function.
    var possible: [String] = []
    
    //for loop to repeat to check each word
    for word in words {
        
        //for loop that checks if the letter in collumn is in required position in the word
        for p in 0..<collumns[position].characters.count {
            
            //add word to possible if it has the current letter from collumn at the right position
            if word[word.startIndex.advancedBy(position)] == collumns[position][collumns[position].startIndex.advancedBy(p)] {
                possible.append(word)
            }
        }
    }
    
    //sets words to all possible words for next execution
    words = possible
}

//runs the magic for the first collumn
findMatches(0)
//this prints all possible words in swift
words
//repeat for all collumns
findMatches(1)
words
findMatches(2)
words
findMatches(3)
words
findMatches(4)
words


Learn More :