This one was pretty straightforward for me, aside from a silly mistake at the end which was making my image a mess. I'm much happier about the puzzles now that I've given myself permission to skip any that involve the word 'intcode'!
$width = 25; $height = 6; $input = file_get_contents("input8.txt"); $num_layers = strlen($input) / ($width * $height); $layer_size = strlen($input) / $num_layers; $layers = str_split($input, $layer_size); $min = get_fewest_zero($layers); echo "Part 1: ".substr_count($layers[$min], "1")*substr_count($layers[$min], "2")."\n"; $image = decode_image($layers, $width, $height); display_image($image); function display_image($image){ foreach ($image as $lines){ foreach ($lines as $pixel){ if ($pixel=="1"){ echo "#"; } else echo " "; } echo "\n"; } } function decode_image($layers, $width, $height){ $final_image = array_fill(0, $height, array_fill(0, $width, " ")); foreach ($layers as $layer){ $lines = str_split($layer, $width); foreach ($lines as $k=>$line){ $chars = str_split($line); foreach ($chars as $m=>$char){ if ($char != "2"){ if ($final_image[$k][$m] == " "){ $final_image[$k][$m] = $char; } } } } } return $final_image; } function get_fewest_zero($layers){ $zeros_per_layer = array(); foreach ($layers as $num=>$layer){ $zeros_per_layer[$num] = substr_count($layer, "0"); } return min(array_keys($zeros_per_layer, min($zeros_per_layer))); }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This one was pretty straightforward for me, aside from a silly mistake at the end which was making my image a mess. I'm much happier about the puzzles now that I've given myself permission to skip any that involve the word 'intcode'!