Last active
December 8, 2016 18:44
-
-
Save Xmerr/a49ef1abbaf65a8eff1fbb99d613666e to your computer and use it in GitHub Desktop.
For Reddit Intermediate Challenge 294
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.IO; | |
using System.Linq; | |
namespace Rack_Management_2 | |
{ | |
class Program | |
{ | |
static string[] highestPointLetters = | |
{ | |
"iogsvooely", | |
"seevurtfci", | |
"vepredequi", | |
"umnyeoumcp", | |
"orhvtudmcz", | |
"fyilnprtia", | |
"yleualaaoitoai??????", | |
"afaimznqxtiaar??????", | |
"yrkavtargoenem??????", | |
"gasfreubevuiex??????" | |
}; | |
static List<string> dictionary; | |
static void Main(string[] args) | |
{ | |
dictionary = new List<string>(); | |
using (StreamReader file = new StreamReader("Dictionary.txt")) | |
{ | |
string line; | |
//Each line of this file is a new word | |
while ((line = file.ReadLine()) != null) | |
dictionary.Add(line.Trim()); | |
} | |
//Highest Point Value | |
foreach (string letterList in highestPointLetters) { | |
Tuple<string, int> highest = HighestValue(letterList); | |
Console.WriteLine(letterList + " -> " + highest.Item1 + " at " + highest.Item2); | |
} | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
static string usedLetters; | |
/// <summary> | |
/// Determines if you can make the word given the letters | |
/// </summary> | |
/// <param name="letters"> | |
/// A string of every letter you have, | |
/// ? is a wild tile | |
/// </param> | |
/// <param name="word"> | |
/// Word you wish to create | |
/// </param> | |
static bool Scrabble(string letters, string word) | |
{ | |
usedLetters = ""; | |
word = ReverseString(word); | |
foreach (char c in word) | |
if (letters.Contains(c)) | |
{ | |
letters = letters.Remove(letters.IndexOf(c), 1); | |
usedLetters += c; | |
} | |
else if (letters.Contains('?')) | |
{ | |
letters = letters.Remove(letters.IndexOf('?'), 1); | |
usedLetters += '?'; | |
} | |
else | |
return false; | |
usedLetters = ReverseString(usedLetters); | |
return true; | |
} | |
/// <summary> | |
/// Reverses the passed in string | |
/// </summary> | |
/// <param name="s"> | |
/// String to be reversed | |
/// </param> | |
public static string ReverseString(string s) | |
{ | |
char[] charArray = s.ToCharArray(); | |
Array.Reverse(charArray); | |
return new string(charArray); | |
} | |
/// <summary> | |
/// Returns the highest valued word for the given letters | |
/// Each letter's value is multiplied by it's position in the word | |
/// </summary> | |
/// <param name="letters"> | |
/// Letters to be examined | |
/// </param> | |
static Tuple<string, int> HighestValue(string letters) | |
{ | |
Tuple<string, int> best = new Tuple<string, int>("", 0); | |
foreach (string word in dictionary.OrderByDescending(x => x.GetValue())) | |
if (Scrabble(letters, word) && usedLetters.GetValue() > best.Item2) | |
best = new Tuple<string, int>(word, usedLetters.GetValue()); | |
return best; | |
} | |
} | |
public static class ExtensionMethods | |
{ | |
static Dictionary<char, int> pointValues = new Dictionary<char, int> | |
{ | |
{ 'a', 1 }, | |
{ 'b', 3 }, | |
{ 'c', 3 }, | |
{ 'd', 2 }, | |
{ 'e', 1 }, | |
{ 'f', 4 }, | |
{ 'g', 2 }, | |
{ 'h', 4 }, | |
{ 'i', 1 }, | |
{ 'j', 8 }, | |
{ 'k', 5 }, | |
{ 'l', 1 }, | |
{ 'm', 3 }, | |
{ 'n', 1 }, | |
{ 'o', 1 }, | |
{ 'p', 3 }, | |
{ 'q', 10 }, | |
{ 'r', 1 }, | |
{ 's', 1 }, | |
{ 't', 1 }, | |
{ 'u', 1 }, | |
{ 'v', 4 }, | |
{ 'w', 4 }, | |
{ 'x', 8 }, | |
{ 'y', 4 }, | |
{ 'z', 10 }, | |
{ '?', 0 } | |
}; | |
/// <summary> | |
/// Returns the value of the passed in word | |
/// Each letter's value is multiplied by it's position in the word | |
/// </summary> | |
/// <param name="word"> | |
/// word to be examined | |
/// </param> | |
public static int GetValue(this string word) | |
{ | |
int value = 0; | |
for(int i = 0; i < word.Length; i++) | |
value += pointValues[word[i]] * (i + 1); | |
return value; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For Reddit Intermediate Challenge 294