1
0
www.mikescher.com/www/internals/modules/euler.php

116 lines
3.0 KiB
PHP
Raw Permalink Normal View History

2020-01-14 22:25:43 +01:00
<?php
2017-11-08 17:39:50 +01:00
2020-01-19 13:16:40 +01:00
class Euler implements IWebsiteModule
2017-11-08 17:39:50 +01:00
{
2020-01-15 00:32:24 +01:00
/** @var array */
private $staticData;
public function __construct()
{
$this->load();
}
private function load()
{
2020-01-15 02:50:23 +01:00
$all = require (__DIR__ . '/../../statics/euler/__all.php');
2020-01-15 00:32:24 +01:00
$this->staticData = array_map(function($a){return self::readSingle($a);}, $all);
}
private static function readSingle($a)
2017-11-08 17:39:50 +01:00
{
2018-01-01 21:07:48 +01:00
$n3p = str_pad($a['number'], 3, '0', STR_PAD_LEFT);
$a['number3'] = $n3p;
2017-11-08 17:39:50 +01:00
$a['rating'] = self::rateTime($a);
2018-01-01 21:07:48 +01:00
$a['url'] = '/blog/1/Project_Euler_with_Befunge/problem-' . $n3p;
2017-12-31 17:53:59 +01:00
$a['canonical'] = "https://www.mikescher.com" . $a['url'];
2018-01-01 21:07:48 +01:00
2017-12-31 17:53:59 +01:00
$a['is93'] = ($a['width'] <= 80 AND $a['height'] <= 25);
2018-01-01 21:07:48 +01:00
$a['url_euler'] = 'https://projecteuler.net/problem=' . $n3p;
$a['url_raw'] = 'https://raw.githubusercontent.com/Mikescher/Project-Euler_Befunge/master/processed/Euler_Problem-' . $n3p . '.b93';
$a['url_github'] = 'https://github.com/Mikescher/Project-Euler_Befunge';
2020-01-17 00:25:29 +01:00
$a['file_description'] = (__DIR__ . '/../../statics/euler/Euler_Problem-'.$n3p.'_description.md');
$a['file_code'] = (__DIR__ . '/../../statics/euler/Euler_Problem-'.$n3p.'.b93');
$a['file_explanation'] = (__DIR__ . '/../../statics/euler/Euler_Problem-'.$n3p.'_explanation.md');
2018-01-01 21:07:48 +01:00
2017-11-08 17:39:50 +01:00
return $a;
}
2020-01-15 00:32:24 +01:00
private static function rateTime($problem)
2017-11-08 17:39:50 +01:00
{
2020-01-15 00:32:24 +01:00
if ($problem['time'] < 100) // < 100ms
return 0;
2018-01-03 22:08:56 +01:00
2020-01-15 00:32:24 +01:00
if ($problem['time'] < 15 * 1000) // < 5s
return 1;
if ($problem['time'] < 60 * 1000) // < 1min
return 2;
if ($problem['time'] < 5 * 60 * 1000) // < 5min
return 3;
return 4;
2017-11-08 17:39:50 +01:00
}
2020-01-15 00:32:24 +01:00
public function listAll()
{
return $this->staticData;
}
public function getEulerProblemFromStrIdent($ident)
2018-01-02 19:41:41 +01:00
{
2019-11-02 20:19:34 +01:00
$e = explode('-', $ident, 2); // problem-xxx
2018-01-02 19:41:41 +01:00
if (count($e)!==2) return null;
$i = intval($e[1], 10);
if ($i == 0) return null;
return self::getEulerProblem($i);
}
2020-01-15 00:32:24 +01:00
public function getEulerProblem($num)
2018-01-01 21:07:48 +01:00
{
foreach (self::listAll() as $ep) {
if ($ep['number'] == $num) return $ep;
}
return null;
}
2020-01-15 00:32:24 +01:00
public function checkConsistency()
2018-01-27 14:21:34 +01:00
{
$warn = null;
2020-01-15 00:32:24 +01:00
$this->load();
2018-01-27 14:21:34 +01:00
$numbers = [];
$realname = [];
2020-01-15 00:32:24 +01:00
foreach ($this->staticData as $ep)
2018-01-27 14:21:34 +01:00
{
if (in_array($ep['number'], $numbers)) return ['result'=>'err', 'message' => 'Duplicate number ' . $ep['number']];
$numbers []= $ep['number'];
if (in_array($ep['title'], $realname)) return ['result'=>'err', 'message' => 'Duplicate title ' . $ep['title']];
$realname []= $ep['title'];
if (!file_exists($ep['file_description'])) return ['result'=>'err', 'message' => 'file_description not found ' . $ep['file_description']];
if (!file_exists($ep['file_code'])) return ['result'=>'err', 'message' => 'file_code not found ' . $ep['file_code']];
if (!file_exists($ep['file_explanation'])) return ['result'=>'err', 'message' => 'file_explanation not found ' . $ep['file_explanation']];
}
if ($warn != null) return $warn;
return ['result'=>'ok', 'message' => ''];
}
2020-01-23 20:41:04 +01:00
public function getOverviewURL()
{
return '/blog/1/Project_Euler_with_Befunge';
}
2017-11-08 17:39:50 +01:00
}