forked from amtoine/advent-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolkit.nu
132 lines (116 loc) · 3.37 KB
/
toolkit.nu
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
# get the data of the day for an authenticated user
#
# # Examples
# ```nushell
# # if `asc.nuon` is a GPG-encrypted file containing with the following signature
# # ```nushell
# # record<cookie: string, mail: string>
# # ```
# aoc get-data 1 --year 2022 --login (
# gpg --quiet --decrypt aoc.nuon.asc | from nuon
# )
# ```
export def "aoc get-data" [
day: int, # the day of the event
--year: int, # the year to consider
--login: record<cookie: string, mail: string>, # the credentials to AoC
]: nothing -> string {
let url = $'https://adventofcode.com/($year)/day/($day)/input'
let header = [
Cookie $'session=($login.cookie)'
User-Agent $'email: ($login.mail)'
]
let res = http get --headers $header --full --allow-errors $url
if $res.status != 200 {
error make --unspanned {
msg: $res.body
}
}
$res.body
}
# get the number of stars for each day of a given year for an authenticated user
#
# # Examples
# ```nushell
# # if `asc.nuon` is a GPG-encrypted file containing with the following signature
# # ```nushell
# # record<cookie: string, mail: string>
# # ```
# aoc get-stars --year 2022 --login (
# gpg --quiet --decrypt aoc.nuon.asc | from nuon
# )
# ```
export def "aoc get-stars" [
--year: int, # the year to consider
--login: record<cookie: string, mail: string>, # the credentials to AoC
]: nothing -> table<day: int, stars: int> {
let url = $"https://adventofcode.com/($year)"
let header = [
Cookie $'session=($login.cookie)'
User-Agent $'email: ($login.mail)'
]
let res = http get --headers $header --full --allow-errors $url
if $res.status != 200 {
error make --unspanned {
msg: $res.body
}
}
$res.body
| lines
| parse --regex '.*\<a aria-label="Day (?<day>\d+)(?<stars>.*)" href=.*'
| into int day
| sort-by day
| update stars {
if ($in | str contains "one") {
1
} else if ($in | str contains "two") {
2
} else {
0
}
}
}
# jump to a solution
export def --env jump [] {
let res = ls solutions/*/*/* | get name | find --invert "/_data/" | input list --fuzzy
if $res == null {
return
}
cd $res
}
export def "init ocaml" [year: int, day: int]: nothing -> nothing {
let day = $"day_($day)"
let target = ("solutions" | path join ($year | into string) "ocaml" $day)
mkdir $target
cp ("templates" | path join "ocaml" "*") $target --recursive
# NOTE: this should work in `do { ... }`
ls ($target | path join "**/*")
| find day_x
| get name
| ansi strip
| wrap old
| insert new {|it| $it.old | str replace day_x $day}
| each { mv $in.old $in.new }
do {
cd $target
^sd Day_x ($day | str capitalize) **/*
^sd day_x $day **/*
^sd yyyy $year **/*
}
mkdir ("data" | path join ($year | into string) $day)
}
export def "run ocaml" [code: closure, --year: int]: nothing -> nothing {
let days = "solutions"
| path join ($year | into string) "ocaml" "*"
| into glob
| ls $in
| get name
for day in $days {
do {
cd $day
print $day
overlay use ocaml.nu
try { do $code }
}
}
}