Skip to content

Commit 0339f59

Browse files
use regex for string manipulation too...
1 parent c8a3b8f commit 0339f59

File tree

1 file changed

+2
-16
lines changed

1 file changed

+2
-16
lines changed

2024-php/03.php

+2-16
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
$time_start = microtime(true);
33
$input = trim(file_get_contents('03.txt'));
44

5-
function op($in): int
5+
function op(string $in): int
66
{
77
$matches = [];
88
preg_match_all('/mul\((\d{1,3}),(\d{1,3})\)/', $in, $matches);
@@ -16,21 +16,7 @@ function op($in): int
1616
$pt1 = op($input);
1717

1818
// for part 2, we use the same regex but first do some string manipulation
19-
while (true) {
20-
$pos = strpos($input, "don't()");
21-
if ($pos === false) {
22-
break;
23-
}
24-
25-
$new = substr($input, 0, $pos);
26-
27-
$next = strpos($input, "do()", $pos+1);
28-
if ($next !== false) {
29-
$new .= substr($input, $next);
30-
}
31-
$input = $new;
32-
}
33-
19+
$input = preg_replace('/don\'t\(\)(?:.|\n)*?do\(\)/', '', $input);
3420
$pt2 = op($input);
3521

3622
echo "--- Day 3: Mull It Over ---", PHP_EOL;

0 commit comments

Comments
 (0)