-
Notifications
You must be signed in to change notification settings - Fork 0
/
d14_1.php
72 lines (56 loc) · 1.52 KB
/
d14_1.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
<?php
function array_swap(&$array, $offset, $length)
{
$arr_length = count($array);
for ($i = 0; $i < intval($length / 2); $i++) {
// Wrap around list
$index = ($offset + $i) % $arr_length;
$index_to = ($offset + $length - 1 - $i) % $arr_length;
$tmp = $array[$index_to];
$array[$index_to] = $array[$index];
$array[$index] = $tmp;
}
}
function str_to_ascii_sequence($input)
{
return array_merge(
array_map(function ($c) {
return ord($c);
}, str_split($input)),
[17, 31, 73, 47, 23]
);
}
function calculate_knot_hash($input)
{
$hash = '';
$lengths = str_to_ascii_sequence($input);
$list = range(0, 255);
$index = $skip = 0;
for ($i = 0; $i < 64; $i++) {
foreach ($lengths as $length) {
array_swap($list, $index, $length);
// Wrap around list
$index = ($index + $length + $skip) % count($list);
$skip++;
}
}
foreach (array_chunk($list, 16) as $chunk) {
$xor = 0;
foreach ($chunk as $num) {
$xor ^= $num;
}
// convert to hex
$hash .= str_pad(dechex($xor), 2, '0', STR_PAD_LEFT);
}
return $hash;
}
$key = 'hxtvlmkl';
$used_square = 0;
for ($i = 0; $i < 128; $i++) {
$hash = calculate_knot_hash("{$key}-{$i}");
for ($j = 0; $j < strlen($hash); $j++) {
$bin = base_convert($hash[$j], 16, 2);
$used_square += substr_count($bin, '1');
}
}
var_dump($used_square);