Finit le jour 2

This commit is contained in:
Clement Desmidt 2022-12-02 09:42:05 +01:00
parent 51aaf9af97
commit dafbffbcf8
2 changed files with 101 additions and 0 deletions

40
day_2/part_1.php Normal file
View File

@ -0,0 +1,40 @@
<?php
$rounds = explode("\n", file_get_contents('./input.txt'));
$total_score = 0;
$types = [
'A' => 'rock',
'X' => 'rock',
'B' => 'paper',
'Y' => 'paper',
'C' => 'scissor',
'Z' => 'scissor',
];
$shapes = [
'rock' => 1,
'paper' => 2,
'scissor' => 3,
];
foreach ($rounds as $round) {
[$opponent, $you] = explode(' ', $round);
$opponent = $types[$opponent];
$you = $types[$you];
if ($you === $opponent) {
$total_score += 3;
} elseif (
($opponent === 'rock' && $you === 'paper') ||
($opponent === 'paper' && $you === 'scissor') ||
($opponent === 'scissor' && $you === 'rock')
) {
$total_score += 6;
}
$total_score += $shapes[$you];
}
echo $total_score;

61
day_2/part_2.php Normal file
View File

@ -0,0 +1,61 @@
<?php
$rounds = explode("\n", file_get_contents('./input.txt'));
$total_score = 0;
$types = [
'A' => 'rock',
'B' => 'paper',
'C' => 'scissor',
'X' => 'lose',
'Y' => 'draw',
'Z' => 'win',
];
$shapes = [
'rock' => 1,
'paper' => 2,
'scissor' => 3,
];
foreach ($rounds as $round) {
[$opponent, $needs_to_end] = explode(' ', $round);
$opponent = $types[$opponent];
$needs_to_end = $types[$needs_to_end];
switch ($needs_to_end) {
case 'draw':
$total_score += 3;
$total_score += $shapes[$opponent];
break;
case 'win':
$total_score += 6;
switch ($opponent) {
case 'rock':
$total_score += $shapes['paper'];
break;
case 'paper':
$total_score += $shapes['scissor'];
break;
case 'scissor':
$total_score += $shapes['rock'];
break;
}
break;
case 'lose':
switch ($opponent) {
case 'rock':
$total_score += $shapes['scissor'];
break;
case 'paper':
$total_score += $shapes['rock'];
break;
case 'scissor':
$total_score += $shapes['paper'];
break;
}
}
}
echo $total_score;