-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.php
62 lines (48 loc) · 1.29 KB
/
code.php
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
<?php
require_once __DIR__ . '/../../lib/php/utils.php';
$input = readInput(__DIR__, PHP_EOL . PHP_EOL);
// Task code
function part01(array $input): int
{
$sum = 0;
foreach ($input as $value) {
$sum += count(
array_unique(
str_split(
str_replace(PHP_EOL, '', $value)
)
)
);
}
return $sum;
}
function part02(array $input): int
{
$sum = 0;
foreach ($input as $line) {
$group = explode(PHP_EOL, $line);
$groupCount = count($group);
$questionList = [];
foreach ($group as $person) {
foreach (str_split($person) as $question) {
$questionList[$question] = ($questionList[$question] ?? 0) + 1;
}
}
$sum += count(array_filter($questionList, static fn ($answers) => $answers === $groupCount));
}
return $sum;
}
// Execute
calcExecutionTime();
$result01 = part01($input);
$result02 = part02($input);
$executionTime = calcExecutionTime();
writeln('Solution Part 1: ' . $result01);
writeln('Solution Part 2: ' . $result02);
writeln('Execution time: ' . $executionTime);
saveBenchmarkTime($executionTime, __DIR__);
// Task test
testResults(
[6748, 3445], // Expected
[$result01, $result02], // Result
);