Skip to content

Instantly share code, notes, and snippets.

@kp666
Created September 22, 2017 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kp666/2876ea5cb3db88170a7bf3e52e623c5b to your computer and use it in GitHub Desktop.
Save kp666/2876ea5cb3db88170a7bf3e52e623c5b to your computer and use it in GitHub Desktop.
def process(file)
text = File.read(file)
string = text.lines.first
string.split(', ')
.map {|c| c.split("=")}
.inject({}) {|c, v| c[v[0]]=v[1].to_s.split(" ").map(&:to_i); c}
.inject([]) {|k, v| v[1].each {|i| k[i] = v[0]}; k}
.join
end
Dir["files/*.txt"].each do |file|
puts process(file)
end
@jikkujose
Copy link

# Consider for readability

def decode(file)
  File
    .open(file, "r") { |f| f.read }
    .split("\n")[0]
    .split(", ")
    .map { |line| line.split("= ") }
    .inject({}) { |hash, (char, string_of_numbers)| hash[char] = string_of_numbers.to_s.split(" ").map(&:to_i) ; hash }
    .inject([]) { |array, (char, indices)| indices.map { |i| array[i] = char } ; array }
    .join
end

p decode("./ccw5ccwf.txt")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment