Browse Source

🚧 Commence le jour 22

main
Clément 10 months ago
parent
commit
c410879c7c
  1. 32
      day_22/part_1.php

32
day_22/part_1.php

@ -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;
Loading…
Cancel
Save