aoc2021/12.jl

75 lines
2.1 KiB
Julia

include("utils.jl")
function solution12_1()
lines = non_empty_lines("12.data")
adj_mat = Dict()
for line in lines
front, back = split(line, "-")
push!(get!(adj_mat, front, []), back)
push!(get!(adj_mat, back, []), front)
end
result = []
growing = [["start"]]
while length(growing) > 0
new = []
for path in growing
for neighbour in get(adj_mat, last(path), 0)
if Base.Unicode.isuppercase(neighbour[1])
push!(new, [path; neighbour])
else
if neighbour == "end"
push!(result, [path; neighbour])
else
if !(neighbour in path)
push!(new, [path; neighbour])
end
end
end
end
end
growing = new
end
length(result)
end
function small_twice(path)
length(filter(unique(path)) do element
Base.Unicode.islowercase(element[1]) && length(filter(==(element), path)) > 1
end) > 0
end
function solution12_2()
lines = non_empty_lines("12.data")
adj_mat = Dict()
for line in lines
front, back = split(line, "-")
push!(get!(adj_mat, front, []), back)
push!(get!(adj_mat, back, []), front)
end
result = []
growing = [["start"]]
while length(growing) > 0
new = []
for path in growing
for neighbour in get(adj_mat, last(path), 0)
if Base.Unicode.isuppercase(neighbour[1])
push!(new, [path; neighbour])
else
if neighbour == "end"
push!(result, [path; neighbour])
else
if neighbour != "start" && (!small_twice(path) || !(neighbour in path))
push!(new, [path; neighbour])
end
end
end
end
end
growing = new
end
length(result)
end
solution12_2()