-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_day
executable file
·139 lines (120 loc) · 4.01 KB
/
new_day
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env php
<?php
$url_base = $argv[1];
// Start by fetching the puzzle description
$curl = curl_init();
if ($curl === false) {
die("Curl initialization failed\n");
}
if (curl_setopt_array($curl, [
CURLOPT_COOKIEFILE => __DIR__.'/../cookies.txt',
CURLOPT_FAILONERROR => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url_base,
]) === false) {
die("Setting curl options failed\n");
}
// Load HTML
$html = curl_exec($curl);
if ($html === false) {
die("Unable to load puzzle description\n");
}
$whole_doc = new DOMDocument();
$snippet = new DOMDocument();
$whole_doc->loadHTML($html);
$day = 'mysteryday';
$title = null;
// This dangerous-looking while is needed because adoptNode() alters
// the iterator. So this runs until there are articles left.
while (true) {
foreach ($whole_doc->getElementsByTagName('article') as $article) {
// Adopt a node
$snippet->appendChild($snippet->adoptNode($article));
// Mangle the title
foreach ($article->getElementsByTagName("h2") as $h2) {
if (preg_match('/^-* (.*) -*$/', $h2->textContent, $matches) !== 1) {
// No match, no touch
break;
}
// Store title
if ($title === null) {
// Upgrade to h1
$h1 = $snippet->createElement('h1');
$h1->textContent = $matches[1];
$h2->replaceWith($h1);
// Store the title, even more compact if possible
if (preg_match('/^Day ([0-9]*): *(.*)$/', $matches[1], $matches_title) !== 1) {
// If regex doesn't match, as such
$title = $matches[1];
} else {
// If it work, the compacted title
$day = $matches_title[1];
$title = $matches_title[2];
}
} else {
// Just mangle, not replace
$h2->textContent = $matches[1];
}
// Stop after 1st h2 element
break;
}
continue 2;
}
break;
}
$fds = [
0 => ['pipe', 'r'],
1 => STDOUT,
2 => STDERR,
];
$day_padded = sprintf("%02s", $day);
$pdf_file = __DIR__."/../puzzles/$day_padded.pdf";
$res = proc_open('pandoc -f html -V geometry:margin=1.5cm -V documentclass:report -V papersize:a5 -o '.escapeshellarg($pdf_file), $fds, $pipes);
if ($res === false) {
die("Unable to run pandoc\n");
}
if (fwrite($pipes[0], $snippet->saveHTML()) === false) {
die("Pandoc did not consume input\n");
}
if (proc_close($res) !== 0) {
die("Pandoc failed\n");
}
// Input fetching
if (curl_setopt($curl, CURLOPT_URL, $url_base.'/input') === false) {
die("Setting curl options failed\n");
}
$input = curl_exec($curl);
if ($input === false) {
die("Unable to load puzzle input\n");
}
$puzzle_input_file = __DIR__.'/../inputs/'.$day_padded;
if (file_put_contents($puzzle_input_file, $input) === false) {
die("Unable to write puzzle input\n");
}
print("Puzzle description written to $pdf_file\n");
print("Puzzle input written to $puzzle_input_file\n");
// Logbook info such as file name and day of week
$ts = mktime(0, 0, 0, 12, $day, 2024);
$dayname = date('l', $ts);
$logbook_filename = __DIR__.'/../log.md';
// Logbook append
$handle = fopen($logbook_filename, 'c+');
if ($handle === false) {
die("Unable to open logbook\n");
}
$contents = stream_get_contents($handle);
if ($contents === false) {
die("Unable to read logbook\n");
}
// Find if there's something which resembles puzzle link
if (strstr($contents, "($url_base)") === false) {
// We add the log boilerplate
$log = "\n## Day $day, $dayname\n\nAssignment: [$title]($url_base)\n\nView my code: [Day$day_padded.hs](src/Day$day_padded.hs)\n";
if (fwrite($handle, $log) === false) {
die("Unable to append to logbook");
}
print("Adding logbook template to $logbook_filename\n");
} else {
print("Logbook already populated, skipping that part\n");
}