AdventOfCode/knot_tying.rb
2017-12-18 12:33:31 +01:00

98 lines
1.8 KiB
Ruby

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