Day 2 solution.

This commit is contained in:
Dan Chadwick 2024-11-24 10:03:05 -05:00
parent 5e5c726ebb
commit f39448a54e
4 changed files with 1113 additions and 12 deletions

View File

@ -8,15 +8,12 @@
* Runtime: ~0.158ms
*/
function day1solution1() {
$start_time = microtime(true);
$instructions = file_get_contents('./2015/inputs/day1.txt');
$up_floors = substr_count($instructions, "(");
$down_floors = substr_count($instructions, ")");
$total = $up_floors - $down_floors;
echo "The answer is $total\n";
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);
echo $execution_time * 1000 . "\n";
return $total;
}
/*
@ -25,7 +22,6 @@ function day1solution1() {
* Runtime: ~.33ms
*/
function day1solution1b() {
$start_time = microtime(true);
$instructions = file_get_contents('./2015/inputs/day1.txt');
$chars = str_split($instructions);
$position = 1;
@ -42,8 +38,6 @@ function day1solution1b() {
$position++;
}
};
echo "The position is $position\n";
$end_time = microtime(true);
$execution_time = ($end_time - $start_time);
echo $execution_time * 1000 . "\n";
return $position;
}

98
2015/days/day2.php Normal file
View File

@ -0,0 +1,98 @@
<?php
class Box {
protected $length;
protected $width;
protected $height;
/**
* @param mixed $property
*/
public function get($property): int|string {
return $this->{$property};
}
/**
* @param mixed $property
* @param mixed $value
*/
public function set($property, $value): void {
$this->{$property} = $value;
}
/**
* Calculates the required sq footage of paper needed.
*/
public function calculateRequiredFootage(): int {
$side_1 = $this->length * $this->width;
$side_2 = $this->width * $this->height;
$side_3 = $this->height * $this->length;
$extra = min($side_1, $side_2, $side_3);
$area =
(2 * $side_1) +
(2 * $side_2) +
(2 * $side_3);
return $area + $extra;
}
public function calculateShortestPerimeter() {
$perimeter_1 = 2 * $this->length + 2 * $this->width;
$perimeter_2 = 2 * $this->width + 2 * $this->height;
$perimeter_3 = 2 * $this->height + 2 * $this->length;
return min($perimeter_1, $perimeter_2, $perimeter_3);
}
/*
* Extracts the dimensions from a string.
*/
public function extractDimensions(string $dimensions): void {
$dimensions_exploded = explode("x", $dimensions);
$this->length = $dimensions_exploded[0];
$this->width = $dimensions_exploded[1];
$this->height = $dimensions_exploded[2];
}
public function calculateRibbonRequirements() {
$bow = $this->length * $this->width * $this->height;
$shortest_perimeter = $this->calculateShortestPerimeter();
return $bow + $shortest_perimeter;
}
}
function day2part1() {
$box_dimension_list = file_get_contents('./2015/inputs/day2.txt');
$box_dimension_list = explode(PHP_EOL, $box_dimension_list);
$wrapping_paper_total = 0;
foreach ($box_dimension_list as $box_dimensions) {
if (strlen($box_dimensions) < 1) {
continue;
}
$box = new Box();
$box->extractDimensions($box_dimensions);
$wrapping_paper_total = $wrapping_paper_total + $box->calculateRequiredFootage();
}
return $wrapping_paper_total;
}
function day2part1b() {
$box_dimension_list = file_get_contents('./2015/inputs/day2.txt');
$box_dimension_list = explode(PHP_EOL, $box_dimension_list);
$ribbon_requirements = 0;
foreach ($box_dimension_list as $box_dimensions) {
if (strlen($box_dimensions) < 1) {
continue;
}
$box = new Box();
$box->extractDimensions($box_dimensions);
$ribbon_requirements = $ribbon_requirements + $box->calculateRibbonRequirements();
}
return $ribbon_requirements;
}

1000
2015/inputs/day2.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,15 @@ foreach ($days as $day) {
require($day);
}
$d1 = day1solution1();
$d1b = day1solution1b();
$solutions = [];
$solutions['day_1']['part_1'] = day1solution1();
$solutions['day_1']['part_2'] = day1solution1b();
$solutions['day_2']['part_1'] = day2part1();
$solutions['day_2']['part_2'] = day2part1b();
// OUTPUT THE SOLUTIONS WE HAVE.
foreach ($solutions as $day => $parts) {
foreach ($parts as $part => $answer) {
echo "*** The answer to $day $part is: $answer ***\n";
}
}