Stuck on day 5.
This commit is contained in:
parent
d17bf4218b
commit
4cad47d04e
78
2015/days/day5.php
Normal file
78
2015/days/day5.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* It contains at least three vowels (aeiou only), like aei, xazegov, or aeiouaeiouaeiou.
|
||||
* It contains at least one letter that appears twice in a row, like xx, abcdde (dd), or aabbccdd (aa, bb, cc, or dd).
|
||||
* It does not contain the strings ab, cd, pq, or xy, even if they are part of one of the other requirements.
|
||||
*/
|
||||
|
||||
|
||||
// What do i need to do for each string?
|
||||
// Count vowels, check if there are >=3
|
||||
// Count unique chars, ensure one char appears twice
|
||||
// Check for a certain set of strings to omit.
|
||||
|
||||
function checkNice(string $string): bool {
|
||||
preg_match_all('/[aeiou]/i', $string, $vowels);
|
||||
if (count($vowels[0]) < 3) {
|
||||
return FALSE;
|
||||
}
|
||||
if (!preg_match('/(.)\1/', $string, $match)){
|
||||
return FALSE;
|
||||
}
|
||||
$omit_strings = [
|
||||
'ab',
|
||||
'cd',
|
||||
'pq',
|
||||
'xy',
|
||||
];
|
||||
foreach ($omit_strings as $omit) {
|
||||
if (str_contains($string, $omit)) {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
function checkNice2(string $string): bool {
|
||||
$two_char_sets = str_split($string, 2);
|
||||
$str_to_arr = str_split($string);
|
||||
$counts = array_count_values($two_char_sets);
|
||||
$is_nice = FALSE;
|
||||
foreach ($counts as $substr => $count) {
|
||||
if ($count >= 2) {
|
||||
$is_nice = TRUE;
|
||||
preg_match_all('/(.)(.)\1/', $string, $matches);
|
||||
if (count($matches[0]) > 1) {
|
||||
$is_nice = TRUE;
|
||||
}
|
||||
else {
|
||||
$is_nice = FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $is_nice;
|
||||
}
|
||||
|
||||
function day5($part = 1) {
|
||||
$file = new SplFileObject("./2015/inputs/day5.txt");
|
||||
// Loop until we reach the end of the file.
|
||||
$nice_strings = 0;
|
||||
while (!$file->eof()) {
|
||||
// Echo one line from the file.
|
||||
if ($part == 1) {
|
||||
$is_nice = checkNice($file->fgets());
|
||||
}
|
||||
elseif ($part == 2) {
|
||||
$is_nice = checkNice2($file->fgets());
|
||||
}
|
||||
if ($is_nice) {
|
||||
$nice_strings++;
|
||||
}
|
||||
}
|
||||
// Unset the file to call __destruct(), closing the file handle.
|
||||
$file = null;
|
||||
|
||||
return $nice_strings;
|
||||
}
|
||||
1000
2015/inputs/day5.txt
Normal file
1000
2015/inputs/day5.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -6,14 +6,17 @@ foreach ($days as $day) {
|
||||
}
|
||||
|
||||
$solutions = [];
|
||||
$solutions['day_1']['part_1'] = day1solution1();
|
||||
$solutions['day_1']['part_2'] = day1solution1b();
|
||||
$solutions['day_2']['part_1'] = day2(1);
|
||||
$solutions['day_2']['part_2'] = day2(2);
|
||||
$solutions['day_3']['part_1'] = day3(1);
|
||||
$solutions['day_3']['part_2'] = day3(2);
|
||||
$solutions['day_4']['part_1'] = day4(1);
|
||||
$solutions['day_4']['part_2'] = day4(2);
|
||||
/* $solutions['day_1']['part_1'] = day1solution1(); */
|
||||
/* $solutions['day_1']['part_2'] = day1solution1b(); */
|
||||
/* $solutions['day_2']['part_1'] = day2(1); */
|
||||
/* $solutions['day_2']['part_2'] = day2(2); */
|
||||
/* $solutions['day_3']['part_1'] = day3(1); */
|
||||
/* $solutions['day_3']['part_2'] = day3(2); */
|
||||
/* $solutions['day_4']['part_1'] = day4(1); */
|
||||
/* $solutions['day_4']['part_2'] = day4(2); */
|
||||
|
||||
$solutions['day_5']['part_1'] = day5(1);
|
||||
$solutions['day_5']['part_2'] = day5(2);
|
||||
|
||||
// OUTPUT THE SOLUTIONS WE HAVE.
|
||||
foreach ($solutions as $day => $parts) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user