10th day both parts
This commit is contained in:
40
10/part2.php
Normal file
40
10/part2.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = array_map('str_split', $input);
|
||||
|
||||
$chunkPairs = [
|
||||
')' => '(',
|
||||
']' => '[',
|
||||
'>' => '<',
|
||||
'}' => '{',
|
||||
];
|
||||
$values = [
|
||||
'(' => 1,
|
||||
'[' => 2,
|
||||
'{' => 3,
|
||||
'<' => 4,
|
||||
];
|
||||
$scores = [];
|
||||
foreach ($input as $line) {
|
||||
$tmpBuffer = [];
|
||||
foreach ($line as $charPos => $char) {
|
||||
if (in_array($char, array_values($chunkPairs))) {
|
||||
$tmpBuffer[] = $char;
|
||||
}
|
||||
if (in_array($char, array_keys($chunkPairs))) {
|
||||
$lastBufferItem = array_pop($tmpBuffer);
|
||||
if ($chunkPairs[$char] !== $lastBufferItem) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
$score = 0;
|
||||
foreach (array_reverse($tmpBuffer) as $bufferChar) {
|
||||
$score *= 5;
|
||||
$score += $values[$bufferChar];
|
||||
}
|
||||
$scores[] = $score;
|
||||
}
|
||||
sort($scores);
|
||||
print_r($scores);
|
||||
print_r($scores[floor(count($scores) / 2)]); //1118645287
|
||||
Reference in New Issue
Block a user