-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.php
68 lines (62 loc) · 1.99 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
<?php
require_once './AbstractBenchmarking.php';
class Day2 extends AbstractBenchmarking
{
private array $data;
private array $testData;
private string $rawData;
private string $rawTestData;
public function __construct()
{
$this->rawData = file_get_contents('Data/day2.txt');
$this->rawTestData = file_get_contents('TestData/day2.txt');
$this->parseData(true);
$this->parseData();
}
public function parseData(bool $test = false): void
{
$rawInputData = $test ? $this->rawTestData : $this->rawData;
foreach (explode(PHP_EOL, $rawInputData) as $value) {
$lineInput = explode(' ', $value);
if ($test) {
$this->testData[] = $lineInput;
} else {
$this->data[] = $lineInput;
}
}
}
public function part1(bool $test = false): int
{
$inputData = $test ? $this->testData : $this->data;
$score = 0;
foreach ($inputData as $roundData) {
$keyOpponent = ord($roundData[0]) - 65;
$keyPlayer = ord($roundData[1]) - 88;
$result = $keyOpponent - $keyPlayer;
if ($result === -1 || $result === 2) {
$score += 6 + $keyPlayer + 1;
} elseif ($result === -2 || $result === 1) {
$score += $keyPlayer + 1;
} else {
$score += 3 + $keyPlayer + 1;
}
}
return $score;
}
public function part2(bool $test = false): int
{
$inputData = $test ? $this->testData : $this->data;
$score = 0;
foreach ($inputData as $roundData) {
$key = ord($roundData[0]) - 65;
if ($roundData[1] === 'X') {
$score += ($key + 2) % 3 + 1;
} elseif ($roundData[1] === 'Y') {
$score += 3 + $key % 3 + 1;
} else {
$score += 6 + ($key + 1) % 3 + 1;
}
}
return $score;
}
}