[2017-12-04] Solutions for both puzzles

master
Elis Hirwing 6 years ago
parent d6143fcaef
commit 35d6b5b3a9
Signed by: etu
GPG Key ID: D57EFA625C9A925F

@ -10,6 +10,8 @@ day3:
php day3/main.php
day4:
php day4/main.php
day5:
day6:
day7:

@ -0,0 +1,44 @@
<?php
// https://adventofcode.com/2017/day/4
function puzzle1(string $input) : int
{
$rows = 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 = 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;
}
Loading…
Cancel
Save