Advent of Code 2021 day 15
12/15/2021, 5:00 AMDan Fingal-Surma
12/15/2021, 6:49 AMelizarov
12/15/2021, 6:51 AMDan Fingal-Surma
12/15/2021, 6:57 AMprintln(unvisited.size)
to the top of the loop to make sure it was going to make progress, but then it finished. Going back and doing this now I can see it making steady, if slow, progressDan Fingal-Surma
12/15/2021, 6:59 AMjava.util.PriorityQueue
but then decided nah, letās go with minByOrNull
elizarov
12/15/2021, 7:01 AMPriorityQueue
is of no help ā you cannot efficiently decrease the key of an arbitrary element in there.knthmn
12/15/2021, 7:02 AMDeque
(breath first), took 5 seconds for part 2. Switched to a PriorityQueue()
and only took 2 seconds. I am not CS trained so I don't know which algorithm I am using.
My solution: https://github.com/knthmn/AdventOfCode-Kotlin/blob/main/src/y21/day15.ktDan Fingal-Surma
12/15/2021, 7:03 AMDan Fingal-Surma
12/15/2021, 7:03 AMDan Fingal-Surma
12/15/2021, 7:03 AMDan Fingal-Surma
12/15/2021, 7:04 AMMichael de Kaste
12/15/2021, 7:05 AMelizarov
12/15/2021, 7:06 AMMichael de Kaste
12/15/2021, 7:09 AMDan Fingal-Surma
12/15/2021, 7:11 AMqueue.remove(it)
queue.add(it)
Dan Fingal-Surma
12/15/2021, 7:15 AMelizarov
12/15/2021, 7:17 AMDan Fingal-Surma
12/15/2021, 7:26 AMDan Fingal-Surma
12/15/2021, 7:28 AMDavid Whittaker
12/15/2021, 7:29 AMPriorityQueue
! I created a data class Chiton(val point: Pair<Int,Int>, val risk: Int)
then a val chitons = mutableListOf<Chiton>()
for each iteration adding possible steps, and then chitons.sortBy { it.risk }
.Michael de Kaste
12/15/2021, 7:30 AMelizarov
12/15/2021, 7:32 AMminBy
every time.Michael de Kaste
12/15/2021, 7:36 AMDan Fingal-Surma
12/15/2021, 7:43 AMDan Fingal-Surma
12/15/2021, 7:44 AMMichael de Kaste
12/15/2021, 7:46 AMMichael de Kaste
12/15/2021, 7:49 AMDan Fingal-Surma
12/15/2021, 7:51 AMDan Fingal-Surma
12/15/2021, 7:51 AMDan Fingal-Surma
12/15/2021, 7:51 AMDan Fingal-Surma
12/15/2021, 7:52 AMhttps://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#/media/File:Dijkstras_progress_animation.gifā¾
Dan Fingal-Surma
12/15/2021, 7:58 AMelizarov
12/15/2021, 8:37 AMDavid Whittaker
12/15/2021, 8:59 AMMichael de Kaste
12/15/2021, 9:01 AMephemient
12/15/2021, 11:38 AMephemient
12/15/2021, 11:39 AMephemient
12/15/2021, 11:39 AMephemient
12/15/2021, 11:39 AMMichael de Kaste
12/15/2021, 12:03 PMephemient
12/15/2021, 12:08 PMMarcin Wisniowski
12/15/2021, 2:11 PMDavid Whittaker
12/15/2021, 5:35 PMgetAdjacentSides
again. I definitely thought of you as I toiled away with my four separate checks.Dan Fingal-Surma
12/15/2021, 5:50 PMDan Fingal-Surma
12/15/2021, 5:54 PMDan Fingal-Surma
12/15/2021, 6:01 PMDan Fingal-Surma
12/15/2021, 6:03 PMDan Fingal-Surma
12/15/2021, 6:03 PMDan Fingal-Surma
12/15/2021, 6:22 PMDan Fingal-Surma
12/15/2021, 6:25 PMphldavies
12/15/2021, 6:48 PMDavid Whittaker
12/15/2021, 6:48 PMhttps://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#/media/File:Dijkstras_progress_animation.gifā¾
Dan Fingal-Surma
12/15/2021, 6:49 PMhttps://en.wikipedia.org/wiki/A*_search_algorithm#/media/File:Astar_progress_animation.gifā¾
Michael de Kaste
12/15/2021, 6:52 PMEdgars
12/15/2021, 7:02 PMephemient
12/15/2021, 7:21 PMphldavies
12/15/2021, 7:31 PMDan Fingal-Surma
12/15/2021, 7:35 PMDan Fingal-Surma
12/15/2021, 7:42 PMif (neighbor !in openSet) {
openSet.add(neighbor)
}
The neighbor may already be in the open set, but at a lower priority. You want to update its priority with the new value of fScore[neighbor]
. Simply modifying the fScore
map doesnāt move it to the correct place in the PQDan Fingal-Surma
12/15/2021, 7:43 PMDan Fingal-Surma
12/15/2021, 7:45 PM!in openSet
is the same complexity as remove
, so you arenāt paying morephldavies
12/15/2021, 7:49 PMDan Fingal-Surma
12/15/2021, 7:50 PMDan Fingal-Surma
12/15/2021, 7:51 PMphldavies
12/15/2021, 7:52 PMtodd.ginsberg
12/15/2021, 8:01 PMDan Fingal-Surma
12/15/2021, 8:04 PMTodor Grudev
12/15/2021, 8:34 PMtodd.ginsberg
12/15/2021, 8:53 PMEdgars
12/15/2021, 9:14 PMh()
to return 0 improved it by 100ms (from 2600ms). Doing openSet.add(neighbor)
without checking if it's in shaved off another 350ms. Removing fScore
altogether - another 200ms.Edgars
12/15/2021, 9:15 PMEdgars
12/15/2021, 9:17 PMDan Fingal-Surma
12/15/2021, 9:17 PMcameFrom
, in your code you can just get gScore[end]
at the endphldavies
12/15/2021, 9:18 PMephemient
12/15/2021, 9:18 PMphldavies
12/15/2021, 9:19 PMEdgars
12/15/2021, 9:25 PMMichael Bƶiers
12/15/2021, 10:03 PM