-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathRobotCleaner.php
More file actions
96 lines (72 loc) · 2.39 KB
/
Copy pathRobotCleaner.php
File metadata and controls
96 lines (72 loc) · 2.39 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace OfficeCleaner1;
class RobotCleaner
{
private $cleanerParser;
private $position;
// Using an associative array to mimic a HashSet for storing coordinates
private $coordHashSet = [];
// This entire block should be set up with some kind of dependency injection
// but for now has been coded like this
private static $north;
private static $south;
private static $east;
private static $west;
private static $northString = "N";
private static $southString = "S";
private static $eastString = "E";
private static $westString = "W";
private static $moveList = [];
private static $directionTable = [];
// block ends
public function __construct()
{
self::$north = new North();
self::$south = new South();
self::$east = new East();
self::$west = new West();
self::$moveList = [
self::$northString,
self::$southString,
self::$eastString,
self::$westString
];
self::$directionTable = [
self::$northString => self::$north,
self::$southString => self::$south,
self::$eastString => self::$east,
self::$westString => self::$west
];
$this->cleanerParser = new RobotCleanerParser(self::$moveList);
}
public function getVisitedPositions()
{
return count($this->coordHashSet);
}
public function parseInput($input)
{
$this->coordHashSet = [];
if (!$this->cleanerParser->parse($input)) {
return false;
}
$this->position = $this->cleanerParser->getStartPosition();
$this->coordHashSet[$this->position->__toString()] = 0;
foreach ($this->cleanerParser->getCommands() as $command) {
if ($command == null) {
continue;
}
$commandParts = explode(' ', $command);
$iterations = intval($commandParts[1]);
$move = self::$directionTable[$commandParts[0]];
for ($count = 0; $count < $iterations; $count++) {
$move->move($this->position);
if (!array_key_exists($this->position->__toString(), $this->coordHashSet)) {
$this->coordHashSet[$this->position->__toString()] = 0;
}
}
}
return true;
}
}
?>