In [1]:
function encode_hash(elem)
elem[1]*elem[2]
end
function knot_hash(lens, size=256, rounds=1)
i = 0
skip = 0
elem = collect(0:(size)-1)
for r = 1:rounds
for l in lens
elem = circshift(reverse(circshift(elem, -i), 1, l), i)
i = (i + l + skip) % size
skip += 1
end
end
elem
end
Out[1]:
In [2]:
@time encode_hash(knot_hash([3 4 1 5], 5))
Out[2]:
In [3]:
@time encode_hash(knot_hash(readdlm("inputs/day10.txt", ',', Int16)))
Out[3]:
In [4]:
function encode_str(values)
vcat(map(Int, collect(values)), [17, 31, 73, 47, 23])
end
function encode_hexa(elem)
elem = reshape(elem, (16, 16))
elem = mapslices(c->reduce(xor,0,c), elem, 1)
join(map(x->hex(x,2), elem))
end
Out[4]:
In [5]:
# The empty string becomes a2582a3a0e66e6e86e3812dcb672a272.
# AoC 2017 becomes 33efeb34ea91902bb2f59c9920caa6cd.
# 1,2,3 becomes 3efbe78a8d82f29979031a4aa0b16a9d.
# 1,2,4 becomes 63960835bcdc130f0b66d7ff4f6a5a8e
In [6]:
encode_hexa(knot_hash(encode_str(""), 256, 64))
Out[6]:
In [7]:
encode_hexa(knot_hash(encode_str("1,2,3"), 256, 64))
Out[7]:
In [8]:
encode_hexa(knot_hash(encode_str("1,2,3"), 256, 64))
Out[8]:
In [9]:
open("inputs/day10.txt") do fd
encode_hexa(knot_hash(encode_str(strip(readstring(fd))), 256, 64))
end
Out[9]:
In [ ]: