Repository navigation
Expand file tree
/
Copy pathAdventOfCode.php
More file actions
170 lines (135 loc) · 4.14 KB
/
Copy pathAdventOfCode.php
File metadata and controls
170 lines (135 loc) · 4.14 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
abstract class AdventOfCode
{
protected int $day;
protected int $year;
protected string $title;
protected bool $test;
public function __construct(bool $test = false)
{
$this->test = $test;
}
abstract protected function setup(): void;
abstract protected function solve_part_one(): string;
// solve_part_two() is optional
// get input from file or website
protected function get_input(): string
{
$type = $this->test ? 'test' : 'puzzle';
$filepath = __DIR__ . '/' . $this->year . '/' . $type . '_inputs';
$filename = $filepath . '/day_' . sprintf('%02d', $this->day) . '.txt';
// don't re-fetch data if it's already saved
if (file_exists($filename)) {
return file_get_contents($filename);
}
// if file doesn't exist, get data from website
$input = $this->test ? $this->get_test_input() : $this->get_puzzle_input();
// make sure directory for file exists
if (!is_dir($filepath)) {
mkdir($filepath, 0777, true);
}
// write data to file to avoid unnecessary re-requests
file_put_contents($filename, $input);
return $input;
}
protected function get_puzzle_input(): string
{
// session required for puzzle inputs
$session = getenv('AOC_SESSION');
if (!$session) {
echo 'AOC_SESSION environment variable not found.';
die;
}
$url = 'https://adventofcode.com/' . $this->year . '/day/' . $this->day . '/input';
return $this->aoc_request($url, $session);
}
protected function get_test_input(): string
{
// get html of puzzle page
$html_string = $this->aoc_request('https://adventofcode.com/' . $this->year . '/day/' . $this->day);
$dom = new DOMDocument();
// suppress warnings about invalid tags
@$dom->loadHTML($html_string);
// assume the example input is the longest <code> block on the page
$code_nodes = $dom->getElementsByTagName('code');
$code_contents = [];
foreach ($code_nodes as $node) {
$code_contents[] = $node->textContent;
}
usort($code_contents, function ($a, $b) {
return strlen($b)-strlen($a);
});
// text content from longest <code>
return $code_contents[0];
}
// curl request to adventofcode.com
// configured to follow AoC automation guidelines
protected function aoc_request(string $url, ?string $session = null): string
{
$timestamp_filename = __DIR__ . '/timestamp.txt';
$last_request_time = 0;
$now = time();
if (file_exists($timestamp_filename)) {
$last_request_time = (int) file_get_contents($timestamp_filename);
}
$elapsed = $now - $last_request_time;
// Wait at least 5 minutes between requests. This
// is required by AoC guidelines. Do not remove
// timestamp.txt to bypass the throttle.
if ($elapsed < 300) {
echo 'Could not complete request to ' . $url;
echo PHP_EOL;
echo 'Please wait ' . (300 - $elapsed) . ' seconds before sending another request to adventofcode.com';
die;
}
file_put_contents($timestamp_filename, $now);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 80);
// required by AoC guidelines
curl_setopt($ch, CURLOPT_USERAGENT, 'github.com/kcamach1/advent-of-code by contact@kcamach1.dev');
if ($session) {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Cookie: session=' . $session
]);
}
$response = curl_exec($ch);
if (curl_error($ch)) {
echo 'Request Error:' . curl_error($ch);
curl_close($ch);
die;
}
curl_close($ch);
return $response;
}
// need getter because day is protected
// to prevent it from being changed
public function get_day(): int
{
return $this->day;
}
// need getter because year is protected
// to prevent it from being changed
public function get_year(): int
{
return $this->year;
}
// runs solve_part_one() and, if it exists, solve_part_two()
public function solve(): void
{
echo $this->title;
if ($this->test) {
echo ' (test)';
}
echo PHP_EOL;
$this->setup();
echo 'Part 1: ' . $this->solve_part_one();
if (method_exists($this, 'solve_part_two')) {
echo PHP_EOL;
echo 'Part 2: ' . $this->solve_part_two();
}
}
}