Skip to content

Instantly share code, notes, and snippets.

@Achocholacek
Created December 2, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Achocholacek/c93f71cd6608da38e09b to your computer and use it in GitHub Desktop.
Save Achocholacek/c93f71cd6608da38e09b to your computer and use it in GitHub Desktop.
Advent of Code 2015 - Day 2 Exercise 2
<?php
$totalRibbon = 0;
function howMuchRibbon($dimension) {
$dimensions = Array();
$dimensions = explode("x",$dimension);
$length = $dimensions[0];
$width = $dimensions[1];
$height = $dimensions[2];
$side1 = $length * $width;
$side2 = $width * $height;
$side3 = $length * $height;
$side1p = $length + $length + $width + $width;
$side2p = $height + $height + $width + $width;
$side3p = $length + $length + $height + $height;
$smallestPer = min($side1p,$side2p,$side3p);
$bowRibbon = $length * $width * $height;
$ribbonNeeded = $smallestPer + $bowRibbon;
return intval($ribbonNeeded);
}
$handle = @fopen("/home/arthur/adventcode/presents.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$totalRibbon = $totalRibbon + howMuchRibbon($buffer);
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
echo "Total Ribbon is " . $totalRibbon;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment