Skip to content

Commit

Permalink
README rebuilt using Weave
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisvalentiner committed Dec 15, 2017
1 parent 87c56b2 commit a5840b5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
58 changes: 58 additions & 0 deletions src/puzzles/puzzle14.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

function hex2binary(x::String)
reduce(*, bin.(parse.(Int, collect(x)), 4))
end

function adjacentcoords(c::Tuple)
x, y = c
return [[(max(_x+x, 1), y) for _x=-1:2:1]; [(x, max(_x+y, 1)) for _x=-1:2:1]]
end

function puzzle14(input::String="jzgqcdpd")
input = "jzgqcdpd"
m = Matrix(128, 128);
for p in 1:128
bytes = string2bytes("$input-$(p-1)")
append!(bytes, [17, 31, 73, 47, 23])
list = knothash(bytes, iterations=64)
sparsehash = reshape(list, 16, 16)
densehash = [reduce(xor, sparsehash[:, i]) for i in 1:size(sparsehash, 2)]
hashstring = reduce(*, hex.(densehash, 2))
binarystring = hex2binary(hashstring)
m[p, :] = parse.(split(binarystring, ""))
end
partone = sum(m)

ref = reshape(1:16384, 128, 128)'
graph = Dict()
for i in 1:16384
if m[i] == 1
# find coordinates
x, y, val = findnz(ref .== i)
nb_coords = [[(_x+x, y) for _x=-1:2:1]; [(x, _x+y) for _x=-1:2:1]]
nb_coords = filter(x -> (0 < x[1][1] < 129) & (0 < x[2][1] < 129), nb_coords)
neighbors = [ref[x[1], x[2]] for x in collect.(flatten.(nb_coords))]
# if neighbor is also 1, add to edge list
graph[i] = [nb for nb in neighbors if m[nb]==1]
end
end

nodes = [first(keys(graph))]
connected = nodes
groups = Dict()

while length(graph) > 0
while true
nodes = setdiff(collect(flatten(get.(graph, nodes, nothing))), connected)
length(nodes) == 0 ? break : append!(connected, nodes)
end
groups[first(connected)] = connected
delete!.(graph, connected)
length(graph) == 0 ? break : nothing
nodes = [first(keys(graph))]
connected = nodes
end
parttwo = length(groups)

return [partone, parttwo]
end
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ end
@test puzzle07() == ["cyrupz", nothing]
@test puzzle08() == [6343, 7184]
@test puzzle09() == [8337, 4330]
@test puzzle10() == [826, nothing]
@test puzzle10() == [826, "d067d3f14d07e09c2e7308c3926605c4"]
@test puzzle11() == [707, 1490]
@test puzzle12() == [115, 221]
@test puzzle13() == [1580, 3943252]
@test puzzle14() == [8074, 1212]

0 comments on commit a5840b5

Please sign in to comment.