-
Notifications
You must be signed in to change notification settings - Fork 0
/
ugly.php
313 lines (284 loc) · 7.77 KB
/
ugly.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
class TernaryNumber {
/**
* @var array
* A terit is the made up name I gave to the ternary equivalent of a bit
*/
protected $terits;
/**
* @param int $numberOfTerits
*/
public function __construct($numberOfTerits = 13)
{
$this->terits = array();
for($i = 0; $i < $numberOfTerits; $i++) {
$this->terits[$i] = 0;
}
}
/**
* @return int
*/
public function getNumberOfTerits()
{
return count($this->terits);
}
/**
* @param int $index
* @return int
*/
public function getTerit($index) {
return $this->terits[$index];
}
/**
* @return array
*/
public function getTerits()
{
return $this->terits;
}
/**
* @param int $index
* @param int $value
* @todo add parameter validation
*/
public function setTerit($index, $value) {
$this->terits[$index] = $value;
}
/**
* @param int $index
* @todo add parameter validation
*/
public function incrementTerit($index) {
if($this->terits[$index] < 2) {
$this->terits[$index]++;
} else {
$this->terits[$index] = 0;
$this->incrementTerit($index+1);
}
}
/**
* Equivalent to ++
*/
public function plusplus()
{
$this->incrementTerit(0);
}
/**
* @return int
*
* Returns the base10 representation of the TernaryNumber
*/
public function toInt()
{
$toReturn = 0;
foreach($this->terits as $key => $terit) {
$toReturn += pow(3, $key) * $terit;
}
return $toReturn;
}
/**
* @return string
* Display a string representation of the TernaryNumber's terits
*/
public function toString()
{
$toReturn = '';
foreach($this->terits as $key => $terit) {
$toReturn .= "Key = {$key}, Value = {$terit}\n\r";
}
return $toReturn;
}
}
/**
* If there is a parameter it should be the file name to read the input from
*/
if($argc > 1) {
$input = file_get_contents($argv[1]);
$input_values = explode(PHP_EOL, $input);
foreach($input_values as $key => $value) {
$numberOfLeadingZeroes = 0;
$trimmedValue = trimmer($value, $numberOfLeadingZeroes);
echo pow(3, $numberOfLeadingZeroes) * UglyCounter::countUgly($trimmedValue) . "\n";
}
/*echo "Count below Threshold = " . Evaler::getCountBelowThreshold() . "\n";
echo "Count above threshold = " . Evaler::getCountOverThreshold() . "\n";*/
}
function trimmer($value, &$trimmedCount)
{
if(strpos($value, '00') === 0) {
return trimmer(substr($value, 1), ++$trimmedCount);
}
return $value;
}
/**
* @param $value
* @return bool
*
* Returns true is the parameter is considered 'ugly'
*/
function isUgly($value)
{
$valueAsInt = (int) $value;
if($valueAsInt % 2 === 0) return true;
if($valueAsInt % 3 === 0) return true;
if($valueAsInt % 5 === 0) return true;
if($valueAsInt % 7 === 0) return true;
return false;
}
class UglyCounter {
/**
* @var array
*/
static protected $knownCounts = array(
);
/**
* @param string $value
* @return int
*/
public static function countUgly($value)
{
if(array_key_exists($value,self::$knownCounts)) {
return self::$knownCounts[$value];
}
// Split the string into char array
$digits = str_split($value);
$operators = array();
$uglyCount = 0;
$numDigits = count($digits);
$t = new TernaryNumber($numDigits - 1);
$maxOperatorPermutations = pow(3, $numDigits - 1);
// Added to improve performance
$tt = $t->toInt();
while($tt < $maxOperatorPermutations) {
$terits = $t->getTerits();
foreach($digits as $key => $digit) {
if($key === 0) {
// don't do anything
} else {
// This is because operators's keys correspond to the key of the right side digit
//$terit = $t->getTerit($key - 1);
$terit = $terits[$key - 1];
if($terit === 0) {
$operators[$key] = 'no';
} else if($terit === 1) {
$operators[$key] = '+';
} else {
$operators[$key] = '-';
}
}
}
if(isUgly(Evaler::magicEval($digits, $operators))) {
$uglyCount++;
}
$t->plusplus();
$tt++;
}
self::$knownCounts[$value] = $uglyCount;
return $uglyCount;
}
}
/**
* @param array $digits
* @param array $operators
*/
function processNullOperators(&$digits, &$operators)
{
$lastValidKey = -1;
$newDigits = array();
$newOperators = array();
foreach($digits as $digitKey => $digit) {
//var_dump('test');
if($digitKey === 0) {
$newDigits[$digitKey] = $digit;
$lastValidKey = $digitKey;
} else if($operators[$digitKey] === 'no') {
$newDigits[$lastValidKey] = nullOperator($newDigits[$lastValidKey], $digit);
} else {
$newDigits[$digitKey] = $digit;
$newOperators[$digitKey] = $operators[$digitKey];
$lastValidKey = $digitKey;
}
}
$digits = $newDigits;
$operators = $newOperators;
}
class Evaler {
/*protected static $countOfEvalsBelowThreshold = 0;
protected static $countOfEvalsOverThreshold = 0;
protected static $threshold = 50;*/
/**
* @param array $digits
* @param array $operators
* @return int
*/
public static function magicEval($digits = array(), $operators = array())
{
processNullOperators($digits, $operators);
// Skip eval (just return 2) if the set of digits will be even
// array_sum turned out to be slower than foreach in testing
//$digitSum = array_sum($digits);
$digitSum = 0;
foreach($digits as $digitKey => $digit) {
$digitSum+= $digit;
}
if($digitSum % 2 === 0) {
return 2;
}/* else if((count($digits) - 1) % 2 === 1) {
// If the digit sum is odd, but the number of + and - operators is also odd, then the evaluation will be an even number,
// since we've already removed the no's we can determine the number of + / - by subtracting 1 from the number of digits
// This isn't true
return 2;
}*/
$result = $digits[0];
foreach($digits as $digitKey => $digit) {
if($digitKey > 0) {
if($operators[$digitKey] == '+') {
$result = sum($result, $digit);
} else if($operators[$digitKey] == '-') {
$result = difference($result, $digit);
}
}
}
/*if($result <= self::$threshold) {
self::$countOfEvalsBelowThreshold++;
} else {
self::$countOfEvalsOverThreshold++;
}*/
return $result;
}
/*public static function getCountBelowThreshold()
{
return self::$countOfEvalsBelowThreshold;
}
public static function getCountOverThreshold()
{
return self::$countOfEvalsOverThreshold;
}*/
}
/**
* @param int $left
* @param int $right
* @return int
*/
function sum($left, $right)
{
return $left + $right;
}
/**
* @param int $left
* @param int $right
* @return int mixed
*/
function difference($left, $right)
{
return $left - $right;
}
/**
* @param int $left
* @param int $right
* @return int
*/
function nullOperator($left, $right)
{
return 10*$left + $right;
}