Day A Dozen

trunk
HeNine 3 years ago
parent e26833939c
commit df299186e3

@ -0,0 +1,4 @@
name = "day12"
uuid = "8b45b4a1-93ce-4a08-b654-2545c47524be"
authors = ["HeNine "]
version = "0.1.0"

@ -0,0 +1,7 @@
start-A
start-b
A-c
A-b
b-d
A-end
b-end

@ -0,0 +1,10 @@
dc-end
HN-start
start-kj
dc-start
dc-HN
LN-dc
HN-end
kj-sa
kj-HN
kj-dc

@ -0,0 +1,18 @@
fs-end
he-DX
fs-he
start-DX
pj-DX
end-zg
zg-sl
zg-pj
pj-he
RW-he
fs-DX
pj-RW
zg-RW
start-pj
he-WI
zg-he
pj-fs
start-RW

@ -0,0 +1,21 @@
TR-start
xx-JT
xx-TR
hc-dd
ab-JT
hc-end
dd-JT
ab-dd
TR-ab
vh-xx
hc-JT
TR-vh
xx-start
hc-ME
vh-dd
JT-bm
end-ab
dd-xx
end-TR
hc-TR
start-vh

@ -0,0 +1,97 @@
include("../../AoC2021.jl")
##
function parsefile(file)
pairs = map(eachline(file)) do line
split(line, "-") |>
p -> (p[1], p[2])
end
nodes = (pairs .|> ((x, y),) -> [x, y]) |>
(x -> vcat(x...)) |>
(x -> filter(x -> x ["start", "end"], x)) |>
unique |>
x -> ["start", x..., "end"]
bigcaves = map(x -> all(isuppercase.(collect(x))), nodes)
graph = zeros(Int, length(nodes), length(nodes))
for (x, y) in pairs
graph[findfirst(nodes .== x), findfirst(nodes .== y)] = 1
graph[findfirst(nodes .== y), findfirst(nodes .== x)] = 1
end
(nodes, bigcaves, graph)
end
function findpaths(currentnode, nodes, bigcaves, graph, visited, path)
path = [path..., currentnode]
if currentnode == "end"
return [path]
end
currentnodeindex = nodes .== currentnode
if currentnodeindex' * bigcaves == 0
visited = [visited..., currentnode]
end
findall(graph * currentnodeindex .> 0) .|>
(x -> nodes[x]) |>
(x -> filter(x -> x visited, x)) .|>
(x -> findpaths(x, nodes, bigcaves, graph, visited, path)) |>
(x -> vcat(x...))
end
##
function task0(file)
(nodes, bigcaves, graph) = parsefile(file)
findpaths("start", nodes, bigcaves, graph, [], []) |> length
end
##
verify(12, 1, 0, 10)
verify(12, 1, 1, 19)
verify(12, 1, 2, 226)
verify(12, 1, 3, 5157)
##
function findpaths2(currentnode, nodes, bigcaves, graph, visited, path, visitedtwice = false)
path = [path..., currentnode]
if currentnode == "end"
return [path]
end
currentnodeindex = nodes .== currentnode
visitedtwice = currentnode visited || visitedtwice
if currentnodeindex' * bigcaves == 0
visited = [visited..., currentnode]
end
findall(graph * currentnodeindex .> 0) .|>
(x -> nodes[x]) |>
(x -> filter(x -> !visitedtwice && x != "start" || x visited, x)) .|>
(x -> findpaths2(x, nodes, bigcaves, graph, visited, path, visitedtwice)) |>
(x -> vcat(x...))
end
function task1(file)
(nodes, bigcaves, graph) = parsefile(file)
findpaths2("start", nodes, bigcaves, graph, [], []) |> length
end
##
verify(12, 2, 0, 36)
@profview verify(12, 2, 3, 144309)
Loading…
Cancel
Save