Created
June 24, 2025 16:33
-
-
Save oantolin/8b1c3bea3dc65e714a799fb00aa47a57 to your computer and use it in GitHub Desktop.
A* implementation in BQN
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Heap β {π€ | |
| items β β¨β© | |
| Lt β 1+2Γβ’ β Rt β 2Γ1+β’ β Up β (βΓ·β2)-(Β¬2βΈ|) | |
| Swap β {items (β½βΎ(π¨βΏπ©βΈβ))β© β π©} | |
| SiftUp β SwapβUp β’_while_ {π©>0?<β(βββitems)βUp π©;0} | |
| MinChild β {(Rtπ©)<β items? {LtβΈ(<β(βββitems))βRt π©? Ltπ©; Rtπ©}π©; Ltπ©} | |
| SiftDown β {π:SwapβMinChildβ’_while_{(Ltπ©)<β items?>β(βββitems)βMinChild π©;0} 0} | |
| Add β {items βΎβ© β¨π¨βΏπ©β© β SiftUp (β items)-1 β @} | |
| PopMin β {π: tββitems β 0 Swap Β―1 β itemsβΛβ©Β―1 β SiftDown@ β t} | |
| Empty β {π:β¨β©β‘items} | |
| π¨ AddΒ¨ π© | |
| } | |
| _path_ β { | |
| Moves _π£_ Goal s0: 3=β’Type goal? Moves _π£_ GoalβΏ0 s0; | |
| Moves _π£_ GoalβΏH s0: | |
| costβΏfromβΏopen β β¨β¨s0β© β’HashMap β¨0β©, β¨β© β’HashMap β¨β©, β¨0β© Heap β¨s0β©β© | |
| end β {π€ | |
| s β 1βopen.PopMin@ | |
| {nβΏc: | |
| d β c+cost.Get s, | |
| {d<β cost.Get n? n cost.Set d β n from.Set s β (d + H n) open.Add n; @} | |
| }Λ (1==)βΆβ¨β’,ββ1Λβ© Moves s | |
| s | |
| } β’_while_ {Β¬(open.Empty@)β¨Goal π©} s0 | |
| {Goal end? (cost.Get end) β from.GetβββΈβΎ β’_while_ (from.Hasββ) β¨endβ©} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment