Skip to content

Instantly share code, notes, and snippets.

@Xmerr
Last active December 9, 2016 14:30
Show Gist options
  • Save Xmerr/051dbd7987b3251c120bb89365ee8432 to your computer and use it in GitHub Desktop.
Save Xmerr/051dbd7987b3251c120bb89365ee8432 to your computer and use it in GitHub Desktop.
For Reddit Hard Challenge 294
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Rack_Management_3
{
class Program
{
static string pile = "sd?zeioao?mluvepesceinfxt?wyiru?ie?giator?t??nuefje?l?odndrotpewlgoobiinysagacaqski?aeh?rbhaervtnl?m";
static string rack = "";
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());
}
int turn = 0;
int score = 0;
while(pile.Length > 0)
{
//Holds the top turn score
//Item1 is score, Item2 is left drawn number
Tuple<int, int> topTurnScore = null;
for(int i = 10 - rack.Length; i > 0; i--)
{
string tempRack = Draw(i, false);
var tempWord = HighestValue(tempRack);
if(topTurnScore == null || tempWord.Item2.GetValue() > topTurnScore.Item1)
topTurnScore = new Tuple<int, int>(tempWord.Item2.GetValue(), i);
}
rack = Draw(topTurnScore.Item2, true);
var word = HighestValue(rack);
Console.WriteLine(word.Item2 + " " + word.Item1);
score += word.Item2.GetValue();
foreach (char c in word.Item2)
rack = rack.Remove(rack.IndexOf(c), 1);
turn++;
}
Console.WriteLine("Final Score: " + score);
Console.ReadKey();
}
static string usedLetters;
/// <summary>
/// Draws a new rack
/// </summary>
static string Draw(int left, bool removeFromPile)
{
string tempPile = pile;
string tempRack = rack;
for (int i = 0; i < left; i++)
{
if(tempPile.Length > 0)
{
tempRack += tempPile[0];
tempPile = tempPile.Substring(1);
}
}
while (tempRack.Length < 10 && tempPile.Length > 0)
{
tempRack += tempPile[tempPile.Length - 1];
tempPile = tempPile.Substring(0, tempPile.Length - 1);
}
if (removeFromPile)
{
pile = tempPile;
Console.Write(left + " ");
}
return tempRack;
}
/// <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, string> HighestValue(string letters)
{
Tuple<string, string> best = new Tuple<string, string>("", "");
foreach (string word in dictionary.OrderByDescending(x => x.GetValue()))
if (Scrabble(letters, word) && usedLetters.GetValue() > best.Item2.GetValue())
best = new Tuple<string, string>(word, usedLetters);
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;
}
}
}
@Xmerr
Copy link
Author

Xmerr commented Dec 9, 2016

Iteration 1: was very basic.

Iteration 2: I had it check every possible hand combination.

Iteration 3: Formatted Output

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