Skip to content

Instantly share code, notes, and snippets.

@JamesKhoury
Last active December 15, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesKhoury/5200737 to your computer and use it in GitHub Desktop.
Save JamesKhoury/5200737 to your computer and use it in GitHub Desktop.
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