-
Notifications
You must be signed in to change notification settings - Fork 12
/
dencoder.d
210 lines (187 loc) · 4.49 KB
/
dencoder.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import std.stdio : write, writeln, File;
import std.conv : to;
import std.outbuffer : OutBuffer;
import std.ascii : isAlpha, isDigit;
import std.algorithm.iteration : filter;
@safe:
static const letters = [
['e', 'E'],
['J', 'N', 'Q', 'j', 'n', 'q'],
['R', 'W', 'X', 'r', 'w', 'x'],
['D', 'S', 'Y', 'd', 's', 'y'],
['F', 'T', 'f', 't'],
['A', 'M', 'a', 'm'],
['C', 'I', 'V', 'c', 'i', 'v'],
['B', 'K', 'U', 'b', 'k', 'u'],
['L', 'O', 'P', 'l', 'o', 'p'],
['G', 'H', 'Z', 'g', 'h', 'z'],
];
static const digitByLetter = buildDigitByLetter();
ubyte[char] buildDigitByLetter()
{
ubyte[char] result;
static foreach (i, row; letters)
{
static foreach (letter; row)
{
result[letter] = cast(ubyte) i;
}
}
return result;
}
struct Key
{
ubyte[] value;
size_t hash;
this(ubyte[] value)
{
this.value = value;
foreach (i, b; value)
{
hash ^= b << (8 * (i % size_t.sizeof));
}
}
void append(ubyte b)
{
auto i = value.length;
value ~= b;
hash ^= b << (8 * (i % size_t.sizeof));
}
size_t toHash() const @safe pure nothrow => hash;
bool opEquals(ref const typeof(this) s) const @safe pure nothrow => this.value == s.value;
}
private Key wordToNumber(string word)
{
ubyte[] result;
foreach (c; word)
{
if (c.isAlpha)
{
result ~= digitByLetter[c];
}
}
return Key(result);
}
private bool lastItemIsDigit(string[] words)
{
return words.length != 0 && words[$ - 1].length == 1 && words[$ - 1][0].isDigit;
}
void printTranslations(string[][Key] dict, ISolutionHandler shandler,
string number, string digits, string[] words)
{
if (digits.length == 0)
{
shandler.put(number, words);
return;
}
auto key = Key(new ubyte[0]);
bool foundWord = false;
foreach (i, c; digits)
{
key.append(cast(ubyte)(c - '0'));
string[]* foundWords = key in dict;
if (foundWords !is null)
{
foundWord = true;
foreach (word; *foundWords)
{
words ~= word;
dict.printTranslations(shandler, number, digits[i + 1 .. $], words);
words = words[0 .. $ - 1];
}
}
}
if (!foundWord && !words.lastItemIsDigit)
{
string digit = [digits[0]];
words ~= digit;
dict.printTranslations(shandler, number, digits[1 .. $], words);
}
}
string[][Key] loadDictionary(string path) @trusted
{
auto file = new File(path);
string[][Key] result;
foreach (line; file.byLine)
{
auto word = line.idup;
auto n = word.wordToNumber;
auto words = n in result;
if (words is null)
{
result[n] = [word];
}
else
{
*words ~= word;
}
}
return result;
}
interface ISolutionHandler
{
void flush();
void put(string number, string[] words);
}
final class Printer : ISolutionHandler
{
private OutBuffer buf = new OutBuffer();
private int counter;
this()
{
buf.reserve(4096);
}
void flush()
{
write(buf.toString());
buf.clear();
}
void put(string number, string[] words)
{
buf.writefln("%s:%-( %s%)", number, words);
counter++;
if (counter >= 100)
{
counter = 0;
flush();
}
}
}
final class Counter : ISolutionHandler
{
private int count;
void flush() => writeln(count);
void put(string number, string[] words)
{
count++;
}
}
ISolutionHandler createSolutionHandler(string arg)
{
switch (arg)
{
case "print":
return new Printer;
case "count":
return new Counter;
default:
assert(0, "First argument must be 'print' or 'count'");
}
}
void main(string[] args) @trusted
{
auto shandler = args.length > 1 ? createSolutionHandler(args[1]) : new Printer;
auto file = args.length > 2 ? args[2] : "tests/words.txt";
auto numbers = args.length > 3 ? args[3] : "tests/numbers.txt";
auto dict = loadDictionary(file);
auto numbersFile = File(numbers);
auto words = new string[16];
foreach (number; numbersFile.byLine)
{
auto num = number.idup;
string digits = num.filter!(c => c.isDigit)
.to!string;
dict.printTranslations(shandler, num, digits, words[0 .. 0]);
}
shandler.flush();
}