2015 Day 2
Part 1
Problem
COMPLETE
General solution
COMPLETE
Solution: PHP
COMPLETE
<?php
declare(strict_types=1);
error_reporting(E_ALL);
$fp = fopen('input', 'r');
$paper_order = 0;
while (($line = fgets($fp)) !== false)
{
// fgets includes the trailing line break, so remove it
$line = trim($line);
list($length, $width, $height) = explode('x', $line);
$side_areas = [
$length * $width,
$width * $height,
$height * $length
];
$slack = min($side_areas);
// Each side is repeated twice
$paper_order += array_sum($side_areas) * 2;
$paper_order += $slack;
}
fclose($fp);
print("The elves should order this many square feet of wrapping paper: $paper_order\n");
Part 2
Problem
COMPLETE
Solution: PHP
COMPLETE
<?php
declare(strict_types=1);
error_reporting(E_ALL);
$fp = fopen('input', 'r');
$ribbon_order = 0;
while (($line = fgets($fp)) !== false)
{
// fgets includes the trailing line break, so remove it
$line = trim($line);
list($length, $width, $height) = explode('x', $line);
$side_perimeters = array_map(function($x) { return $x * 2; }, [
$length + $width,
$width + $height,
$height + $length
]);
$perimeter_ribbon = min($side_perimeters);
$volume_ribbon = $length * $width * $height;
$ribbon_order += $perimeter_ribbon;
$ribbon_order += $volume_ribbon;
}
fclose($fp);
print("The elves should order this many feet of ribbon: $ribbon_order\n");