-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle.sh
executable file
·252 lines (205 loc) · 5.5 KB
/
puzzle.sh
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env bash
set -euo pipefail
rootdir=$(realpath $(dirname $0)/..)
declare -a features=
day=
year=
available=
usage()
{
echo "Usage: $0 [day]"
exit
}
parse_args()
{
declare -a args=
for i ; do
case $i in
-h|--help) usage ;;
-r|--rs|--rust) features+=("rust") ;;
-p|--py|--python) features+=("python") ;;
-t|--test) features+=("test_samples") ;;
*) args+=$i ;;
esac
done
set -- "${args[@]}"
if [[ $(basename $PWD) =~ day* ]]; then
day=$(basename $PWD)
day=${day##day}
year=$(basename $(realpath $PWD/..))
year=${year##year}
else
if [ $# -eq 0 ]; then
usage
fi
year=$(basename $PWD)
day=$1
mkdir -p day$day
cd day$day
fi
}
has_feature()
{
[[ -z "${features[@]}" ]] || [[ "${features[@]}" =~ "$1" ]]
}
fetch_input()
{
local session
local now
local opening
local waiting
session=$(awk '/^[^#].*/{ if (! session) session=$1 } END{print session}' < $rootdir/.session)
now=$(date -u +%Y%m%d%H%M%S)
local ts=$(printf "%04d%02d%02d%02d%02d%02d" $year 12 $day 5 0 0)
if [[ $now == $ts ]] || [[ $now > $ts ]] ; then
curl "https://adventofcode.com/$year/day/$day/input" \
-H "Cookie: session=$session" -o input.txt
head input.txt
wc -l input.txt
available=1
else
opening=$(date -v${year}y -v12m -v${day}d -v5H -v0M -v0S -u +%s)
now=$(date -u +%s)
waiting=$(($opening-$now))
printf "\033[5;93m"
printf "Puzzle unavailable: please wait "
if [[ $(($waiting / 3600)) != 0 ]]; then printf "$(($waiting / 3600)) hours, "; fi
printf "$(( $(($waiting / 60)) % 60)) minutes and $(($waiting % 60)) seconds."
printf "\033[0m\n"
available=
fi
}
fetch_samples()
{
local session
[[ $available ]] || return 0
session=$(awk '/^[^#].*/{ if (! session) session=$1 } END{print session}' < $rootdir/.session)
curl -s "https://adventofcode.com/$year/day/$day" \
-H "Cookie: session=$session" | python3 -EB -c '
import re, sys, pathlib
for i, m in enumerate(re.finditer(r"<pre><code>(.*?)</code></pre>", sys.stdin.read(), re.DOTALL), 1):
sample = m[1]
sample = re.sub(r"<em>(.*?)</em>", r"\1", sample)
sample = sample.replace(">", ">")
sample = sample.replace("<", "<")
print(f"\033[32mextracting sample {i} ({len(sample)} bytes)\033[0m")
pathlib.Path(f"sample_{i}.txt").write_text(sample)
'
}
create_python()
{
has_feature python || return 0
if [ -f day$day.py ]; then
printf "\033[31mPython script already exists.\033[0m\n"
return
fi
local title=$($rootdir/scripts/answers.py --get-title --year $year --day $day)
cat <<EOF >day$day.py
#!/usr/bin/env python3
# $title
from pathlib import Path
from argparse import ArgumentParser
from copy import deepcopy
from collections import defaultdict, deque, namedtuple, Counter
from functools import reduce
from operator import mul
import sys, re, math, itertools, time, atexit, re
parser = ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-t", "--test", action="store_true")
parser.add_argument("--elapsed", action="store_true")
parser.add_argument("filename", nargs="?", type=Path, default="input.txt")
args = parser.parse_args()
if args.test:
args.filename = Path("test.txt")
data = args.filename.read_text()
if args.elapsed:
start_time_ns = time.time_ns()
atexit.register(lambda: print(f"elapsed: {(time.time_ns() - start_time_ns) / 1_000_000}ms"))
EOF
chmod a+x day$day.py
printf "\033[32mPython script template created.\033[0m\n"
}
create_rust()
{
has_feature rust || return 0
if [ -f day$day.rs ]; then
printf "\033[31mRust program already exists.\033[0m\n"
return
fi
local title=$($rootdir/scripts/answers.py --get-title --year $year --day $day)
cat <<EOF >day$day.rs
//! $title
struct Puzzle {
//
}
impl Puzzle {
/// Initialize from the puzzle input.
const fn new(data: &str) -> Self {
Self { }
}
/// Solve part one.
fn part1(&self) -> i32 {
0
}
/// Solve part two.
fn part2(&self) -> i32 {
0
}
}
/// # Panics
#[must_use]
pub fn solve(data: &str) -> (i32, i32) {
let puzzle = Puzzle::new(data);
(puzzle.part1(), puzzle.part2())
}
pub fn main() {
let args = aoc::parse_args();
args.run(solve);
}
#[cfg(test)]
mod test {
use super::*;
// const TEST_INPUT: &str = include_str!("test.txt");
EOF
for sample in sample_*.txt ; do
local sample_name=$(echo ${sample%.txt} | tr "[:lower:]" "[:upper:]")
cat <<EOF >>day$day.rs
// const $sample_name: &str = include_str!("$sample");
EOF
done
cat <<EOF >>day$day.rs
// #[test]
// fn part1() {
// let puzzle = Puzzle::new(SAMPLE_1);
// assert_eq!(puzzle.part1(), 0);
// }
// #[test]
// fn part2() {
// let puzzle = Puzzle::new(TEST_INPUT);
// assert_eq!(puzzle.part2(), 0);
// }
}
EOF
printf "\033[32mRust program template created.\033[0m\n"
if [ ! -f Cargo.toml ]; then
cat <<EOF >Cargo.toml
[package]
name = "day$day"
version = "0.1.0"
edition = "2021"
[dependencies]
aoc = { path = "../../../crates/aoc" }
rustc-hash = "*"
itertools = "*"
[[bin]]
name = "day$day"
path = "day$day.rs"
EOF
fi
}
parse_args "$@"
fetch_input
fetch_samples
create_python
create_rust