15 minute code challenge - for fun

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

15 minute code challenge - for fun

Post by togFox »

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. :)
Last edited by togFox on Tue Jun 22, 2021 10:20 am, edited 2 times in total.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: 15 minute code challenge - for fun

Post by grump »

togFox wrote: Tue Jun 22, 2021 6:41 am moving from 2,-2 to 3,2 = 5 steps.
You should check your code for bugs.
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: 15 minute code challenge - for fun

Post by togFox »

oops - 4 steps. :) I'll add that diagonal movement is permitted.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: 15 minute code challenge - for fun

Post by darkfrei »

Diagonal movement is permitted.
Less than one minute:

Code: Select all

math.max (math.abs(x2-x1), math.abs(y2-y1))
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: 15 minute code challenge - for fun

Post by milon »

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.
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: 15 minute code challenge - for fun

Post by pgimeno »

Knowing about metric spaces helps ;)
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: 15 minute code challenge - for fun

Post by darkfrei »

Another version of this exercise:
Straight movement costs s = 10 points, diagonal movement costs d = 14 points.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 764
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: 15 minute code challenge - for fun

Post by togFox »

Noice. I was hoping it would take longer than 1 minute. :) I'll try harder next time. ;)
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
pgimeno
Party member
Posts: 3541
Joined: Sun Oct 18, 2015 2:58 pm

Re: 15 minute code challenge - for fun

Post by pgimeno »

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
where ws = cost (weight) straight and wd = cost diagonal
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: 15 minute code challenge - for fun

Post by darkfrei »

@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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests