Day 21 and 22 part1 in both of them works part2 is something quite different.
This commit is contained in:
2
21/input
Normal file
2
21/input
Normal file
@@ -0,0 +1,2 @@
|
||||
Player 1 starting position: 6
|
||||
Player 2 starting position: 4
|
||||
0
21/input.sample
Normal file
0
21/input.sample
Normal file
37
21/part1.php
Normal file
37
21/part1.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
$playerA = ['position' => 6, 'score' => 0];
|
||||
$playerB = ['position' => 4, 'score' => 0];
|
||||
$totalDiceRolls = 0;
|
||||
while (max([$playerA['score'], $playerB['score']]) < 1000) {
|
||||
$rolls = 0;
|
||||
|
||||
$rolls += ++$totalDiceRolls;
|
||||
$rolls += ++$totalDiceRolls;
|
||||
$rolls += ++$totalDiceRolls;
|
||||
|
||||
$playerScore = ($playerA['position'] + $rolls) % 10;
|
||||
if (($playerA['position'] + $rolls) % 10 === 0) {
|
||||
$playerScore = 10;
|
||||
}
|
||||
$playerA['position'] = $playerScore;
|
||||
$playerA['score'] += $playerScore;
|
||||
|
||||
if ($playerA['score'] >= 1000) {
|
||||
break;
|
||||
}
|
||||
$rolls = 0;
|
||||
|
||||
$rolls += ++$totalDiceRolls;
|
||||
$rolls += ++$totalDiceRolls;
|
||||
$rolls += ++$totalDiceRolls;
|
||||
|
||||
$playerScore = ($playerB['position'] + $rolls) % 10;
|
||||
if (($playerB['position'] + $rolls) % 10 === 0) {
|
||||
$playerScore = 10;
|
||||
}
|
||||
$playerB['position'] = $playerScore;
|
||||
$playerB['score'] += $playerScore;
|
||||
|
||||
}
|
||||
echo min([$playerA['score'], $playerB['score']]) * $totalDiceRolls; //920580
|
||||
3
21/part2.php
Normal file
3
21/part2.php
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
$input = file('input', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
||||
|
||||
Reference in New Issue
Block a user