8000 Day2 · FailedCode/adventofcode-2022-php@a73c46e · GitHub
[go: up one dir, main page]

Skip to content

Commit a73c46e

Browse files
committed
Day2
1 parent bf142f8 commit a73c46e

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

src/Days/Day2.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Failedcode\Aoc2022\Days;
4+
5+
class Day2 extends AbstractDay
6+
{
7+
public function solve_part_1(): string
8+
{
9+
$scoresPlay = [
10+
'X' => 1,
11+
'Y' => 2,
12+
'Z' => 3,
13+
];
14+
$scoresOutcome = [
15+
'loss' => 0,
16+
'draw' => 3,
17+
'win' => 6,
18+
];
19+
$outcomes = [
20+
"A X" => "draw",
21+
"B Y" => "draw",
22+
"C Z" => "draw",
23+
"A Y" => "win",
24+
"B Z" => "win",
25+
"C X" => "win",
26+
"A Z" => "loss",
27+
"B X" => "loss",
28+
"C Y" => "loss",
29+
];
30+
31+
$points = 0;
32+
$list = $this->util->loadInput(2);
33+
foreach ($list as $row) {
34+
if (empty($row)) {
35+
continue;
36+
}
37+
list($enemyMove, $myMove) = explode(' ', $row, 2);
38+
$outcome = $outcomes[$row];
39+
$points += $scoresOutcome[$outcome];
40+
$points += $scoresPlay[$myMove];
41+
42+
}
43+
44+
return $points;
45+
}
46+
47+
public function solve_part_2(): string
48+
{
49+
$scoresPlay = [
50+
'A' => 1,
51+
'B' => 2,
52+
'C' => 3,
53+
];
54+
$scoresOutcome = [
55+
'X' => 0,
56+
'Y' => 3,
57+
'Z' => 6,
58+
];
59+
$moves = [
60+
"A X" => "C",
61+
"A Y" => "A",
62+
"A Z" => "B",
63+
"B X" => "A",
64+
"B Y" => "B",
65+
"B Z" => "C",
66+
"C X" => "B",
67+
"C Y" => "C",
68+
"C Z" => "A",
69+
];
70+
71+
$points = 0;
72+
$list = $this->util->loadInput(2);
73+
foreach ($list as $row) {
74+
if (empty($row)) {
75+
continue;
76+
}
77+
list($enemyMove, $outcome) = explode(' ', $row, 2);
78+
$myMove = $moves[$row];
79+
$points += $scoresOutcome[$outcome];
80+
$points += $scoresPlay[$myMove];
81+
82+
}
83+
84+
return $points;
85+
}
86+
}

0 commit comments

Comments
 (0)
0