-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.jl
62 lines (56 loc) · 1.71 KB
/
day25.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Day25
using AdventOfCode2023
using DataStructures
using Random
function day25(input::String = readInput(joinpath(@__DIR__, "..", "data", "day25.txt")))
graph, total_size = parse_input(input)
solve(graph, total_size)
end
function parse_input(input::AbstractString)
i = 1
translations = Dict{String,Int}()
graph = Dict{Int, Vector{Int}}()
for line ∈ eachsplit(rstrip(input), "\n")
nodes = split.(replace(line, ":" => ""), " ")
n = Int[]
for node ∈ nodes
if haskey(translations, node)
push!(n, translations[node])
else
translations[node] = i
graph[i] = Vector{Int}()
push!(n, i)
i += 1
end
end
node = nodes[begin]
for n ∈ nodes[begin+1:end]
push!(graph[translations[node]], translations[n])
push!(graph[translations[n]], translations[node])
end
end
return graph, i - 1
end
function solve(graph::Dict{Int,Vector{Int}}, total_size::Int)
for k = shuffle(1:total_size)
not_connected = PriorityQueue{Int,Int}()
connected = Set{Int}()
for i ∈ 1:total_size
not_connected[i] = 0
end
not_connected[k] = -1000
while !isempty(not_connected)
if (values(not_connected) |> sum) == -3
return length(not_connected) * (total_size - length(not_connected))
end
v = dequeue!(not_connected)
push!(connected, v)
for n ∈ graph[v]
if haskey(not_connected, n)
not_connected[n] -= 1
end
end
end
end
end
end # module