Skip to content

Instantly share code, notes, and snippets.

@gnutix
Created January 14, 2021 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnutix/df1e2415ce6a5fa57a361d8b1aeb7eb3 to your computer and use it in GitHub Desktop.
Save gnutix/df1e2415ce6a5fa57a361d8b1aeb7eb3 to your computer and use it in GitHub Desktop.
<?php
$deals = [
1 => ['forfait' => 60, 'km' => 0.12],
2 => ['forfait' => 0, 'km' => 0.6],
];
foreach ($deals as $deal => $dealData) {
echo 'Deal '.$deal.': forfait '.($dealData['forfait'] === 0 ? 'gratuit' : 'de '.$dealData['forfait'].'.-').' et '.
($dealData['km'] * 100).' cts le kilomètre.'.PHP_EOL;
}
echo PHP_EOL;
$displayData = [];
foreach (range(1, 300) as $km) {
$bestPrice = PHP_INT_MAX;
$bestPriceDeal = 1;
$data = ['km' => $km];
foreach (array_keys($deals) as $deal) {
$price = $deals[$deal]['forfait'] + ($km * $deals[$deal]['km']);
if ($bestPrice === $price) {
$bestPrice = $price;
$bestPriceDeal = null;
} else if ($bestPrice > $price) {
$bestPrice = $price;
$bestPriceDeal = $deal;
}
$data['deals'][$deal] = number_format($price, 2);
}
foreach ($data['deals'] as $deal => $price) {
$data['deals'][$deal] .= ($deal === $bestPriceDeal ? ' (best)'."\t" : "\t\t");
}
$displayData[] = $data;
}
$t = "\t";
$line = '+--------------------------------------------------------------+'.PHP_EOL;
$header = "|{$t}Km{$t}|{$t}Deal 1{$t}{$t}|{$t}Deal 2{$t}{$t}|".PHP_EOL;
echo $line.$header.$line;
foreach ($displayData as $displayDataItem) {
echo "|{$t}{$displayDataItem['km']}{$t}|";
foreach ($displayDataItem['deals'] as $deal => $price) {
echo "{$t}${price}|";
}
echo PHP_EOL;
}
echo $line;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment