{"id":20010649,"url":"https://github.com/bradhowes/astar","last_synced_at":"2025-03-02T01:46:55.169Z","repository":{"id":63906044,"uuid":"257982346","full_name":"bradhowes/astar","owner":"bradhowes","description":"A* path finding library in Swift","archived":false,"fork":false,"pushed_at":"2023-12-19T17:34:53.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-02T02:56:43.475Z","etag":null,"topics":["algorithm","pathfinding","swift-package-manager","swift5"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bradhowes.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"bradhowes"}},"created_at":"2020-04-22T18:11:03.000Z","updated_at":"2022-04-13T07:09:53.000Z","dependencies_parsed_at":"2024-11-13T07:23:24.525Z","dependency_job_id":"8b946a65-ff51-4fee-b142-019ad69a4870","html_url":"https://github.com/bradhowes/astar","commit_stats":{"total_commits":28,"total_committers":1,"mean_commits":28.0,"dds":0.0,"last_synced_commit":"cf55d641ac4d6c505d196e7dccdcca7b9aa4cb7e"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradhowes%2Fastar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradhowes%2Fastar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradhowes%2Fastar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bradhowes%2Fastar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bradhowes","download_url":"https://codeload.github.com/bradhowes/astar/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241447523,"owners_count":19964314,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","pathfinding","swift-package-manager","swift5"],"created_at":"2024-11-13T07:21:00.942Z","updated_at":"2025-03-02T01:46:55.149Z","avatar_url":"https://github.com/bradhowes.png","language":"Swift","readme":"[![Swift](https://github.com/bradhowes/AStar/workflows/CI/badge.svg)]()\n[![Swift Badge]][Swift]\n[![SwiftPM compatible](https://img.shields.io/badge/SwiftPM-compatible-brightgreen.svg)](https://swift.org/package-manager/)\n[![License Badge]][License]\n\n# AStar - an A* Library in Swift\n\nThe AStar library implements the classic A* path-finding algorithm. It uses a min priority queue for managing potential paths, ordered by each\npath's known and estimated cost. The AStar class delegates map-related functionality to a `MapOracle` protocol to determine valid positions\nas well as the cost of using a position. Example of a `MapOracle` can be found in the `AStarTests.swift` file.\n\nThe AStar API is very basic. There is just the static `find` method. Here is an example of it being used:\n\n```swift\nlet mapData = MapData(data: [\n    [.🌊, .🌲, .🌲, .🌲, .🌲, .🌲, .🌲, .🌲],\n    [.🌊, .🌲, .🌲, .🌲, .🌲, .🌲, .🌲, .🌲],\n    [.🌲, .🌲, .🌲, .🌲, .🗻, .🌲, .🌲, .🌲],\n    [.🌲, .🌲, .🗻, .🗻, .🗻, .🗻, .🗻, .🌲],\n    [.🌲, .🌲, .🗻, .🌲, .🌲, .🗻, .🌊, .🌊],\n    [.🌲, .🌲, .🗻, .🌲, .🗻, .🌲, .🌲, .🌊],\n    [.🌊, .🌲, .🗻, .🌲, .🌲, .🌲, .🗻, .🗻],\n    [.🌊, .🌲, .🌲, .🌲, .🌲, .🌲, .🗻, .🌲]\n])\n\nlet start = Coord2D(x: 4, y: 0)\nlet end = Coord2D(x: 4, y: 4)\nfunc distanceToEnd(position: Coord2D) -\u003e Int { abs(position.x - end.x) + abs(position.y - end.y) }\nlet path = AStar.find(mapOracle: mapOracle, considerDiagonalPaths: true,\n                      heuristicCostCalulator: distancToEnd,\n                      start: start, end: end)\n```\n\nYou supply something that implements the `MapOracle` protocol like the `MapData` above. You decide if diagonal paths are acceptable,\nand provide a way to estimate the cost of moving from a given point on the map to the end point (the _heuristic cost_). The start and end\npoints complete the `find` request.\n\nYou get back an optional array of `Coord2D` values. If `nil` then there was no path to be found. Otherwise, the array will have the map\ncoordinates of the path that was found, starting at `start` and ending with `end`.\n\nHere is the visual representation of the map with the found path. The starting position appears as a red flag (🚩) and the end position is a\ncheckered flag (🏁). The path in between these two points contains an adventurer (🏃).\n\n```swift\nlet image = mapData.asString(path: path!)\nprint(image)\n🌊🌲🌲🌲🚩🌲🌲🌲\n🌊🌲🌲🌲🌲🏃🌲🌲\n🌲🌲🌲🌲🗻🌲🏃🌲\n🌲🌲🗻🗻🗻🗻🗻🏃\n🌲🌲🗻🌲🏁🗻🏃🌊\n🌲🌲🗻🌲🗻🏃🌲🌊\n🌊🌲🗻🌲🌲🌲🗻🗻\n🌊🌲🌲🌲🌲🌲🗻🌲\n```\n\nThe map contains three different terrain elements, each with their own cost for travelling into their square:\n\n* 🌲 tree (1)\n* 🌊 water (2)\n* 🗻 boulder (∞)\n\nThe algorithm minimizes the cost of traveling over terrain elements while at the same time trying to keep to the shortest path\nto the goal. For comparison, here is what the algorithm found when constrained to not use diagonal moves:\n\n```swift\n🌊🌲🌲🌲🚩🌲🌲🌲\n🌊🌲🌲🏃🏃🌲🌲🌲\n🌲🏃🏃🏃🗻🌲🌲🌲\n🌲🏃🗻🗻🗻🗻🗻🌲\n🌲🏃🗻🏃🏁🗻🌊🌊\n🌲🏃🗻🏃🗻🌲🌲🌊\n🌊🏃🗻🏃🌲🌲🗻🗻\n🌊🏃🏃🏃🌲🌲🗻🌲\n\n```\n\nThere is another path to the right that is also 16 moves, but it goes over two 🌊 positions which increases the total cost of the trip by 2. Thus the algorithm chose the one shown above.\n\n[License Badge]: https://img.shields.io/github/license/bradhowes/AStar.svg?color=yellow \"MIT License\"\n[License]: https://github.com/bradhowes/AStar/blob/master/LICENSE.txt\n\n[Swift Badge]: https://img.shields.io/badge/swift-5.2-orange.svg \"Swift 5.2\"\n[Swift]: https://swift.org/blog/swift-5-2-released/\n","funding_links":["https://github.com/sponsors/bradhowes"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbradhowes%2Fastar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbradhowes%2Fastar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbradhowes%2Fastar/lists"}