Created
December 2, 2015 19:41
-
-
Save Achocholacek/beb10930c4b5490c769b to your computer and use it in GitHub Desktop.
Advent of Code 2015 - Day 2 Exercise 1
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 | |
$totalPaper = 0; | |
function howMuchPaper($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; | |
$smallestSide = min($side1, $side2, $side3); | |
$paperNeeded = (2 * $side1) + (2 * $side2) + (2 * $side3); | |
$paperNeeded = $paperNeeded + $smallestSide; | |
return intval($paperNeeded); | |
} | |
$handle = @fopen("/home/arthur/adventcode/presents.txt", "r"); | |
if ($handle) { | |
while (($buffer = fgets($handle, 4096)) !== false) { | |
$totalPaper = $totalPaper + howMuchPaper($buffer); | |
} | |
if (!feof($handle)) { | |
echo "Error: unexpected fgets() fail\n"; | |
} | |
fclose($handle); | |
} | |
echo "Total Paper is " . $totalPaper; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment