diff --git a/day_10/part_1.php b/day_10/part_1.php new file mode 100644 index 0000000..191e7d8 --- /dev/null +++ b/day_10/part_1.php @@ -0,0 +1,33 @@ + 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)); diff --git a/day_10/part_2.php b/day_10/part_2.php new file mode 100644 index 0000000..2ed2659 --- /dev/null +++ b/day_10/part_2.php @@ -0,0 +1,47 @@ +';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)]; \ No newline at end of file