Skip to content

Commit a73b2f6

Browse files
committed
Added solution for 2016 day 2
1 parent 2e5d2d0 commit a73b2f6

File tree

1 file changed

+47
-40
lines changed

1 file changed

+47
-40
lines changed

src/AdventSolutions/Year2016/Day2/Solution2016Day2.php

Lines changed: 47 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,34 @@ class Solution2016Day2 extends AbstractSolution
88
{
99
public function solvePart1($input): string
1010
{
11-
$startposition = [1, 1];
12-
1311
$code = '';
14-
15-
// for each input line, move according to each letter
12+
$position = [1, 1]; // Start at '5' on a 3x3 grid
1613

1714
foreach ($input as $line) {
18-
$position = $startposition;
1915
$instructions = str_split($line);
2016
foreach ($instructions as $instruction) {
21-
switch ($instruction) {
22-
case 'U':
23-
$position = $this->moveUp($position);
24-
break;
25-
case 'D':
26-
$position = $this->moveDown($position);
27-
break;
28-
case 'L':
29-
$position = $this->moveLeft($position);
30-
break;
31-
case 'R':
32-
$position = $this->moveRight($position);
33-
break;
34-
}
17+
$position = $this->move($position, $instruction, $this->keypad());
3518
}
3619
$code .= $this->keypad()[$position[1]][$position[0]];
3720
}
38-
39-
return "The code is: <info>$code</info>";
21+
22+
return "The code for Part 1 is: <info>$code</info>";
4023
}
4124

4225
public function solvePart2($input): string
4326
{
44-
// Implement the logic for solving part 2 here
45-
46-
return "Part 2 not yet implemented!";
27+
$code = '';
28+
$position = [0, 2]; // Start at '5' on the Part 2 keypad layout
29+
30+
foreach ($input as $line) {
31+
$instructions = str_split($line);
32+
foreach ($instructions as $instruction) {
33+
$position = $this->move($position, $instruction, $this->keypad2());
34+
}
35+
$code .= $this->keypad2()[$position[1]][$position[0]];
36+
}
37+
38+
return "The code for Part 2 is: <info>$code</info>";
4739
}
4840

4941
private function keypad(): array
@@ -55,27 +47,42 @@ private function keypad(): array
5547
];
5648
}
5749

58-
private function moveUp($position): array
50+
private function keypad2(): array
5951
{
60-
$position[1] = max(0, $position[1] - 1);
61-
return $position;
52+
return [
53+
[null, null, 1, null, null],
54+
[null, 2, 3, 4, null],
55+
[5, 6, 7, 8, 9],
56+
[null, 'A', 'B', 'C', null],
57+
[null, null, 'D', null, null],
58+
];
6259
}
6360

64-
private function moveDown($position): array
61+
private function move(array $position, string $direction, array $keypad): array
6562
{
66-
$position[1] = min(2, $position[1] + 1);
67-
return $position;
68-
}
63+
$x = $position[0];
64+
$y = $position[1];
6965

70-
private function moveLeft($position): array
71-
{
72-
$position[0] = max(0, $position[0] - 1);
73-
return $position;
74-
}
66+
switch ($direction) {
67+
case 'U':
68+
$y = max(0, $y - 1);
69+
break;
70+
case 'D':
71+
$y = min(count($keypad) - 1, $y + 1);
72+
break;
73+
case 'L':
74+
$x = max(0, $x - 1);
75+
break;
76+
case 'R':
77+
$x = min(count($keypad[$y]) - 1, $x + 1);
78+
break;
79+
}
80+
81+
// Only update position if within bounds of the keypad layout
82+
if ($keypad[$y][$x] !== null) {
83+
$position = [$x, $y];
84+
}
7585

76-
private function moveRight($position): array
77-
{
78-
$position[0] = min(2, $position[0] + 1);
7986
return $position;
8087
}
81-
}
88+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy