You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.4 KiB
PHP
63 lines
1.4 KiB
PHP
<?php
|
|
// https://adventofcode.com/2017/day/4
|
|
|
|
function puzzle1(string $input) : int
|
|
{
|
|
$rows = array_filter(explode(PHP_EOL, $input));
|
|
$sum = 0;
|
|
|
|
foreach ($rows as $row) {
|
|
$words = explode(' ', $row);
|
|
|
|
if (count($words) === count(array_unique($words))) {
|
|
$sum++;
|
|
}
|
|
}
|
|
|
|
return $sum;
|
|
}
|
|
|
|
function puzzle2(string $input) : int
|
|
{
|
|
$rows = array_filter(explode(PHP_EOL, $input));
|
|
$sum = 0;
|
|
|
|
foreach ($rows as $row) {
|
|
$words = explode(' ', $row);
|
|
|
|
// Sort letters in alphabetical order in words
|
|
$words = array_map(function($value) {
|
|
$letters = str_split($value, 1);
|
|
|
|
sort($letters);
|
|
|
|
return implode('', $letters);
|
|
}, $words);
|
|
|
|
// Count unique words
|
|
if (count($words) === count(array_unique($words))) {
|
|
$sum++;
|
|
}
|
|
}
|
|
|
|
return $sum;
|
|
}
|
|
|
|
echo 'First challange sample:'.PHP_EOL;
|
|
echo puzzle1('aa bb cc dd ee
|
|
aa bb cc dd aa
|
|
aa bb cc dd aaa').PHP_EOL.PHP_EOL;
|
|
|
|
echo 'First challange solution:'.PHP_EOL;
|
|
echo puzzle1(file_get_contents(dirname(__FILE__).'/input.txt')).PHP_EOL.PHP_EOL;
|
|
|
|
echo 'Second challenge sample:'.PHP_EOL;
|
|
echo puzzle2('abcde fghij
|
|
abcde xyz ecdab
|
|
a ab abc abd abf abj
|
|
iiii oiii ooii oooi oooo
|
|
oiii ioii iioi iiio').PHP_EOL.PHP_EOL;
|
|
|
|
echo 'Second challange solution:'.PHP_EOL;
|
|
echo puzzle2(file_get_contents(dirname(__FILE__).'/input.txt')).PHP_EOL.PHP_EOL;
|