advent_of_code_2022/day_2/part_1.php

40 lines
770 B
PHP

<?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;