-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.php
executable file
·70 lines (58 loc) · 1.98 KB
/
generate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env php
<?php
declare(strict_types=1);
use App\Aoc\InputFetcher;
use App\Aoc\SolutionFactory;
require_once './vendor/autoload.php';
$noInteraction = (bool)getenv('NO_INTERACTION');
[, $year, $day, $name] = array_pad($argv, 4, null);
/** @var SolutionFactory $solutionsFactory */
$solutionsFactory = require 'config/solutionFactory.php';
/** @var InputFetcher $fetcher */
$fetcher = require 'config/fetcher.php';
$nextChallenge = $solutionsFactory->mostRecentChallenge()->next();
$ask = static fn (string $prompt, string|int $default) => $noInteraction
? $default
: (readline("$prompt [$default]: ") ?: $default);
$yearRange = range(2015, 2022);
while (false === in_array((int)$year, $yearRange, true)) {
$year = $ask('Please specify year', $nextChallenge->year);
}
$dayRange = range(1, 25);
while (false === in_array((int)$day, $dayRange, true)) {
$day = $ask('Please specify day', $nextChallenge->day);
}
if (!$name) {
$html = file_get_contents("https://adventofcode.com/$nextChallenge->year/day/$nextChallenge->day");
preg_match('/<h2>--- Day \d+: (.+) ---<\/h2>/', $html, $match);
$default = end($match);
$name = $ask('Please specify the name', $default);
}
$source = __DIR__ . '/template/';
$dayPad = sprintf('%02d', $day);
$name = implode(
"",
array_map(
ucfirst(...),
preg_split("/[^[a-z0-9]+/i", strtr($name, ['\'' => ''])),
),
);
$targetDir = __DIR__ . "/src/Solutions/Y$year/D$dayPad";
echo "Generating Y$year/D$dayPad/$name\n";
is_dir($targetDir) || mkdir($targetDir, recursive: true);
foreach (glob($source . '*.php') as $file) {
file_put_contents(
$targetDir . '/' . str_replace('NAME', $name, basename($file)),
strtr(
file_get_contents($file),
[
'0000' => $year,
'00' => $dayPad,
'0' => $day,
'NAME' => $name,
],
),
);
}
echo "Fetching input for $nextChallenge\n";
$fetcher->fetch($nextChallenge);