This December I participated in Advent of Code.

The program was rather easy to build. This day I had to learn a new trick to scale it: Memoization. Question, early version of part 1. I only show part 2 below, because the only difference between the 2 parts is a bit at the end of the program. (Probably there were more differences originally, but I forgot to save the part 1 version.)
Part 2:
<?php
function dig($design, $level) {
// towels are at most 8 characters long
global $towels, $dug;
if(isset($dug[$design])) {
return $dug[$design];
}
$hit = 0;
for($i=8;$i>0;$i--) {
// if the beginning of this design is towel
if(in_array(substr($design, 0, $i), $towels)) {
if(strlen($design) == $i) {
$dug[$design] = 1;
$hit += 1;
}
$tmphit = dig(substr($design, $i), $level+1);
if($tmphit > 0) {
$dug[$design] = $tmphit;
$hit += $tmphit;
}
}
}
$dug[$design] = $hit;
return $hit;
}
///////////////////////////////////////////////////////////
$input = file_get_contents('./d19input2.txt', true);
$phase = 1;
foreach(preg_split("/((\r?\n)|(\r\n?))/", $input) as $line) {
if(strlen($line)>2) {
if($phase == 1) {
$towels = explode(", ", $line);
} else {
$designs[] = $line;
}
} else {
$phase = 2;
}
}
$hits = 0;
foreach($designs as $design) {
echo "$design\n";
$tmphit = dig($design, 0);
if($tmphit > 0) {
// part 1:
//$hits++;
// part 2:
$hits += $tmphit;
}
}
echo $hits."\n";