This repository was archived by the owner on Jul 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay10.php
More file actions
79 lines (61 loc) · 1.84 KB
/
Day10.php
File metadata and controls
79 lines (61 loc) · 1.84 KB
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
<?php
function processInstructions($list, $instructions, $loopCount)
{
$index = 0;
$skipSize = 0;
$count = count($list);
for ($i = 0; $i < $loopCount; $i++) {
foreach ($instructions as $instruction) {
$tempData = array_merge($list, $list, $list);
$subSet = array_slice($tempData, $index, $instruction);
$subSet = array_reverse($subSet);
foreach ($subSet as $key => $newValue) {
$idx = ($index + $key) % $count;
$list[$idx] = $newValue;
}
$index = ($index + $instruction + $skipSize) % $count;
$skipSize++;
}
}
return $list;
}
$input = '129,154,49,198,200,133,97,254,41,6,2,1,255,0,191,108';
$min = 0;
$count = 256;
$instructions = explode(',', $input);
$list = range($min, $count - 1);
$list = processInstructions($list, $instructions, 1);
$answer = $list[0] * $list[1];
echo "\npart 1: $answer\n";
$instructions = str_split($input);
$list = range($min, $count - 1);
foreach ($instructions as &$instruction) {
$instruction = ord($instruction);
}
array_push($instructions, 17, 31, 73, 47, 23);
$list = processInstructions($list, $instructions, 64);
$elements = [];
while (count($list)) {
$elements[] =
array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list)
^ array_shift($list);
}
$output = '';
foreach ($elements as $element) {
$output .= sprintf('%0.2s', dechex($element));
}
echo "part 2 $output\n";