Last active
December 15, 2015 04:19
-
-
Save JamesKhoury/5200737 to your computer and use it in GitHub Desktop.
Code for the question at http://codereview.stackexchange.com/questions/23901/code-jam-alien-language
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
using System.Text.RegularExpressions; | |
namespace CodeJamPractice | |
{ | |
class Program | |
{ | |
private const string BaseFolderPath = @"C:\path\to\CodeJam\"; | |
static void Main(string[] args) | |
{ | |
var inputLines = File.ReadAllLines(BaseFolderPath + @"AlienLanguage\A-large-practice.in"); | |
//GetWordlength / DictionaryLength / Number of testCases | |
string[] ldnValues = inputLines[0].Split(' '); | |
int LettersInWord = int.Parse(ldnValues[0]); | |
int WordsInLanguage = int.Parse(ldnValues[1]); | |
int NumberOfTestCases = int.Parse(ldnValues[2]); | |
// Create a dictionary of (test case number , test case string) so that it can be paralellised and still keep its test number | |
int index = 1; | |
Dictionary<int, string> testCasesWithIndex = inputLines.Skip(1 + WordsInLanguage).Take(NumberOfTestCases).ToDictionary(x => index++, x => x); | |
var resultDict = new Dictionary<int, string>(); | |
string allWords = string.Join("|", inputLines.Skip(1).Take(WordsInLanguage)); | |
testCasesWithIndex.AsParallel().ForAll(kvp => | |
{ | |
int testCaseNumber = kvp.Key; | |
string testCase = kvp.Value; | |
var possibleValues = Regex.Matches(allWords, testCase.Replace("(", "[").Replace(")", "]")); | |
string result = string.Format("Case #{0}: {1}", testCaseNumber, possibleValues.Count); | |
resultDict[kvp.Key] = result; | |
}); | |
string output = string.Join("\n", resultDict.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)); | |
File.WriteAllText(BaseFolderPath + @"AlienLanguage\output.txt", output); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment