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

Skip to content

Commit 4c74560

Browse files
committed
Day6
1 parent 6669e10 commit 4c74560

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/Days/Day6.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Failedcode\Aoc2022\Days;
4+
5+
class Day6 extends AbstractDay
6+
{
7+
public function solve_part_1(): string
8+
{
9+
$input = $this->util->loadInput(6)[0];
10+
return $this->getSignalLengthPosition($input, 4);
11+
}
12+
13+
public function solve_part_2(): string
14+
{
15+
$input = $this->util->loadInput(6)[0];
16+
return $this->getSignalLengthPosition($input, 14);
17+
}
18+
19+
protected function getSignalLengthPosition($input, $signalLength)
20+
{
21+
for($i = 0; $i < $signalLength; $i += 1) {
22+
$signals[] = substr($input, $i, 1);
23+
}
24+
for($i = $signalLength; $i < strlen($input); $i += 1) {
25+
if (count(array_unique($signals)) === $signalLength) {
26+
return $i;
27+
}
28+
$signals[] = substr($input, $i, 1);
29+
array_shift($signals);
30+
}
31+
return "Error: no position found";
32+
}
33+
}

0 commit comments

Comments
 (0)
0