From a23330f007598cc84c916a161b75ed8fa83e105f Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Mon, 18 Dec 2017 12:33:31 +0100 Subject: [PATCH] 2nd part --- knot_tying.rb | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 knot_tying.rb diff --git a/knot_tying.rb b/knot_tying.rb new file mode 100644 index 0000000..56f47f1 --- /dev/null +++ b/knot_tying.rb @@ -0,0 +1,97 @@ +INPUT = [97,167,54,178,2,11,209,174,119,248,254,0,255,1,64,190] +INPUT2 = "97,167,54,178,2,11,209,174,119,248,254,0,255,1,64,190" + +def do_knot text + knot_lol = (0..255).to_a + skips = 0 + curr_pos = 0 + text.each do |i| + temp = [] + + (0...i).each do |j| + temp << (knot_lol[(curr_pos+j) % 256]) + end + (0...i).each do |j| + knot_lol[(curr_pos+i-1-j) % 256] = temp[j] + end + curr_pos += skips + i + skips += 1 + end + knot_lol +end + + + + +def do_knot2 text + knot_lol = (0..255).to_a + skips = 0 + curr_pos = 0 + (0...64).each do + text.each do |i| + temp = [] + + (0...i).each do |j| + temp << (knot_lol[(curr_pos+j) % 256]) + end + (0...i).each do |j| + knot_lol[(curr_pos+i-1-j) % 256] = temp[j] + end + curr_pos += skips + i + skips += 1 + end + end + knot_lol +end + +def dense_hash input + blocks = input.each_slice(16).to_a + final_hash = [] + blocks.each do |block| + hash = block[0] + block[1..block.size-1].each do |s| + hash = hash ^ s + end + final_hash << hash + end + final_hash +end + +def knot_hash input + knot_hashed = [] + input.each do |s| + a = s.to_s(16) + if a.size == 1 then a = "0" + a end + knot_hashed << a + end + knot_hashed.join "" +end + + +# input.each do |size| +# # p skips+curr_val + +# if size > 0 then +# knot_lol = knot_lol.each_slice(size).to_a +# # p knot_lol +# knot_lol[0].reverse! +# # p knot_lol +# knot_lol.flatten! +# end +# knot_lol = knot_lol.rotate(skips+curr_val) + +# head += (size + [skips, 0].max) +# head = head % 5 +# # p "head " + knot_lol[head].to_s + + +# skips += 1 +# curr_val = size +# # p knot_lol +# end + +out = do_knot INPUT +p (out[0] * out[1]).to_s + +input_2 = INPUT2.split("").map(&:ord) + [17, 31, 73, 47, 23] +p knot_hash dense_hash do_knot2 input_2