-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd08.php
38 lines (28 loc) · 940 Bytes
/
d08.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
<?php
function escapedHexToHex($escaped)
{
return chr(hexdec($escaped[1]));
}
$lines = file('input_d08.txt', FILE_IGNORE_NEW_LINES);
$literal_count = 0;
$char_count = 0;
$encode_count = 0;
$count = 0;
foreach ($lines as $line) {
// This is the best approach, from Reddit
// eval('$str = ' . $line . ';');
// $count += strlen($line) - strlen($str);
$literal_count += strlen($line);
// Part 1
// Replace hex with its chars, with string inside outer-most double quotes
$str = preg_replace_callback('/\\\\x([a-f0-9]{2})/i', 'escapedHexToHex', substr($line, 1, -1));
// Replace double slashes
$str = str_replace('\\\\', '\\', $str);
// Replace lash and double quote
$str = str_replace('\"', '"', $str);
$char_count += strlen($str);
// Part 2
$encode_count += strlen(addslashes($line)) + 2;
}
var_dump($literal_count - $char_count);
var_dump($encode_count - $literal_count);