1
0

AdventOfCode

This commit is contained in:
Mike Schwörer 2020-01-15 00:04:40 +01:00
parent 020515c4af
commit 2b450bdef1
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 37 additions and 137 deletions

View File

@ -1,109 +0,0 @@
<?php
require_once (__DIR__ . '/../internals/base.php');
require_once (__DIR__ . '/../extern/Parsedown.php');
require_once (__DIR__ . '/../extern/ParsedownExtra.php');
class ParsedownCustom extends ParsedownExtra
{
protected function element(array $Element)
{
if (isset($Element['custom']) && $Element['custom'] && isset($Element['handler']))
return $this->{$Element['handler']}($Element['text']);
else
return parent::element($Element);
}
protected function blockFencedCode($Line)
{
$Block = parent::blockFencedCode($Line);
if ($Block === null) return $Block;
$Block['custom'] = false;
if (isset($Block['element']['text']['attributes']))
{
foreach ($Block['element']['text']['attributes'] as $attr)
{
$spl = explode('__', $attr);
if ($spl[0] === 'language-befungerunner')
{
$Block['element']['handler'] = 'handleBef93';
$Block['custom'] = true;
$Block['element']['text']['b93_speed'] = null;
$Block['element']['text']['b93_interactive'] = true;
$Block['element']['text']['b93_editable'] = true;
foreach ($spl as $param)
{
if (startsWith($param, 'speed-')) $Block['element']['text']['b93_speed'] = intval( substr($param, strlen('speed-')));
if (startsWith($param, 'interactive-')) $Block['element']['text']['b93_interactive'] = boolval(substr($param, strlen('interactive-')));
if (startsWith($param, 'editable-')) $Block['element']['text']['b93_editable'] = boolval(substr($param, strlen('editable-')));
}
return $Block;
}
else if ($spl[0] === 'language-bfjoustrunner')
{
$Block['element']['handler'] = 'handleBFJoust';
$Block['custom'] = true;
return $Block;
}
}
}
return $Block;
}
protected function blockFencedCodeComplete($Block)
{
if (! $Block['custom']) { return parent::blockFencedCodeComplete($Block); }
$Block['element']['custom'] = true;
return $Block;
}
protected function handleBFJoust(array $Element)
{
global $PARAM_CODE_LEFT;
global $PARAM_CODE_RIGHT;
$split = preg_split("/\-{16,}/", $Element['text']);
$PARAM_CODE_LEFT = trim($split[0]);
$PARAM_CODE_RIGHT = trim($split[1]);
return require (__DIR__ . '/../fragments/widget_bfjoust.php');
}
protected function handleBef93(array $Element)
{
global $PARAM_BEFUNGE93RUNNER;
$PARAM_BEFUNGE93RUNNER =
[
'code' => $Element['text'],
'url' => '',
'interactive' => $Element['b93_interactive'],
'speed' => $Element['b93_speed'],
'editable' => $Element['b93_editable'],
];
return require (__DIR__ . '/../fragments/widget_befunge93.php');
}
protected function blockTable($Line, array $Block = null)
{
// https://stackoverflow.com/a/46346412/1761622
$Block = parent::blockTable($Line, $Block);
if ($Block === null) return $Block;
if (!key_exists('element', $Block)) return $Block;
$Block['element']['attributes']['class'] = 'stripedtable';
return $Block;
}
}

View File

