-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.php
60 lines (45 loc) · 1.38 KB
/
02.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
<?php
$input = file_get_contents(__DIR__ . '/02-input.txt');
$codes = explode("\n", trim($input));
$codes_count = count($codes);
echo "Codes scaned: " . $codes_count . "\n";
$twos = $threes = 0;
foreach($codes as $code) {
$letter_frequency = [];
for($i = 0; $i<strlen($code); $i++) {
$letter_frequency[$code[$i]] = isset($letter_frequency[$code[$i]]) ? $letter_frequency[$code[$i]] + 1 : 1;
}
$twos += array_search(2, $letter_frequency, true) !== false ? 1 : 0;
$threes += array_search(3, $letter_frequency, true) !== false ? 1 : 0;
}
$checksum = $twos * $threes;
echo "Twos: $twos\nThrees: $threes\nChecksum: $checksum\n";
$pairs = [];
for($i = 0; $i < $codes_count - 1; $i++) {
for($j = $i + 1; $j < $codes_count; $j++) {
$code1 = $codes[$i];
$code2 = $codes[$j];
$different_char_count = 0;
$letter = 0;
while($different_char_count < 2 && $letter < strlen($code1)) {
if ($code1[$letter] !== $code2[$letter]) {
$different_char_count++;
}
$letter++;
}
if ($different_char_count === 1) {
$pairs[$code1][] = $code2;
}
}
}
foreach($pairs as $first => $similar) {
$code2 = reset($similar);
$common_letters = '';
for($i = 0; $i<strlen($first); $i++) {
if ($first[$i] === $code2[$i]) {
$common_letters .= $first[$i];
}
}
echo implode(', ', array_merge([$first], $similar)) . ' have "' . $common_letters . '" in common' . PHP_EOL;
}
print_r($pairs);