-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4b.php
More file actions
66 lines (51 loc) · 2.06 KB
/
4b.php
File metadata and controls
66 lines (51 loc) · 2.06 KB
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
63
64
65
66
<?php
/**
--- Day 4: High-Entropy Passphrases ---
A new system policy has been put in place that requires all accounts to use a passphrase instead of simply a password. A passphrase consists of a series of words (lowercase letters) separated by spaces.
To ensure security, a valid passphrase must contain no duplicate words.
For example:
aa bb cc dd ee is valid.
aa bb cc dd aa is not valid - the word aa appears more than once.
aa bb cc dd aaa is valid - aa and aaa count as different words.
The system's full passphrase list is available as your puzzle input. How many passphrases are valid?
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
For added security, yet another system policy has been put in place. Now, a valid passphrase must contain no two words that are anagrams of each other - that is, a passphrase is invalid if any word's letters can be rearranged to form any other word in the passphrase.
For example:
abcde fghij is a valid passphrase.
abcde xyz ecdab is not valid - the letters from the third word can be rearranged to form the first word.
a ab abc abd abf abj is a valid passphrase, because all letters need to be used when forming another word.
iiii oiii ooii oooi oooo is valid.
oiii ioii iioi iiio is not valid - any of these words can be rearranged to form any other word.
Under this new system policy, how many passphrases are valid?
*/
$input = <<<INPUT
abcde fghij
abcde xyz ecdab
a ab abc abd abf abj
iiii oiii ooii oooi oooo
oiii ioii iioi iiio
INPUT;
$passphrases = explode("\n", $input);
$valid = 0;
foreach ($passphrases as $passphrase) {
$invalid = FALSE;
$words = explode(' ', $passphrase);
for ($i = 0; $i < count($words); $i++) {
$iword = str_split($words[$i]);
sort($iword);
for ($j = $i + 1; $j < count($words); $j++) {
$jword = str_split($words[$j]);
sort($jword);
if ($iword == $jword) {
$invalid = TRUE;
break(2);
}
}
}
if (!$invalid) {
print $passphrase . "\n";
$valid++;
}
}
print $valid . "\n";