/* 2. "a" を 1、"b" を 2、...、"z" を 26 として、文字列を数列に変換します。この数列を A($baseArr) とします。 3. 数列 A の隣り合う 2 つの数を足して前から順番に並べた新しい数列 A' を作り、これを新たに A とします。 このとき、A の要素の大きさが 101 を超えていた場合、その要素から 101 を引きます。 4. 数列 A の要素数が 1 になるまで 3. の手順を繰り返します。 A の要素数が 1 となったとき、残った要素の値を「二人の相性」とします。 名前の並べ方は 2 通りあります。 2 通りの方法で計算した「二人の相性」のうち[大きい方]を出力する */ class MakeNewArr { public function __construct(private array $baseNameArr, private array $baseaAlphabetArr) { } public function makeNewArr(): array { $newArr = []; foreach ($this->baseNameArr as $index => $letter) { foreach ($this->baseaAlphabetArr as $alphabet => $number) { if ($letter === $alphabet) { $newArr[] = $number; } } } $makeNewArrApostrophe = new MakeNewArrApostrophe($newArr); $newArrApostrophe = $makeNewArrApostrophe->makeNewArrApostrophe(); return $newArrApostrophe; } } class MakeNewArrApostrophe { public function __construct(private array $newArr) { } public function makeNewArrApostrophe(): array { $newArrApostrophe = []; for ($i = 0; $i < count($this->newArr) - 1; $i++) { if ($this->newArr[$i] + $this->newArr[$i + 1] > 101) { $newArrApostrophe[] = $this->newArr[$i] + $this->newArr[$i + 1] - 101; } else { $newArrApostrophe[] = $this->newArr[$i] + $this->newArr[$i + 1]; } } return $newArrApostrophe; } } class Divination { public function __construct(private array $baseNameArrPattern, private array $baseaAlphabetArr) { } public function calculateDivination(): array { $makeNewArr = new makeNewArr($this->baseNameArrPattern, $this->baseaAlphabetArr); $newArr = $makeNewArr->makeNewArr(); $count = (int) count($newArr); while($count > 1) { $makeNewArrApostrophe = new MakeNewArrApostrophe($newArr); $newArr = $makeNewArrApostrophe->makeNewArrApostrophe(); $count = count($newArr); } return $newArr; } } class Game { public function start(): int { fscanf(STDIN, "%s %s", $s, $t); $name1 = str_split($s); $name2 = str_split($t); $baseNameArrPattern1 = array_merge($name1, $name2); $baseNameArrPattern2 = array_merge($name2, $name1); $baseaAlphabetArr = []; foreach (range('a', 'z') as $i => $letter) { $baseaAlphabetArr[$letter] = $i + 1; } $divination1 = new Divination($baseNameArrPattern1, $baseaAlphabetArr); $result1 = $divination1->calculateDivination(); $divination2 = new Divination($baseNameArrPattern2, $baseaAlphabetArr); $result2 = $divination2->calculateDivination(); $result = max($result1[0], $result2[0]); return $result; } } $game = new Game(); echo $game->start();