-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.jl
157 lines (135 loc) · 3.76 KB
/
lib.jl
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
# utils to be included in daily solutions:
# include("../lib.jl")
parseints(s:: AbstractString) = parse.(Int, split(s))
parseints(strs:: AbstractVector{<:AbstractString}) = parse.(Int, strs)
parseints(ints:: AbstractVector{<:Integer}) = Int.(ints)
function makegrid(lines:: AbstractVector{<:AbstractString}; bg:: Char = '.', sentinels:: Bool = false)
n = length(lines)
m = maximum(length.(lines))
if sentinels
grid = fill(bg, (n+2, m+2))
for i in 1:n
k = length(lines[i])
for j in 1:k
grid[i+1, j+1] = lines[i][j]
end
end
return grid
else
grid = fill(bg, (n, m))
for i in 1:n
k = length(lines[i])
for j in 1:k
grid[i, j] = lines[i][j]
end
end
return grid
end
end
function printgrid(io:: IO, grid:: AbstractMatrix{<:AbstractChar})
n, m = size(grid)
for i in 1:n
for j in 1:m
print(io, grid[i,j])
end
println(io)
end
end
printgrid(grid:: AbstractMatrix{<:AbstractChar}) = printgrid(stdout, grid)
function nbors(i, j; diag:: Bool)
if diag
(
(i-1, j-1),
(i-1, j ),
(i-1, j+1),
(i, j+1),
(i+1, j+1),
(i+1, j ),
(i+1, j-1),
(i, j-1),
)
else
(
(i-1, j),
(i, j+1),
(i+1, j),
(i, j-1),
)
end
end
function manhattan(ax, ay, bx, by; diag:: Bool = false)
if diag
dx = abs(ax-bx)
dy = abs(ay-by)
md = min(dx, dy)
dx + dy - md
else
abs(ax-bx) + abs(ay-by)
end
end
manhattan(ax, ay; diag:: Bool = false) = manhattan(ax, ay, 0, 0, diag=diag)
manhattan(a:: Tuple{<:Any, <:Any}, b:: Tuple{<:Any, <:Any}; diag:: Bool = false) = manhattan(a..., b..., diag=diag)
struct Windows
vector:: Union{AbstractVector, AbstractString}
winlen:: Integer
end
# Returns an iterable going through all subarrays (or substrings) of length winlen.
windows(vector:: Union{AbstractVector, AbstractString}, winlen:: Integer) = Windows(vector, winlen)
function Base.iterate(w:: Windows)
if w.winlen <= length(w.vector)
(view(w.vector, 1:w.winlen), 2)
else
nothing
end
end
function Base.iterate(w:: Windows, idx:: Integer)
if idx + w.winlen - 1 <= length(w.vector)
(view(w.vector, idx:idx + w.winlen - 1), idx + 1)
else
nothing
end
end
Base.length(w:: Windows) = max(0, length(w.vector) - w.winlen)
struct Groups
vector:: Union{AbstractVector, AbstractString}
grplen:: Integer
end
# Returns an iterable going through subarrays (or substrings) of length grplen, one after another, non-overlapping.
groups(vector:: Union{AbstractVector, AbstractString}, grplen:: Integer) = Groups(vector, grplen)
function Base.iterate(g:: Groups)
if g.grplen <= length(g.vector)
(view(g.vector, 1:g.grplen), g.grplen + 1)
else
nothing
end
end
function Base.iterate(g:: Groups, idx:: Integer)
if idx + g.grplen - 1 <= length(g.vector)
(view(g.vector, idx:idx + g.grplen - 1), idx + g.grplen)
else
nothing
end
end
Base.length(g:: Groups) = div(length(g.vector), g.grplen)
struct Vsplit
vector:: AbstractVector
pred:: Function
end
# Returns an iterable going through subarrays, splitting the vector by elements for which the predicate returns true.
# To filter out empty subarrays, set the keyword argument keepempty to false.
function vsplit(vector:: AbstractVector, pred:: Function; keepempty:: Bool = true)
vs = Vsplit(vector, pred)
keepempty ? vs : Iterators.filter(!isempty, vs)
end
function vsplit(vector:: AbstractVector, splitval; keepempty:: Bool = true)
vsplit(vector, x -> x == splitval, keepempty=keepempty)
end
function Base.iterate(vs:: Vsplit)
nextidx = findfirst(vs.pred, vs.vector)
isnothing(nextidx) ? (vs.vector, -1) : (view(vs.vector, 1:nextidx - 1), nextidx + 1)
end
function Base.iterate(vs:: Vsplit, idx:: Integer)
idx < 0 && return nothing
nextidx = findnext(vs.pred, vs.vector, idx)
isnothing(nextidx) ? (view(vs.vector, idx:length(vs.vector)), -1) : (view(vs.vector, idx:nextidx - 1), nextidx + 1)
end