|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Year2023\Day07; |
| 4 | + |
| 5 | +$start = microtime(true); |
| 6 | + |
| 7 | +#$lines = file('example.txt', FILE_IGNORE_NEW_LINES); |
| 8 | +$lines = file('input.txt', FILE_IGNORE_NEW_LINES); |
| 9 | + |
| 10 | + |
| 11 | +class Hand { |
| 12 | + private CONST CARDS_VALUES = [ |
| 13 | + 2 => '02', |
| 14 | + 3 => '03', |
| 15 | + 4 => '04', |
| 16 | + 5 => '05', |
| 17 | + 6 => '06', |
| 18 | + 7 => '07', |
| 19 | + 8 => '08', |
| 20 | + 9 => '09', |
| 21 | + 'T' => '10', |
| 22 | + 'J' => '11', |
| 23 | + 'Q' => '12', |
| 24 | + 'K' => '13', |
| 25 | + 'A' => '14', |
| 26 | + ]; |
| 27 | + |
| 28 | + private const SORT_SCORES = [ |
| 29 | + 'fiveOfAKind' => 700000000000, |
| 30 | + 'fourOfAKind' => 600000000000, |
| 31 | + 'fullHouse' => 500000000000, |
| 32 | + 'threeOfAKind' => 400000000000, |
| 33 | + 'twoPair' => 300000000000, |
| 34 | + 'onePair' => 200000000000, |
| 35 | + 'highCard' => 100000000000, |
| 36 | + ]; |
| 37 | + |
| 38 | + private array $cards = []; |
| 39 | + private int $bid; |
| 40 | + |
| 41 | + private int $sortScore = 0; |
| 42 | + public function __construct(public string $raw) |
| 43 | + { |
| 44 | + [$cards, $this->bid] = explode(" ", $raw); |
| 45 | + $cards = str_split($cards); |
| 46 | + foreach ($cards as $card) { |
| 47 | + $this->cards[] = self::CARDS_VALUES[$card]; |
| 48 | + } |
| 49 | + $this->sortScore = (int)implode("", $this->cards); |
| 50 | + rsort($this->cards); |
| 51 | + $this->determineSortScore(); |
| 52 | + } |
| 53 | + |
| 54 | + public function getBid(): int |
| 55 | + { |
| 56 | + return $this->bid; |
| 57 | + } |
| 58 | + private function determineSortScore(): void |
| 59 | + { |
| 60 | + // five of a kind -- cards are ordered, they are the same if first and last have the same value |
| 61 | + if ($this->cards[0] === $this->cards[4]) { |
| 62 | + $this->sortScore += self::SORT_SCORES['fiveOfAKind']; |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + // four of a kind -- cards are ordered, they are the same if first and penultimate have the same value or the second and the last |
| 67 | + if ($this->cards[0] === $this->cards[3] || $this->cards[1] === $this->cards[4]) { |
| 68 | + $this->sortScore += self::SORT_SCORES['fourOfAKind']; |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // fullHouse -- cards are ordered, they are the same if first and penultimate have the same value |
| 73 | + if (($this->cards[0] === $this->cards[1] && $this->cards[2] === $this->cards[4]) || ($this->cards[0] === $this->cards[2] && $this->cards[3] === $this->cards[4])) { |
| 74 | + $this->sortScore += self::SORT_SCORES['fullHouse']; |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // three of a kind |
| 79 | + if ($this->cards[0] === $this->cards[2] || $this->cards[1] === $this->cards[3] || $this->cards[2] === $this->cards[4]) { |
| 80 | + $this->sortScore += self::SORT_SCORES['threeOfAKind']; |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + // twoPair |
| 85 | + if (($this->cards[0] === $this->cards[1] && $this->cards[2] === $this->cards[3]) || ($this->cards[1] === $this->cards[2] && $this->cards[3] === $this->cards[4])) { |
| 86 | + $this->sortScore += self::SORT_SCORES['twoPair']; |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // onePair |
| 91 | + if ($this->cards[0] === $this->cards[1] || $this->cards[1] === $this->cards[2] || $this->cards[2] === $this->cards[3] || $this->cards[3] === $this->cards[4]) { |
| 92 | + $this->sortScore += self::SORT_SCORES['onePair']; |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // HighCard |
| 97 | + $this->sortScore += self::SORT_SCORES['highCard']; # pointless |
| 98 | + } |
| 99 | + |
| 100 | + public function getSortScore(): int |
| 101 | + { |
| 102 | + return $this->sortScore; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +$hands = []; |
| 107 | +foreach ($lines as $line) { |
| 108 | + $hands[] = $hand = new Hand($line); |
| 109 | + #echo $hand->raw, ": ", $hand->getSortScore(), "\n"; |
| 110 | +} |
| 111 | + |
| 112 | +echo "\n\n"; |
| 113 | + |
| 114 | +usort($hands, function (Hand $a, Hand $b) { |
| 115 | + return $a->getSortScore() <=> $b->getSortScore(); |
| 116 | +}); |
| 117 | + |
| 118 | +$sum = 0; |
| 119 | +foreach ($hands as $pos => $hand) { |
| 120 | + echo $hand->getSortScore(), " ", $hand->raw, ": ", ($pos + 1), " * ", $hand->getBid(), " => ", ($partScore = (($pos + 1) * $hand->getBid())), "\n"; |
| 121 | + $sum += $partScore; |
| 122 | +} |
| 123 | + |
| 124 | +echo "\n", $sum, "\n"; |
| 125 | + |
| 126 | +echo microtime(true) - $start; |
| 127 | +echo "\n"; |
| 128 | + |
0 commit comments