advent_of_code_2022/day_2/part_2.php

61 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2022-12-02 09:42:05 +01:00
<?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;