solve day 12

This commit is contained in:
Huanzo 2021-12-12 07:06:12 +01:00
parent 21ebe11abb
commit 1e43c1d1b2
2 changed files with 65 additions and 0 deletions

22
data/day_12.txt Normal file
View File

@ -0,0 +1,22 @@
ax-end
xq-GF
end-xq
im-wg
ax-ie
start-ws
ie-ws
CV-start
ng-wg
ng-ie
GF-ng
ng-av
CV-end
ie-GF
CV-ie
im-xq
start-GF
GF-ws
wg-LY
CV-ws
im-CV
CV-wg

43
src/day_12.jl Normal file
View File

@ -0,0 +1,43 @@
include(joinpath(readchomp(`git rev-parse --show-toplevel`), "aoc.jl"))
import .Aoc: @aoc
using Pipe: @pipe
function visit(map, curr_cave, visited)
curr_cave == "end" ? (return 1) : nothing
curr_cave[begin] in 'a':'z' && curr_cave visited ? (return 0) : nothing
v = copy(visited)
push!(v, curr_cave)
return sum([ visit(map, e, v) for e in map[curr_cave] ])
end
function visit2(map, curr_cave, visited, d)
curr_cave == "end" ? (return 1) : nothing
curr_cave == "start" && length(visited) != 0 ? (return 0) : nothing
if curr_cave[begin] in 'a':'z' && curr_cave visited
d == nothing ? (d = curr_cave) : (return 0)
end
v = copy(visited)
push!(v, curr_cave)
return sum([ visit2(map, e, v, d) for e in map[curr_cave] ])
end
function prepare_input(input)
map = Dict()
for l in input
a, b = split(l, "-")
!haskey(map, a) ? (map[a] = [b]) : (push!(map[a], b))
!haskey(map, b) ? (map[b] = [a]) : (push!(map[b], a))
end
return map
end
function p1(input::Vector{String})
return visit(prepare_input(input), "start", [])
end
function p2(input::Vector{String})
return visit2(prepare_input(input), "start", [], nothing)
end
@aoc(2021, 12)