|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DaanMooij\AdventOfCode\Year2015\Day1; |
|
4
|
|
|
|
|
5
|
|
|
use DaanMooij\AdventOfCode\Day; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
|
|
8
|
|
|
class Day1 implements Day |
|
9
|
|
|
{ |
|
10
|
|
|
private const FLOOR_UP = '('; |
|
11
|
|
|
private const FLOOR_DOWN = ')'; |
|
12
|
|
|
private const STARTING_FLOOR = 0; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var array<string> |
|
16
|
|
|
*/ |
|
17
|
|
|
private array $directions = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return void |
|
21
|
|
|
*/ |
|
22
|
|
|
public function loadInput(): void |
|
23
|
|
|
{ |
|
24
|
|
|
$filepath = __DIR__ . "/input.txt"; |
|
25
|
|
|
$file = file_get_contents($filepath); |
|
26
|
|
|
|
|
27
|
|
|
if (!is_string($file)) { |
|
|
|
|
|
|
28
|
|
|
throw new Exception("Could not open input file: {$filepath}"); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$this->directions = str_split($file) ?? []; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return void |
|
36
|
|
|
*/ |
|
37
|
|
|
public function solve(): void |
|
38
|
|
|
{ |
|
39
|
|
|
printf("The resulting floor is: %s\n", $this->calculateFloor(self::STARTING_FLOOR)); |
|
40
|
|
|
printf( |
|
41
|
|
|
"The first direction position that causus Santa to go in the basement is: %s\n", |
|
42
|
|
|
$this->calculateFirstBasementDirectionPosition(self::STARTING_FLOOR) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int $floor |
|
48
|
|
|
* @return int |
|
49
|
|
|
*/ |
|
50
|
|
|
public function calculateFloor(int $floor = self::STARTING_FLOOR): int |
|
51
|
|
|
{ |
|
52
|
|
|
foreach ($this->directions as $direction) { |
|
53
|
|
|
if ($direction === self::FLOOR_UP) { |
|
54
|
|
|
$floor++; |
|
55
|
|
|
} elseif ($direction === self::FLOOR_DOWN) { |
|
56
|
|
|
$floor--; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $floor; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param int $floor |
|
65
|
|
|
* @return int |
|
66
|
|
|
*/ |
|
67
|
|
|
public function calculateFirstBasementDirectionPosition(int $floor = self::STARTING_FLOOR): int |
|
68
|
|
|
{ |
|
69
|
|
|
if ($floor < 0) { |
|
70
|
|
|
return 0; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$basementPostion = 1; |
|
74
|
|
|
$inBasement = false; |
|
75
|
|
|
while (!$inBasement) { |
|
76
|
|
|
foreach ($this->directions as $position => $direction) { |
|
77
|
|
|
if ($direction === self::FLOOR_UP) { |
|
78
|
|
|
$floor++; |
|
79
|
|
|
} elseif ($direction === self::FLOOR_DOWN) { |
|
80
|
|
|
if ($floor === 0) { |
|
81
|
|
|
$basementPostion = $position + 1; |
|
82
|
|
|
$inBasement = true; |
|
83
|
|
|
break; |
|
84
|
|
|
} |
|
85
|
|
|
$floor--; |
|
86
|
|
|
} |
|
87
|
|
|
$basementPostion = $position + 1; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $basementPostion; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array<string> $directions |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setDirections(array $directions): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->directions = $directions; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return array<string> |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getDirections(): array |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->directions; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|