advent_of_code_2021/day_10/part_1.php

34 lines
767 B
PHP
Raw Permalink Normal View History

2021-12-10 09:24:39 +01:00
<?php
$lines = explode("\n", file_get_contents('./input.txt'));
$scores = [
')' => 3,
']' => 57,
'}' => 1197,
'>' => 25137,
];
$corrupted = [];
foreach ($lines as $line) {
$chunks = [];
foreach (str_split($line) as $chunk) {
switch ($chunk) {
case '(': $chunks[] = ')';break;
case '[': $chunks[] = ']';break;
case '{': $chunks[] = '}';break;
case '<': $chunks[] = '>';break;
default:
$last_chunk = array_pop($chunks);
if ($last_chunk !== $chunk) {
$corrupted[] = $chunk;
}
}
}
}
echo array_sum(array_map(static function ($score) use ($scores) {
return $scores[$score];
}, $corrupted));