<commit_before>//
//  main.swift
//  threadword
//
//  Created by <NAME> on 6/18/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

let parser = GBCommandLineParser()
parser.registerOption("prefix", shortcut: 112 /* p */, requirement: GBValueRequired)
//parser.registerOption("file", shortcut: 102 /* f */, requirement: GBValueRequired)

var options = Dictionary<String, AnyObject>()
var lines = Array<String>()

func _parseOption(flags: GBParseFlags, argument: String?, value: AnyObject?, stop: CMutablePointer<ObjCBool>?) {
	switch flags {
	case GBParseFlagOption:
		options[argument!] = value
	case GBParseFlagArgument:
		if let strValue: String = value? as? NSString {
			if (countElements(strValue) > 0) {
				lines += strValue
			}
		}
	case GBParseFlagMissingValue:
		println("Error: option \(argument) requires a value")
		exit(2)
	case GBParseFlagUnknownOption:
		println("Error: uknown option \(argument)")
		exit(2)
	default:
		println("Error: this should never happen - unknown GBParseFlags \(flags)")
		exit(1)
	}
}

parser.parseOptionsUsingDefaultArgumentsWithBlock(_parseOption)

println("Solving puzzle:")
for line in lines {
	println(" \(line)")
}

let puzzle = JOThreadWordsPuzzle(level: lines)

let allWords = puzzle.solve()
var solution: String[]
let prefix: String? = options["prefix"]? as? String
let filePath: String? = options["file"]? as? String
var fileWords: String[]

/*if filePath != nil {
	let fileData: String? = NSString.stringWithContentsOfFile(filePath) as? String
	if fileData == nil {
		println("The specified file did not exist!")
		exit(3)
	} else {
		fileWords = fileData!.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
	}
}*/

if prefix != nil || filePath != nil {
	solution = allWords.filter() { word -> Bool in
		if prefix != nil && !word.bridgeToObjectiveC().hasPrefix(prefix) {
			return false
		}

		// Never runs, is disabled
		if fileWords != nil && JOUtil.isFound(JOUtil.binarySearchArray(fileWords, forObject:word)) {
			return false
		}<commit_msg>Add --all to show all possible combinations.<commit_after>//
//  main.swift
//  threadword
//
//  Created by <NAME> on 6/18/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import Foundation

let parser = GBCommandLineParser()
parser.registerOption("prefix", shortcut: 112 /* p */, requirement: GBValueRequired)
//parser.registerOption("file", shortcut: 102 /* f */, requirement: GBValueRequired)
parser.registerOption("all", shortcut: 61 /* a */, requirement: GBValueNone)

var options = Dictionary<String, AnyObject>()
var lines = Array<String>()

func _parseOption(flags: GBParseFlags, argument: String?, value: AnyObject?, stop: CMutablePointer<ObjCBool>?) {
	switch flags {
	case GBParseFlagOption:
		options[argument!] = value
	case GBParseFlagArgument:
		if let strValue: String = value? as? NSString {
			if (countElements(strValue) > 0) {
				lines += strValue
			}
		}
	case GBParseFlagMissingValue:
		println("Error: option \(argument) requires a value")
		exit(2)
	case GBParseFlagUnknownOption:
		println("Error: uknown option \(argument)")
		exit(2)
	default:
		println("Error: this should never happen - unknown GBParseFlags \(flags)")
		exit(1)
	}
}

parser.parseOptionsUsingDefaultArgumentsWithBlock(_parseOption)

if lines.count <= 1 {
	println("Error: you must give a puzzle to solve!")
	exit(2)
}

println("Solving puzzle:")
for line in lines {
	println(" \(line)")
}

let puzzle = JOThreadWordsPuzzle(level: lines)

let allWords = puzzle.solve()
var solution: String[]
let prefix: String? = options["prefix"]? as? String
let showAllWords: Bool = (options["all"] != nil)
let filePath: String? = options["file"]? as? String
var fileWords: String[]

/*if filePath != nil {
	let fileData: String? = NSString.stringWithContentsOfFile(filePath) as? String
	if fileData == nil {
		println("The specified file did not exist!")
		exit(3)
	} else {
		fileWords = fileData!.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
	}
}*/

if showAllWords { // --all
	solution = allWords
} else if prefix != nil || filePath != nil { // --prefix and/or --file
	solution = allWords.filter() { word -> Bool in
		if prefix != nil && !word.bridgeToObjectiveC().hasPrefix(prefix) {
			return false
		}

		// Never runs, is disabled
		if fileWords != nil && !JOUtil.isFound(JOUtil.binarySearchArray(fileWords, forObject:word)) {
			return false
		}
{'fraction_non_alphanumeric': 0.13455931823278763, 'fraction_numerical': 0.009867683337071092, 'mean_word_length': 4.62421185372005, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5592575', 'n_tokens_mistral': 1501, 'n_tokens_neox': 1370, 'n_words': 392}