Skip to content

Instantly share code, notes, and snippets.

@mrbellek
Last active December 7, 2023 15:02
Show Gist options
  • Save mrbellek/5f451bf80668f8c6c4c94db43dc0d98a to your computer and use it in GitHub Desktop.
Save mrbellek/5f451bf80668f8c6c4c94db43dc0d98a to your computer and use it in GitHub Desktop.
Advent of Code day 5 - part 1
<?php
$input = file('input.txt', FILE_IGNORE_NEW_LINES);
//$input = file('sample.txt', FILE_IGNORE_NEW_LINES);
$seeds = explode(' ', str_replace('seeds: ', '', $input[0]));
$seedToSoilMap = getMapByHeader($input, 'seed-to-soil map:');
$soilToFertilizerMap = getMapByHeader($input, 'soil-to-fertilizer map:');
$fertilizerToToWaterMap = getMapByHeader($input, 'fertilizer-to-water map:');
$waterToLightMap = getMapByHeader($input, 'water-to-light map:');
$lightToTemperatureMap = getMapByHeader($input, 'light-to-temperature map:');
$temperatureToHumidityMap = getMapByHeader($input, 'temperature-to-humidity map:');
$humidityToLocationMap = getMapByHeader($input, 'humidity-to-location map:');
$locations = [];
foreach ($seeds as $seed) {
$soilForSeed = getDestinationForSource($seed, $seedToSoilMap);
//printf('seed %s moet in soil %s' . PHP_EOL, $seed, $soilForSeed);
$fertilizerForSoil = getDestinationForSource($soilForSeed, $soilToFertilizerMap);
//printf('soil %s moet in fertilizer %s' . PHP_EOL, $soilForSeed, $fertilizerForSoil);
$waterForFertilizer = getDestinationForSource($fertilizerForSoil, $fertilizerToToWaterMap);
//printf('fertilizer %s moet in water %s' . PHP_EOL, $fertilizerForSoil, $waterForFertilizer);
$lightForWater = getDestinationForSource($waterForFertilizer, $waterToLightMap);
$tempForLight = getDestinationForSource($lightForWater, $lightToTemperatureMap);
$humidityToTemp = getDestinationForSource($tempForLight, $temperatureToHumidityMap);
$locationForHumidity = getDestinationForSource($humidityToTemp, $humidityToLocationMap);
$locations[] = $locationForHumidity;
printf('seed %s moet in location %s' . PHP_EOL, $seed, $locationForHumidity);
}
printf('en de laagste location is dus %s' . PHP_EOL, min($locations));
function getDestinationForSource(int $seed, array $map)
{
$sources = $map['source'];
$destinations = $map['destination'];
foreach ($sources as $i => $source) {
if ($source['start'] <= $seed && $source['end'] >= $seed) {
return $seed + ($destinations[$i]['start'] - $source['start']);
}
}
return $seed;
}
function getMapByHeader(array $input, string $header): array
{
$mapLines = [];
$found = false;
foreach ($input as $i => $line) {
if ($line == '' && $found) {
break;
}
if ($found) {
$mapLines[] = $line;
}
if ($line == $header) {
$found = true;
}
}
$map = ['destination' => [], 'source' => []];
foreach ($mapLines as $mapLine) {
$nums = explode(' ', $mapLine);
$map['destination'][] = [
'start' => intval($nums[0]),
'length' => intval($nums[2]),
'end' => $nums[0] + $nums[2] - 1,
];
$map['source'][] = [
'start' => intval($nums[1]),
'length' => intval($nums[2]),
'end' => $nums[1] + $nums[2] - 1,
];
}
return $map;
}
function dd($a, $b = null, $c = null, $d = null)
{
die(var_dump($a, $b, $c, $d));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment