🚧 Commence le jour 22

This commit is contained in:
Clément 2020-12-23 14:58:44 +01:00
parent 0b07e67fcc
commit c410879c7c

32
day_22/part_1.php Normal file
View File

@ -0,0 +1,32 @@
<?php
$players = explode("\n\n", file_get_contents('input.txt'));
[$cards_1, $cards_2] = $players;
$cards_1 = explode("\n", $cards_1);
array_shift($cards_1);
$cards_2 = explode("\n", $cards_2);
array_shift($cards_2);
while (!empty($cards_1) && !empty($cards_2)) {
$card_1 = array_shift($cards_1);
$card_2 = array_shift($cards_2);
$cards = [$card_1, $card_2];
rsort($cards);
if ($card_1 > $card_2) {
$cards_1 = array_merge($cards_1, $cards);
} else {
$cards_2 = array_merge($cards_2, $cards);
}
}
$final_deck = empty($cards_1) ? $cards_2 : $cards_1;
$final_deck = array_reverse($final_deck);
$sum = 0;
for ($i = 1, $l = count($final_deck); $i <= $l; $i++) {
$sum += ($i * $final_deck[$i-1]);
}
echo $sum;