Skip to content

Instantly share code, notes, and snippets.

@Xmerr
Created December 6, 2016 13:57
Show Gist options
  • Save Xmerr/e7e9f29ed9a4f791bae7a890751c7d94 to your computer and use it in GitHub Desktop.
Save Xmerr/e7e9f29ed9a4f791bae7a890751c7d94 to your computer and use it in GitHub Desktop.
For Reddit Easy Challenge 294: Rack Management 1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Rack_Management_1
{
class Program
{
static Tuple<string, string>[] scrabbleLetters =
{
Tuple.Create("ladilmy", "daily"),
Tuple.Create("eerriin", "eerie"),
Tuple.Create("orrpgma", "program"),
Tuple.Create("orppgma", "program"),
Tuple.Create("pizza??", "pizzazz"),
Tuple.Create("piizza?", "pizzazz"),
Tuple.Create("a??????", "program"),
Tuple.Create("b??????", "program")
};
static string[] longestLetters =
{
"dcthoyueorza",
"uruqrnytrois",
"rryqeiaegicgeo??",
"udosjanyuiuebr??",
"vaakojeaietg????????"
};
static string[] highestPointLetters =
{
"dcthoyueorza",
"uruqrnytrois",
"rryqeiaegicgeo??",
"udosjanyuiuebr??",
"vaakojeaietg????????"
};
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());
}
//Scrabble
foreach (Tuple<string, string> row in scrabbleLetters)
Console.WriteLine(row.Item1 + ", " + row.Item2 + " -> " + Scrabble(row.Item1, row.Item2));
Console.ReadKey();
Console.Clear();
 
//Longest
foreach (string letterList in longestLetters)
Console.WriteLine(letterList + " -> " + Longest(letterList));
Console.ReadKey();
Console.Clear();
//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 = "";
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;
return true;
}
/// <summary>
/// Returns the longest possible word you can make using the passed in letters
/// </summary>
/// <param name="letters">
/// Letters you have on your rack,
/// ? is a wild tile
/// </param>
static string Longest(string letters)
{
foreach(string word in dictionary.OrderByDescending(x => x.Length))
if (Scrabble(letters, word))
return word;
return null;
}
static Tuple<string, int> HighestValue(string letters)
{
Tuple<string, int> best = new Tuple<string, int>("", 0);
foreach (string word in dictionary.Where(x => x.GetValue() > best.Item2).OrderByDescending(x => x.GetValue()))
if (Scrabble(letters, word) && usedLetters.GetValue() > best.Item2)
best = new Tuple<string, int>(usedLetters, 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 }
};
public static int GetValue(this string word)
{
int value = 0;
foreach (char letter in word)
value += pointValues[letter];
return value;
}
}
}
@Xmerr
Copy link
Author

Xmerr commented Dec 6, 2016

For Reddit Easy Challenge 294

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment