Compare commits

...

2 Commits

Author SHA1 Message Date
0dad7a3244 Adjust template. 2020-12-04 23:24:48 -08:00
b1889cbbba Add heap. 2020-12-04 23:24:41 -08:00
2 changed files with 86 additions and 0 deletions

83
heap.cr Normal file
View File

@ -0,0 +1,83 @@
class Array(T)
def bubble_up(i, &cmp)
return if i >= size
while i != 0
j = (i-1)//2
break if yield self[i], self[j]
self[i], self[j] = self[j], self[i]
i = j
end
end
def percalate_down(i, &cmp)
while i*2+1 < size
j1, j2 = i*2+1, i*2+2
v1 = self[j1]
v2 = self[j2]?
if v2 && (yield v1, v2) && (yield self[i], v2)
self[j2], self[i] = self[i], v2
i = j2
elsif yield self[i], v1
self[j1], self[i] = self[i], v1
i = j1
else
break
end
end
end
def heapify(&cmp : T,T -> Bool)
size.times do |i|
i = size - i - 1
bubble_up(i, &cmp)
end
self
end
def heapify
heapify do |i,j|
i < j
end
end
def heap_push(v, &cmp : T,T -> Bool)
self << v
bubble_up(size - 1, &cmp)
end
def heap_push(v)
heap_push(v) do |i,j|
i < j
end
end
def heap_pop(&cmp : T,T -> Bool)
self[0], self[size-1] = self[size-1], self[0]
v = pop
percalate_down(0, &cmp)
v
end
def heap_pop
heap_pop do |i, j|
i < j
end
end
def is_heap?(&cmp : T,T -> Bool)
(size-1).times do |i|
i = size - i - 1
vi = self[i]
vp = self[(i-1)//2]
return false unless (yield self[i], self[(i-1)//2]) || vi == vp
end
return true
end
def is_heap?
is_heap? do |i,j|
i < j
end
end
end

View File

@ -7,3 +7,6 @@ end
def part2
input = INPUT.clone
end
part1
part2