@ -1,5 +1,7 @@
<?php <?php
require_once 'website.php';
class AdventOfCode class AdventOfCode
{ {
const YEARS = const YEARS =
@ -23,29 +25,36 @@ class AdventOfCode
'ts' => ['ext'=>'ts', 'css'=>'language-typescript', 'name'=>'Typescript'], 'ts' => ['ext'=>'ts', 'css'=>'language-typescript', 'name'=>'Typescript'],
]; ];
public static function listAllFromAllYears() /** @var array */
private $staticData;
public function __construct()
{
$this->load();
}
private function load()
{ {
$all = require (__DIR__ . '/../statics/aoc/__all.php'); $all = require (__DIR__ . '/../statics/aoc/__all.php');
array_walk($all, function(&$value, $year) { array_walk($value, function (&$innervalue) use ($year) { $innervalue = self::readSingle($year, $innervalue); }); }); array_walk($all, function(&$value, $year) { array_walk($value, function (&$innervalue) use ($year) { $innervalue = self::readSingle($year, $innervalue); }); });
return $all; $this->staticData = $all;
} }
public static function listSingleYear($year) public function listAllFromAllYears()
{ {
$all = require (__DIR__ . '/../statics/aoc/__all.php'); return $this->staticData;
$result = $all[$year];
array_walk($result, function(&$value) use ($year) { $value = self::readSingle($year, $value); });
return $result;
} }
public static function listSingleYearAssociative($year) public function listSingleYear($year)
{ {
$all = self::listSingleYear($year); return $this->staticData[$year];
}
public function listSingleYearAssociative($year)
{
$all = $this->listSingleYear($year);
$result = array_fill(0, 25, null); $result = array_fill(0, 25, null);
@ -57,11 +66,9 @@ class AdventOfCode
return $result; return $result;
} }
public static function listYears() public function listYears()
{ {
$all = require (__DIR__ . '/../statics/aoc/__all.php'); return array_keys($this->staticData);
return array_keys($all);
} }
public static function readSingle($year, $a) public static function readSingle($year, $a)
@ -93,7 +100,7 @@ class AdventOfCode
return $a; return $a;
} }
public static function getDayFromStrIdent($year, $ident) public function getDayFromStrIdent($year, $ident)
{ {
$e = explode('-', $ident, 2); // day-xxx $e = explode('-', $ident, 2); // day-xxx
if (count($e)!==2) return null; if (count($e)!==2) return null;
@ -104,25 +111,25 @@ class AdventOfCode
return self::getSingleDay($year, $i); return self::getSingleDay($year, $i);
} }
public static function getSingleDay($year, $day) public function getSingleDay($year, $day)
{ {
foreach (self::listSingleYear($year) as $aocd) { foreach ($this->listSingleYear($year) as $aocd) {
if ($aocd['day'] == $day) return $aocd; if ($aocd['day'] == $day) return $aocd;
} }
return null; return null;
} }
public static function getGithubLink($year) public function getGithubLink($year)
{ {
return self::YEARS['' . $year]['github']; return self::YEARS['' . $year]['github'];
} }
public static function getURLForYear($year) public function getURLForYear($year)
{ {
return '/blog/' . self::YEARS[''.$year]['blog-id'] . '/Advent_of_Code_' . $year . '/'; return '/blog/' . self::YEARS[''.$year]['blog-id'] . '/Advent_of_Code_' . $year . '/';
} }
public static function getPrevYear($year) public function getPrevYear($year)
{ {
$last = null; $last = null;
foreach (self::YEARS as $y => $d) foreach (self::YEARS as $y => $d)
@ -133,7 +140,7 @@ class AdventOfCode
return null; return null;
} }
public static function getNextYear($year) public function getNextYear($year)
{ {
$found = false; $found = false;
foreach (self::YEARS as $y => $d) foreach (self::YEARS as $y => $d)
@ -144,12 +151,12 @@ class AdventOfCode
return null; return null;
} }
public static function getLanguageCSS($data) public function getLanguageCSS($data)
{ {
return self::LANGUAGES[$data['language']]['css']; return self::LANGUAGES[$data['language']]['css'];
} }
public static function getSolutionCode($data, $i) public function getSolutionCode($data, $i)
{ {
$raw = file_get_contents($data['file_solutions'][$i]); $raw = file_get_contents($data['file_solutions'][$i]);
@ -165,11 +172,13 @@ class AdventOfCode
return $raw; return $raw;
} }
public static function checkConsistency() public function checkConsistency()
{ {
$warn = null; $warn = null;
foreach (self::listAllFromAllYears() as $year => $yd) $this->load();
foreach ($this->listAllFromAllYears() as $year => $yd)
{ {
$daylist = []; $daylist = [];
$titlelist = []; $titlelist = [];