-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathaoc.ijs
126 lines (107 loc) · 2.89 KB
/
aoc.ijs
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
load 'web/gethttp stats/bonsai'
coclass 'aoc'
AOCDIR=: jpath '~/code/aoc'
cookie_file=: 3 : 0
AOCDIR,'/cookie'
)
save_cookie=: 1!:2 <@cookie_file
read_cookie=: 1!:1@<@cookie_file
input_file=: 3 : 0
'y d'=. ": &.> y
AOCDIR,'/input/',y,'/',d,'.in'
)
input_url=: 3 : 0
'y d'=. ": &.> y
'https://adventofcode.com/',y,'/day/',d,'/input'
)
input_exists=: fexist @ input_file
input_req=: 3 : 0
out=. input_file y
cki=.'Cookie: session=',read_cookie y
hdr=.'-H "',cki,'"'
hdr ; input_url y
)
'Y0 Y1' =: 2015;2025
get_input =: 3 : 0
'y d' =. y
'cy cm cd' =. 3 {. 6!:0 ''
assert. *./ (1<:d),(d<:25),(Y0<:y),(y<:Y1)
assert. (y<Y1)+.(cy=y)*.(d<:cd)*.(cm=12)
file=. input_file y,d
try. assert. fexist file
1!:1 < file
catch. 'hdr url'=. input_req y,d
input=. 2!:0 'curl ', (>hdr) , ' ' , >url
if. +./'Puzzle inputs differ by user'E.input do. echo input
else. input 1!:2 < file end.
input
end.
)
3 : 0 '' NB. initialize
for_j. Y0+i.1+Y1-Y0 do.
assert. fpathcreate AOCDIR,'/input/',(":j),'/'
end.
)
aoc_z_ =: get_input_aoc_
update_cookie_z_ =: 3 : 0
1!:55 < cookie_file_aoc_ ''
y 1!:2 < cookie_file_aoc_ ''
)
bfs =: 4 : 0
NB. get tree from bfs starting at x in graph y
Q =. ~.,x NB. seed queue from x and mark x explored
S =. -. (T =. i.#y) e. x NB. explored v iff 0 = v{S
while. #Q do. 'u Q' =. ({.;}.) Q NB. pop Q
vs =. I. S * u{y NB. unexplored out edges u -> v
Q =. vs ,~ Q NB. push vs
S =. 0 vs} S NB. mark vs explored
T =. u vs} T NB. indicate parent in tree T
end. T
)
egcd =: 4 : 0 NB. extended euclid
'a b c d s t' =. 1 0 0 1,x,y
while. r =. t - s*q =. <. t%s do.
c =. t [ a =. c-q*a [ t =. a
d =. t [ b =. d-q*b [ t =. b
s =. r [ t =. s
end. a,b,s assert. s = (a*x) + b*y
)
crt =: 4 : 0 NB. chinese remainder theorem
's t g'=.m egcd n['b n'=.y['a m'=.x
<.(<.m*n%g)(|,[)(a*t*<.n%g)+(b*m*<.s%g) assert. 0 = g | a-b
)
P =: 3 : 0 NB. converting depth vector to parent vector
ps=. 0 #~ n =. # y
for_lk. 2 ]\ (i.n) </.~ y do.
ps=. ps k }~ l {~ <: l I. k [ 'l k' =. lk
end. ps + (i.n) * 0=y
)
NB. https://en.wikipedia.org/wiki/Cycle_detection#Brent's_algorithm
brent =: 1 : 0 NB. brent cycle detection algorithm. outputs period and iterations before cycle
power =. lambda =. 1
tortoise =. y
hare =. u y
while. tortoise ~:&< hare do. NB. boxing to get deep equality seems to work...
if. power = lambda do. lambda =. 0 [ power =. 2*power [ tortoise =. hare end.
lambda =. 1+lambda [ hare =. u hare
end.
tortoise =. y
hare =. u^:lambda y
mu =. 0
while. tortoise ~:&< hare do.
mu =. 1+mu [ hare =. u hare [ tortoise =. u tortoise
end.
lambda,mu NB. lambda is period, mu is length until function cycles
)
NB. some typical charsets
az =: a.{~97+i.26
AZ =: a.{~65+i.26
a09 =: a.{~48+i.10
bfs_z_ =: bfs_aoc_
bez_z_ =: bez_aoc_
crt_z_ =: crt_aoc_
P_z_ =: P_aoc_
brent_z_ =: brent_aoc_
az_z_ =: az_aoc_
AZ_z_ =: AZ_aoc_
a09_z_ =: a09_aoc_