Created
December 2, 2015 19:42
-
-
Save Achocholacek/c93f71cd6608da38e09b to your computer and use it in GitHub Desktop.
Advent of Code 2015 - Day 2 Exercise 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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