✨ Finit le jour 5
This commit is contained in:
parent
f0609cd245
commit
ff57718fa1
@ -4,7 +4,7 @@
|
||||
|
||||
| Lundi | Mardi | Mercredi | Jeudi | Vendredi | Samedi | Dimanche |
|
||||
|---------|---------|----------|---------|----------|---------|----------|
|
||||
| | | | 1 ✅✅ | 2 🔲🔲 | 3 🔲🔲 | 4 🔲🔲 |
|
||||
| 5 🔲🔲 | 6 🔲🔲 | 7 🔲🔲 | 8 🔲🔲 | 9 🔲🔲 | 10 🔲🔲 | 11 🔲🔲 |
|
||||
| | | | 1 ✅✅ | 2 ✅✅ | 3 ✅✅ | 4 ✅✅ |
|
||||
| 5 ✅✅ | 6 🔲🔲 | 7 🔲🔲 | 8 🔲🔲 | 9 🔲🔲 | 10 🔲🔲 | 11 🔲🔲 |
|
||||
| 12 🔲🔲 | 13 🔲🔲 | 14 🔲🔲 | 15 🔲🔲 | 16 🔲🔲 | 17 🔲🔲 | 18 🔲🔲 |
|
||||
| 19 🔲🔲 | 20 🔲🔲 | 21 🔲🔲 | 22 🔲🔲 | 23 🔲🔲 | 24 🔲🔲 | 25 🔲🔲 |
|
||||
|
40
day_5/part_1.php
Normal file
40
day_5/part_1.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
[$columns, $movements] = explode("\n\n", file_get_contents('./input.txt'));
|
||||
|
||||
$columns = parseColumns($columns);
|
||||
|
||||
$movements = explode("\n", $movements);
|
||||
foreach ($movements as $movement) {
|
||||
preg_match('/^move (\d+) from (\d+) to (\d+)$/', $movement, $matches);
|
||||
[, $number, $from, $to] = $matches;
|
||||
for ($i = 0; $i < $number; $i++) {
|
||||
$element = array_pop($columns[$from]);
|
||||
if ($element === null) {
|
||||
break;
|
||||
}
|
||||
|
||||
$columns[$to][] = $element;
|
||||
}
|
||||
}
|
||||
|
||||
echo implode(array_map('array_pop', $columns));
|
||||
|
||||
function parseColumns(string $columns)
|
||||
{
|
||||
$parsed_columns = [];
|
||||
$rows = array_map('str_split', explode("\n", $columns));
|
||||
array_unshift($rows, null);
|
||||
$rows = array_map(...$rows);
|
||||
$rows = array_map('array_reverse', $rows);
|
||||
foreach ($rows as $row) {
|
||||
$number = array_shift($row);
|
||||
if (!is_numeric($number) || empty($number)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parsed_columns[$number] = array_filter($row, fn($cell) => !empty(trim($cell)));
|
||||
}
|
||||
|
||||
return $parsed_columns;
|
||||
}
|
34
day_5/part_2.php
Normal file
34
day_5/part_2.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
[$columns, $movements] = explode("\n\n", file_get_contents('./input.txt'));
|
||||
|
||||
$columns = parseColumns($columns);
|
||||
|
||||
$movements = explode("\n", $movements);
|
||||
foreach ($movements as $movement) {
|
||||
preg_match('/^move (\d+) from (\d+) to (\d+)$/', $movement, $matches);
|
||||
[, $number, $from, $to] = $matches;
|
||||
$elements = array_splice($columns[$from], count($columns[$from]) - $number, $number);
|
||||
$columns[$to] = array_merge($columns[$to], $elements);
|
||||
}
|
||||
|
||||
echo implode(array_map('array_pop', $columns));
|
||||
|
||||
function parseColumns(string $columns)
|
||||
{
|
||||
$parsed_columns = [];
|
||||
$rows = array_map('str_split', explode("\n", $columns));
|
||||
array_unshift($rows, null);
|
||||
$rows = array_map(...$rows);
|
||||
$rows = array_map('array_reverse', $rows);
|
||||
foreach ($rows as $row) {
|
||||
$number = array_shift($row);
|
||||
if (!is_numeric($number) || empty($number)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parsed_columns[$number] = array_filter($row, fn($cell) => !empty(trim($cell)));
|
||||
}
|
||||
|
||||
return $parsed_columns;
|
||||
}
|
Loading…
Reference in New Issue
Block a user