10th day both parts
This commit is contained in:
33
10/part1.php
Normal file
33
10/part1.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
$input = array_map('str_split', $input);
|
||||
|
||||
$chunkPairs = [
|
||||
')' => '(',
|
||||
']' => '[',
|
||||
'>' => '<',
|
||||
'}' => '{',
|
||||
];
|
||||
$illegalValues = [
|
||||
')' => 3,
|
||||
']' => 57,
|
||||
'}' => 1197,
|
||||
'>' => 25137,
|
||||
];
|
||||
$illegalValue = 0;
|
||||
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) {
|
||||
$illegalValue += $illegalValues[$char];
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
echo $illegalValue; //366027
|
||||
Reference in New Issue
Block a user