/
day5.php
166 lines (126 loc) · 2.63 KB
/
day5.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
$path = explode("\\", __FILE__);
$file = array_pop($path);
$day = substr(explode(".", $file)[0], 3);
$input = file_get_contents("input/day" . $day . ".txt");
include("test.php");
/**
First half
*/
echo '<h1>Day ' . $day . ' - First half</h1>';
function contains($haystack, $needles = []) {
if(preg_match('/(' . implode('|', $needles) . ')/', $haystack)) {
return true;
}
return false;
}
function vowels($haystack, $vowels = []) {
$regex = '[' . implode('', $vowels) . ']';
if(preg_match('/' . $regex . '.*' . $regex .'.*' . $regex . '/', $haystack)) {
return true;
}
return false;
}
function part_1($line) {
global $not_contains, $vowels;
if(contains($line, $not_contains)) {
return false;
}
if(!vowels($line, $vowels)) {
return false;
}
if(!preg_match('/([a-z])\1{1,}/', $line)) {
return false;
}
return true;
}
$nice = $naughty = 0;
$not_contains = [
'ab',
'cd',
'pq',
'xy'
];
$vowels = [
'a',
'e',
'i',
'o',
'u'
];
$time = microtime(true);
foreach(explode("\n", $input) as $line) {
if(part_1($line)) {
$nice++;
continue;
}
$naughty++;
}
echo 'Nice: ' . $nice . '<br>';
echo 'Naughty: ' . $naughty . '<br>';
echo 'Time: ' . round(microtime(true) - $time, 6) . 's';
$part1_tests = [
'ugknbfddgicrmopn' => true,
'aaa' => true,
'jchzalrnumimnmhp' => false,
'haegwjzuvuyypxyu' => false,
'dvszwmarrgswjxmb' => false,
];
echo '<h2>Tests:</h2>';
foreach($part1_tests as $k => $v) {
echo test_input(part_1($k), $v, $k);
}
/**
Second half
*/
echo '<h1>Day ' . $day . ' - First half</h1>';
function part_2($line) {
/**
Check for repeating letter with exactly one letter in between them (xyx, acdefeghi (efe), aaa)
*/
$potential_nice = 0;
for($i = 2; $i < strlen($line); $i++) {
if($line[$i] === $line[$i - 2]) {
$potential_nice++;
}
}
if($potential_nice === 0) {
return false;
}
/**
Pair of any two letters (xyxy, aabcdefgaa) without overlap, so no (aaa)
*/
$potential_nice = 0;
for($i = 1; $i < strlen($line); $i++) {
if(substr_count($line, $line[$i-1] . $line[$i]) > 1) {
$potential_nice++;
}
}
if($potential_nice === 0) {
return false;
}
return true;
}
$nice = $naughty = 0;
foreach(explode("\n", $input) as $line) {
if(part_2($line)) {
$nice++;
continue;
}
$naughty++;
}
echo 'Nice: ' . $nice . '<br>';
echo 'Naughty: ' . $naughty . '<br>';
echo 'Time: ' . round(microtime(true) - $time, 6) . 's';
$part2_tests = [
'qjhvhtzxzqqjkmpb' => true,
'xxyxx' => true,
'uurcxstgmygtbstg' => false,
'ieodomkazucvgmuy' => false,
'aaa' => false,
'aaaa' => true,
];
echo '<h2>Tests:</h2>';
foreach($part2_tests as $k => $v) {
echo test_input(part_2($k), $v, $k);
}