diff --git a/day_2/part_1.php b/day_2/part_1.php new file mode 100644 index 0000000..2d13ceb --- /dev/null +++ b/day_2/part_1.php @@ -0,0 +1,40 @@ + '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; \ No newline at end of file diff --git a/day_2/part_2.php b/day_2/part_2.php new file mode 100644 index 0000000..9c4f36a --- /dev/null +++ b/day_2/part_2.php @@ -0,0 +1,61 @@ + '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; \ No newline at end of file