Finit le jour 10

This commit is contained in:
Clement Desmidt 2021-12-10 09:24:39 +01:00
parent c290b5fee6
commit 592bc62c63
2 changed files with 80 additions and 0 deletions

33
day_10/part_1.php Normal file
View File

@ -0,0 +1,33 @@
<?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));

47
day_10/part_2.php Normal file
View File

@ -0,0 +1,47 @@
<?php
$lines = explode("\n", file_get_contents('./input.txt'));
$coolos = [];
foreach ($lines as $line) {
$chunks = [];
$is_cool = true;
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) {
$is_cool = false;
break 2;
}
}
}
if ($is_cool) {
$coolos[] = $chunks;
}
}
$scores = [];
foreach ($coolos as $remaining_chunks) {
$remaining_chunks = array_reverse($remaining_chunks);
$scores[] = array_reduce($remaining_chunks, static function ($carry, $chunk) {
$additions = [
')' => 1,
']' => 2,
'}' => 3,
'>' => 4,
];
$carry *= 5;
$carry += $additions[$chunk];
return $carry;
});
}
sort($scores);
echo $scores[(int) floor(count($scores) / 2)];