-
Notifications
You must be signed in to change notification settings - Fork 0
/
4.php
54 lines (43 loc) · 998 Bytes
/
4.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
<?php
$contents = file_get_contents('4-input.txt');
$rows = explode("\n", trim($contents));
$valid = 0;
foreach ($rows as $row) {
$words = explode(" ", $row);
$w = [];
$invalid = false;
foreach ($words as $word) {
if (!isset($w[$word])) {
$w[$word] = true;
continue;
}
$invalid = true;
}
if (! $invalid) {
$valid++;
}
}
echo 'Part A: ' . $valid . PHP_EOL;
$valid = 0;
foreach ($rows as $row) {
$words = explode(" ", $row);
foreach ($words as $idx => &$word) {
$letters = array_filter(preg_split('##', trim($word)));
sort($letters);
$words[$idx] = implode('', $letters);
}
unset($word);
$w = [];
$invalid = false;
foreach ($words as $word) {
if (!isset($w[$word])) {
$w[$word] = true;
continue;
}
$invalid = true;
}
if (! $invalid) {
$valid++;
}
}
echo 'Part B: ' . $valid . PHP_EOL;