From 1ab2e30614a20479a8e1dc41a5bd44de8732c03b Mon Sep 17 00:00:00 2001 From: Alexander Seiler Date: Sat, 30 Dec 2023 02:51:51 +0100 Subject: [PATCH] [Day 25] New approach --- README.md | 2 +- src/day25.jl | 82 ++++++++++------------------------------------------ 2 files changed, 17 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index 3c4f012..722f921 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/day25.jl b/src/day25.jl index d9dc504..4d486b1 100644 --- a/src/day25.jl +++ b/src/day25.jl @@ -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