advent_of_code_2020/day_22/part_1.php
2020-12-23 14:58:44 +01:00

32 lines
758 B
PHP

<?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;