15th day and 16th day WIP :(
This commit is contained in:
101
16/part1.php
Normal file
101
16/part1.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = str_split($input[0]);
|
||||
|
||||
$bin = '';
|
||||
foreach ($input as $char) {
|
||||
$bin .= str_pad(base_convert($char, 16, 2), 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
print_r(decodeString($bin));
|
||||
//decodeString($bin);
|
||||
|
||||
|
||||
function decodeString($binaryString, $offset = 0)
|
||||
{
|
||||
$data = [];
|
||||
$data['maxoffset'] = $offset;
|
||||
echo str_repeat(' ', $offset) . $binaryString . "\n";
|
||||
echo str_repeat(' ', $offset) . substr($binaryString, 0, 3) . "\n";
|
||||
echo str_repeat(' ', $offset + 3) . substr($binaryString, 3, 3) . "\n";
|
||||
$data['version'] = bindec(substr($binaryString, 0, 3));
|
||||
$data['type'] = bindec(substr($binaryString, 3, 3));
|
||||
if ($data['type'] == 4) {
|
||||
$isLast = 0;
|
||||
$bitNumber = 0;
|
||||
$literalValue = '';
|
||||
$position = 6;
|
||||
while (!$isLast) {
|
||||
$position = 6 + $bitNumber * 5;
|
||||
echo str_repeat(' ', $offset + $position) . substr($binaryString, $position, 5) . "\n";
|
||||
$data['maxoffset'] = $offset + $position + 5;
|
||||
$binaryBit = substr($binaryString, $position, 5);
|
||||
$literalValue .= substr($binaryBit, 1, 4);
|
||||
$isLast = !$binaryBit[0];
|
||||
$bitNumber++;
|
||||
}
|
||||
|
||||
$data['literalValue'] = bindec($literalValue);
|
||||
$position += 5;
|
||||
// if ($nesting > 2) {
|
||||
//// print_r($binaryString);
|
||||
//// echo 1;
|
||||
//// die(print_r($data, 1));
|
||||
// }
|
||||
if (strlen($binaryString) - $position > 10) {
|
||||
$data['subpacket'] = [];
|
||||
|
||||
$decoded = decodeString(substr($binaryString, $position), $offset + $position);
|
||||
if ($data['maxoffset'] < $decoded['maxoffset']) {
|
||||
$data['maxoffset'] = $decoded['maxoffset'];
|
||||
}
|
||||
$data['subpacket'][] = $decoded;
|
||||
|
||||
}
|
||||
} else {
|
||||
$data['lengthTypeId'] = bindec(substr($binaryString, 6, 1));
|
||||
echo str_repeat(' ', $offset + 6) . substr($binaryString, 6, 1) . "\n";
|
||||
if ($data['lengthTypeId']) {
|
||||
$data['numberOfPackets'] = bindec(substr($binaryString, 7, 11));
|
||||
echo str_repeat(' ', $offset + 7) . substr($binaryString, 7, 11) . "\n";
|
||||
$bin = substr($binaryString, 18);
|
||||
$data['subpacket'] = [];
|
||||
// if ($nesting > 2) {
|
||||
//// print_r($binaryString);
|
||||
//// echo 2;
|
||||
//// die(print_r($data, 1));
|
||||
// }
|
||||
for ($i = 0; $i < $data['numberOfPackets']; $i++) {
|
||||
$decoded = decodeString($bin, $offset + 18);
|
||||
if ($data['maxoffset'] < $decoded['maxoffset']) {
|
||||
$data['maxoffset'] = $decoded['maxoffset'];
|
||||
$offset = $data['maxoffset']-18;
|
||||
}
|
||||
$data['subpacket'][] = $decoded;
|
||||
$bin = substr($bin, $data['maxoffset']);
|
||||
}
|
||||
// $data['subpacket'][] = decodeString($bin, $offset + 18);
|
||||
} else {
|
||||
$data['totalLengthOfPackets'] = bindec(substr($binaryString, 7, 15));
|
||||
$bin = substr($binaryString, 22, $data['totalLengthOfPackets']);
|
||||
echo str_repeat(' ', $offset + 7) . substr($binaryString, 7, 15) . "\n";
|
||||
$data['subpacket'] = [];
|
||||
// if ($nesting > 2) {
|
||||
//// echo 3;
|
||||
//// die(print_r($data, 1));
|
||||
// }
|
||||
$decoded = decodeString($bin, $offset + 22);
|
||||
if ($data['maxoffset'] < $decoded['maxoffset']) {
|
||||
$data['maxoffset'] = $decoded['maxoffset'];
|
||||
}
|
||||
$data['subpacket'][] = $decoded;
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
//echo chr(bindec('01010000001'));
|
||||
//8A004A801A8002F478 represents an operator packet
|
||||
// (version 4) which contains an operator packet
|
||||
// (version 1) which contains an operator packet
|
||||
// (version 5) which contains a literal value
|
||||
// (version 6); this packet has a version sum of 16.
|
||||
Reference in New Issue
Block a user