Requirement: write a function that determines the distance between two tiles in a grid such that the result is the number of 'steps' required to get to the 2nd tile (i.e. not a straight line distance). Diagonal movement is permitted.
Input: x1,y1,x2,y2 (two tiles. Integer values. May be negative)
Output: an integer value >= 0
Example:
moving from 0,0 to 3,2 = 3 steps.
moving from 1,5 to 3,2 = 3 steps.
moving from 2,-2 to 3,2 = 4 steps.
Not hard. Just fun - and can do on the bus/train.
15 minute code challenge - for fun
15 minute code challenge - for fun
Last edited by togFox on Tue Jun 22, 2021 10:20 am, edited 2 times in total.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Re: 15 minute code challenge - for fun
oops - 4 steps. I'll add that diagonal movement is permitted.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Re: 15 minute code challenge - for fun
Less than one minute:Diagonal movement is permitted.
Code: Select all
math.max (math.abs(x2-x1), math.abs(y2-y1))
Re: 15 minute code challenge - for fun
Bravo, darkfrei! I wouldn't have come up with such an elegant and simple answer. Love it!
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Re: 15 minute code challenge - for fun
Knowing about metric spaces helps
Re: 15 minute code challenge - for fun
Another version of this exercise:
Straight movement costs s = 10 points, diagonal movement costs d = 14 points.
Straight movement costs s = 10 points, diagonal movement costs d = 14 points.
Re: 15 minute code challenge - for fun
Noice. I was hoping it would take longer than 1 minute. I'll try harder next time.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Re: 15 minute code challenge - for fun
Code: Select all
local function weightedDistance(x1, y1, x2, y2, ws, wd)
local dx = math.abs(x2 - x1)
local dy = math.abs(y2 - y1)
local diagSteps = math.min(dx, dy)
local straightSteps = math.max(dx, dy) - diagSteps
return diagSteps * wd + straightSteps * ws
end
Re: 15 minute code challenge - for fun
@pgimeno, nice! It was my solution too
The next one: ws = 3, but wd = 1: the diagonal movement is cheaper and it has higher priority for movement.
The next one: ws = 3, but wd = 1: the diagonal movement is cheaper and it has higher priority for movement.
Who is online
Users browsing this forum: No registered users and 4 guests