Day 3 solution.
This commit is contained in:
parent
7f79c6079a
commit
674175c5ea
96
2015/days/day3.php
Normal file
96
2015/days/day3.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
function part1() {
|
||||
// Get elf instructions.
|
||||
$directions = file_get_contents('./2015/inputs/day3.txt');
|
||||
// Convert the string to an array for processing.
|
||||
$directions = str_split($directions);
|
||||
$position = [0,0];
|
||||
$deliveries = [];
|
||||
foreach ($directions as $direction) {
|
||||
// First, deliver a present at current location.
|
||||
$location_key = $position[0] . '_' . $position[1];
|
||||
$location_key = crypt($location_key, '123');
|
||||
if (isset($deliveries[$location_key])) {
|
||||
$deliveries[$location_key]++;
|
||||
}
|
||||
else {
|
||||
$deliveries[$location_key] = 1;
|
||||
}
|
||||
if ($direction == '^') {
|
||||
$position[1]++;
|
||||
}
|
||||
elseif($direction == 'v') {
|
||||
$position[1]--;
|
||||
}
|
||||
elseif ($direction == '>') {
|
||||
$position[0]++;
|
||||
}
|
||||
elseif ($direction == '<') {
|
||||
$position[0]--;
|
||||
}
|
||||
}
|
||||
|
||||
return $deliveries;
|
||||
}
|
||||
|
||||
function part2() {
|
||||
// Get elf instructions.
|
||||
$directions = file_get_contents('./2015/inputs/day3.txt');
|
||||
// Convert the string to an array for processing.
|
||||
$directions = str_split($directions);
|
||||
$santa_position = [0,0];
|
||||
$robo_position = [0,0];
|
||||
$deliveries = [];
|
||||
$turn = 'santa';
|
||||
foreach ($directions as $direction) {
|
||||
if ($turn == 'santa') {
|
||||
$deliveries = updateDeliveryList($deliveries, $santa_position);
|
||||
$santa_position = changeCoordinates($santa_position, $direction);
|
||||
$turn = 'robo';
|
||||
}
|
||||
elseif ($turn == 'robo') {
|
||||
$deliveries = updateDeliveryList($deliveries, $robo_position);
|
||||
$robo_position = changeCoordinates($robo_position, $direction);
|
||||
$turn = 'santa';
|
||||
}
|
||||
}
|
||||
|
||||
return $deliveries;
|
||||
}
|
||||
|
||||
function updateDeliveryList(array $list, $coords) {
|
||||
$location_key = $coords[0] . '_' . $coords[1];
|
||||
$location_key = crypt($location_key, '123');
|
||||
if (!isset($list[$location_key])) {
|
||||
$list[$location_key] = 1;
|
||||
}
|
||||
else {
|
||||
$list[$location_key]++;
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
function changeCoordinates($coords, $move) {
|
||||
match ($move) {
|
||||
'^' => $coords[1]++,
|
||||
'v' => $coords[1]--,
|
||||
'>' => $coords[0]++,
|
||||
'<' => $coords[0]--,
|
||||
default => null,
|
||||
};
|
||||
|
||||
return $coords;
|
||||
}
|
||||
|
||||
|
||||
function day3($part = 1) {
|
||||
if ($part == 1) {
|
||||
return count(part1());
|
||||
}
|
||||
elseif ($part == 2) {
|
||||
return count(part2());
|
||||
}
|
||||
}
|
||||
|
||||
1
2015/inputs/day3.txt
Normal file
1
2015/inputs/day3.txt
Normal file
File diff suppressed because one or more lines are too long
@ -10,6 +10,8 @@ $solutions['day_1']['part_1'] = day1solution1();
|
||||
$solutions['day_1']['part_2'] = day1solution1b();
|
||||
$solutions['day_2']['part_1'] = day2(1);
|
||||
$solutions['day_2']['part_2'] = day2(2);
|
||||
$solutions['day_3']['part_1'] = day3(1);
|
||||
$solutions['day_3']['part_2'] = day3(2);
|
||||
|
||||
// OUTPUT THE SOLUTIONS WE HAVE.
|
||||
foreach ($solutions as $day => $parts) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user