-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.php
75 lines (61 loc) · 1.59 KB
/
day2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
include(__DIR__."/_loader.php");
$input = InputHelper::loadFile("datasets/day2.txt");
// Part 1
$total = 0;
$scores = [
'Rock' => 1,
'Paper' => 2,
'Scissors' => 3,
];
$mapping = [
'A' => 'Rock',
'B' => 'Paper',
'C' => 'Scissors',
'X' => 'Rock',
'Y' => 'Paper',
'Z' => 'Scissors',
];
foreach ($input as $line) {
$opp = strtr(substr($line, 0, 1), $mapping);
$my = strtr(substr($line, 2, 1), $mapping);
$total += $scores[$my];
// draw
if ($opp == $my) $total += 3;
// win
elseif (($opp == 'Rock' && $my == 'Paper') || ($opp == 'Paper' && $my == 'Scissors') || ($opp == 'Scissors' && $my == 'Rock')) $total += 6;
}
echo "Answer #1: ".$total.PHP_EOL;
// Part 2
$total = 0;
$mapping = [
'A' => 'Rock',
'B' => 'Paper',
'C' => 'Scissors',
];
$scores = [
'Rock' => 1,
'Paper' => 2,
'Scissors' => 3,
'X' => 0,
'Y' => 3,
'Z' => 6,
];
foreach ($input as $line) {
$opp = strtr(substr($line, 0, 1), $mapping);
$my = substr($line, 2, 1);
$total += $scores[$my];
// draw
if ($my == 'Y') $total += $scores[$opp];
// win
elseif ($my == 'Z') {
if ($opp == 'Rock') $total += $scores['Paper'];
elseif ($opp == 'Paper') $total += $scores['Scissors'];
else $total += $scores['Rock'];
} else {
if ($opp == 'Paper') $total += $scores['Rock'];
elseif ($opp == 'Scissors') $total += $scores['Paper'];
else $total += $scores['Scissors'];
}
}
echo "Answer #2: ".$total.PHP_EOL;