Part 1


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]:
knot_hash (generic function with 3 methods)

In [2]:
@time encode_hash(knot_hash([3 4 1 5], 5))


  0.389266 seconds (122.28 k allocations: 6.276 MiB, 12.17% gc time)
Out[2]:
12

In [3]:
@time encode_hash(knot_hash(readdlm("inputs/day10.txt", ',', Int16)))


  0.556250 seconds (326.87 k allocations: 17.492 MiB, 0.94% gc time)
Out[3]:
29240

Part 2


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]:
encode_hexa (generic function with 1 method)

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]:
"a2582a3a0e66e6e86e3812dcb672a272"

In [7]:
encode_hexa(knot_hash(encode_str("1,2,3"), 256, 64))


Out[7]:
"3efbe78a8d82f29979031a4aa0b16a9d"

In [8]:
encode_hexa(knot_hash(encode_str("1,2,3"), 256, 64))


Out[8]:
"3efbe78a8d82f29979031a4aa0b16a9d"

In [9]:
open("inputs/day10.txt") do fd
    encode_hexa(knot_hash(encode_str(strip(readstring(fd))), 256, 64))
end


Out[9]:
"4db3799145278dc9f73dcdbc680bd53d"

In [ ]: