@@ -8,42 +8,34 @@ class Solution2016Day2 extends AbstractSolution
8
8
{
9
9
public function solvePart1 ($ input ): string
10
10
{
11
- $ startposition = [1 , 1 ];
12
-
13
11
$ code = '' ;
14
-
15
- // for each input line, move according to each letter
12
+ $ position = [1 , 1 ]; // Start at '5' on a 3x3 grid
16
13
17
14
foreach ($ input as $ line ) {
18
- $ position = $ startposition ;
19
15
$ instructions = str_split ($ line );
20
16
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 ());
35
18
}
36
19
$ code .= $ this ->keypad ()[$ position [1 ]][$ position [0 ]];
37
20
}
38
-
39
- return "The code is: <info> $ code</info> " ;
21
+
22
+ return "The code for Part 1 is: <info> $ code</info> " ;
40
23
}
41
24
42
25
public function solvePart2 ($ input ): string
43
26
{
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> " ;
47
39
}
48
40
49
41
private function keypad (): array
@@ -55,27 +47,42 @@ private function keypad(): array
55
47
];
56
48
}
57
49
58
- private function moveUp ( $ position ): array
50
+ private function keypad2 ( ): array
59
51
{
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
+ ];
62
59
}
63
60
64
- private function moveDown ( $ position ): array
61
+ private function move ( array $ position, string $ direction , array $ keypad ): array
65
62
{
66
- $ position [1 ] = min (2 , $ position [1 ] + 1 );
67
- return $ position ;
68
- }
63
+ $ x = $ position [0 ];
64
+ $ y = $ position [1 ];
69
65
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
+ }
75
85
76
- private function moveRight ($ position ): array
77
- {
78
- $ position [0 ] = min (2 , $ position [0 ] + 1 );
79
86
return $ position ;
80
87
}
81
- }
88
+ }
0 commit comments