Skip to content

Commit

Permalink
Day 3 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
marshall-davis committed Dec 3, 2024
1 parent 61def8f commit ab7c39b
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions 2024/03/puzzle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@

$input = fopen('input.txt', 'r+');
$memory = file_get_contents('input.txt');
$matches = [];

preg_match_all('/mul\((\d{1,3}),(\d{1,3})\)/', $memory, $matches);

$sum = 0;

$parts = explode('don\'t()', $memory);

$initial = array_shift($parts);
preg_match_all('/mul\((\d{1,3}),(\d{1,3})\)/', $initial, $matches);
foreach ($matches[1] as $occurrence => $match) {
$sum += $match * $matches[2][$occurrence];
}
echo "Initially: $sum\n";
foreach ($parts as $part => $segment) {

$matches = [];
$start = strpos($segment, 'do()');
if ($start === false) {
continue;
}
$instruction = substr($segment, $start + 4);
echo $instruction."\n\n";
preg_match_all('/mul\((\d{1,3}),(\d{1,3})\)/', $instruction, $matches);
foreach ($matches[1] as $occurrence => $match) {
$sum += $match * $matches[2][$occurrence];
}
}

assert(173529487 >= $sum);

echo $sum. PHP_EOL;
echo $sum.PHP_EOL;

0 comments on commit ab7c39b

Please sign in to comment.