Skip to content

Commit

Permalink
[Day 25] New approach
Browse files Browse the repository at this point in the history
  • Loading branch information
goggle committed Dec 30, 2023
1 parent aef3894 commit 1ab2e30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 67 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ This Julia package contains my solutions for [Advent of Code 2023](https://adven
| 22 | [:white_check_mark:](https://adventofcode.com/2023/day/22) | 790.712 ms | 631.26 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day22.jl) |
| 23 | [:white_check_mark:](https://adventofcode.com/2023/day/23) | 2.979 s | 9.69 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day23.jl) |
| 24 | [:white_check_mark:](https://adventofcode.com/2023/day/24) | 41.181 ms | 49.71 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day24.jl) |
| 25 | [:white_check_mark:](https://adventofcode.com/2023/day/25) | 148.392 ms | 173.36 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day25.jl) |
| 25 | [:white_check_mark:](https://adventofcode.com/2023/day/25) | 153.698 ms | 176.55 MiB | [:white_check_mark:](https://github.com/goggle/AdventOfCode2023.jl/blob/master/src/day25.jl) |


The benchmarks have been measured on this machine:
Expand Down
82 changes: 16 additions & 66 deletions src/day25.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,76 +37,26 @@ function parse_input(input::AbstractString)
end

function solve(graph::Dict{Int,Set{Int}}, total_size::Int)
edge_count = Dict{Tuple{Int,Int},Int}()
for i = 1:total_size
for j = i + 1:total_size
edge_count[(i, j)] = 0
end
end
while true
for _ 1:10
source = rand(1:total_size)
goals = Random.randsubseq(1:total_size, 0.1)
isempty(goals) && continue
prev = dijkstra(graph, source)
for goal goals
goal == source && continue
u = goal
while haskey(prev, u)
edge_count[minmax(u, prev[u])] += 1
u = prev[u]
end
end
end
ignore_edges = [x[1] for x sort(collect(edge_count), by=last, rev=true)[begin:begin+2]]
start1, start2 = ignore_edges[1]
size1 = bfs_count_size(graph, start1, Set(ignore_edges))
size2 = bfs_count_size(graph, start2, Set(ignore_edges))
if size1 + size2 == total_size
return size1 * size2
end
end
end

function dijkstra(graph::Dict{Int,Set{Int}}, source::Int)
pq = PriorityQueue{Int,Int}()
dist = Dict{Int,Int}()
dist[source] = 0
prev = Dict{Int,Int}()
for i eachindex(graph)
if i != source
dist[i] = typemax(Int)
prev[i] = 0
end
pq[i] = dist[i]
end
while !isempty(pq)
u = dequeue!(pq)
for v graph[u]
alt = dist[u] + 1
if alt < dist[v]
dist[v] = alt
prev[v] = u
pq[v] = alt
for k = 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
end
end
return prev
end

function bfs_count_size(graph::Dict{Int,Set{Int}}, start::Int, ignore_edges::Set{Tuple{Int,Int}})
visited = Set{Int}([start])
queue = [n for n graph[start] if minmax(start, n) ignore_edges]
while !isempty(queue)
node = popfirst!(queue)
push!(visited, node)
for n graph[node]
if n visited && minmax(node, n) ignore_edges
push!(queue, n)
v = dequeue!(not_connected)
push!(connected, v)
for n graph[v]
if haskey(not_connected, n)
not_connected[n] -= 1
end
end
end
end
return length(visited)
end

end # module

0 comments on commit 1ab2e30

Please sign in to comment.