Skip to content

Instantly share code, notes, and snippets.

@cmatheson
Created February 2, 2014 05:42
Show Gist options
  • Save cmatheson/8763560 to your computer and use it in GitHub Desktop.
Save cmatheson/8763560 to your computer and use it in GitHub Desktop.
stripectf3 level 3 solution
#!/usr/bin/env ruby
exit if ARGV.shift != "--master"
ARGV.shift until ARGV.empty?
require 'sinatra'
require 'json'
require 'thread'
require 'set'
set :port, 9090
Thread.abort_on_exception = true
$indexed = false
get '/healthcheck' do
content_type "application/json"
{success: true}.to_json
end
get '/isIndexed' do
content_type "application/json"
{success: $indexed}.to_json
end
get '/' do
content_type "application/json"
{
success: true,
results: lookup(params[:q]),
}.to_json
end
get '/index' do
Thread.new {
index(params[:path])
}
content_type "application/json"
"OK"
end
def index(dir)
Dir.chdir(dir)
$word_locations = {}
$word_str = ","
files = Dir["**/*"].select { |f| File.stat(f).file? }
files.each do |f|
File.open(f).each_line.with_index { |l,i|
l.scan(/\w+/).each { |w|
unless $word_locations[w]
$word_str << w << ","
$word_locations[w] = []
end
$word_locations[w] << f.to_sym << i + 1
}
}
end
$indexed = true
end
def matches(q)
offset = 0
words = []
while offset = $word_str.index(q, offset)
r = $word_str.index(",", offset) - 1
l = $word_str.rindex(",", offset) + 1
words << $word_str[l..r]
offset += 1
end
words
end
def lookup(q)
matches(q).flat_map { |w|
$word_locations[w].each_slice(2).map { |f, i|
"#{f}:#{i}"
}
}.uniq
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment