Skip to content

Instantly share code, notes, and snippets.

@ff8c00
Created March 10, 2017 16:38
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 ff8c00/bbc577b0087d1864c22ac04054d8171f to your computer and use it in GitHub Desktop.
Save ff8c00/bbc577b0087d1864c22ac04054d8171f to your computer and use it in GitHub Desktop.
Reddit Daily Programmer 294 Easy
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Reddit
{
internal static class Easy
{
internal static readonly int[] Points = new int[]
{
1, 3, 3, 2, 1, 4, 2, 4, 1, 8,
5, 1, 3, 1, 1, 3, 10, 1, 1, 1,
1, 4, 4, 8, 4, 10
};
internal static int Score(string word)
{
int score = 0;
for (int i = 0; i < word.Length; i++)
{
char value = word[i];
if (word[i] != '?')
{
int index = value - 'a';
score = score + Points[index];
}
}
return score;
}
internal static List<string> Words = new List<string>();
internal static void Load(string path)
{
Words.Clear();
var items = File.ReadAllLines(path);
foreach (string item in items.Where(e => e.Length <= 20))
{
Words.Add(item);
}
}
internal static string Find(string rack, string word)
{
char[] input = rack.ToArray();
char[] output = word.ToArray();
for (int i = 0; i < output.Length; i++)
{
bool flag = true;
for (int j = 0; j < input.Length; j++)
{
if (input[j] == output[i])
{
input[j] = ' ';
flag = false;
break;
}
}
if (flag)
{
for (int j = 0; j < input.Length; j++)
{
if (input[j] == '?')
{
output[i] = '?';
input[j] = ' ';
flag = false;
break;
}
}
}
if (flag)
{
return null;
}
}
return new string(output);
}
internal static bool Scrabble(string rack, string word)
{
string value = Find(rack, word);
return value != null;
}
internal static string Longest(string rack)
{
int length = 0;
string word = null;
foreach (string item in Words)
{
string result = Find(rack, item);
if (result != null)
{
int value = result.Length;
if (value > length)
{
length = value;
word = item;
}
}
}
return word;
}
internal static string Highest(string rack)
{
int score = 0;
string word = null;
foreach (string item in Words)
{
string result = Find(rack, item);
if (result != null)
{
int value = Score(result);
if (value > score)
{
score = value;
word = item;
}
}
}
return word;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment