Something went wrong. Try again.
Code for the Advent of Code event
aoc advent-of-code
Something went wrong. Try again.
710 B · 23 lines
Ruby
at rust
123456789101112131415161718192021222324#!/usr/bin/env ruby# frozen_string_literal: true
require_relative '../lib/astar'require_relative '../lib/grid'
def heuristic(a, b) x1, y1 = a.to_a x2, y2 = b.to_a (x1 - x2).abs + (y1 - y2).absend
diagram4 = GridWithWeights.new 10, 10diagram4.walls = [[1, 7], [1, 8], [2, 7], [2, 8], [3, 7], [3, 8]].map { Vector[*_1] }
start, goal = Vector[1, 4], Vector[1, 9]came_from, cost_so_far = AStar.find_path(diagram4, start, goal, &method(:heuristic))diagram4.draw point_to: came_from, start: start, goal: goalpath = AStar.reconstruct_path(came_from, start, goal)diagram4.draw path: pathdiagram4.draw number: cost_so_far, start: start, goal: goal
puts "Shortest path has cost #{cost_so_far[goal]}"