15th day and 16th day WIP :(

This commit is contained in:
kplaczek
2021-12-16 21:18:02 +01:00
parent e3494e234a
commit 223863aa1f
8 changed files with 299 additions and 0 deletions

1
16/input Normal file
View File

@@ -0,0 +1 @@
E0525D9802FA00B80021B13E2D4260004321DC648D729DD67B2412009966D76C0159ED274F6921402E9FD4AC1B0F652CD339D7B82240083C9A54E819802B369DC0082CF90CF9280081727DAF41E6A5C1B9B8E41A4F31A4EF67E2009834015986F9ABE41E7D6080213931CB004270DE5DD4C010E00D50401B8A708E3F80021F0BE0A43D9E460007E62ACEE7F9FB4491BC2260090A573A876B1BC4D679BA7A642401434937C911CD984910490CCFC27CC7EE686009CFC57EC0149CEFE4D135A0C200C0F401298BCF265377F79C279F540279ACCE5A820CB044B62299291C0198025401AA00021D1822BC5C100763A4698FB350E6184C00A9820200FAF00244998F67D59998F67D5A93ECB0D6E0164D709A47F5AEB6612D1B1AC788846008780252555097F51F263A1CA00C4D0946B92669EE47315060081206C96208B0B2610E7B389737F3E2006D66C1A1D4ABEC3E1003A3B0805D337C2F4FA5CD83CE7DA67A304E9BEEF32DCEF08A400020B1967FC2660084BC77BAC3F847B004E6CA26CA140095003900BAA3002140087003D40080022E8C00870039400E1002D400F10038C00D100218038F400B6100229500226699FEB9F9B098021A00800021507627C321006E24C5784B160C014A0054A64E64BB5459DE821803324093AEB3254600B4BF75C50D0046562F72B1793004667B6E78EFC0139FD534733409232D7742E402850803F1FA3143D00042226C4A8B800084C528FD1527E98D5EB45C6003FE7F7FCBA000A1E600FC5A8311F08010983F0BA0890021F1B61CC4620140EC010100762DC4C8720008641E89F0866259AF460C015D00564F71ED2935993A539C0F9AA6B0786008D80233514594F43CDD31F585005A25C3430047401194EA649E87E0CA801D320D2971C95CAA380393AF131F94F9E0499A775460

1
16/input.sample Normal file
View File

@@ -0,0 +1 @@
A0016C880162017C3686B18A3D4780

101
16/part1.php Normal file
View 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.

3
16/part2.php Normal file
View File

@@ -0,0 +1,3 @@
<?php
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);