Codingame Puzzle 1 – Onboarding Haskell Solution - Foo Bar Code

Description of the puzzle

This is the first puzzle from codingame.com, in this puzzle we have to defend a planet with a big laser cannon from the invading insectoid alien ships !

Strategy to solve this puzzle

To solve this puzzle we need to find which ennemy is the closest to us and shoot him, the way I’ve solved that is by using the sortBy function to sort ennemies by distance and blast off the closest one 😀

My solution

import System.IO import Control.Monad import Data.List main :: IO () main = do hSetBuffering stdout NoBuffering -- DO NOT REMOVE -- The code below will read all the game information for you. -- On each game turn, information will be available on the standard input, you will be sent: -- -> the total number of visible enemies -- -> for each enemy, its name and distance from you -- The system will wait for you to write an enemy name on the standard output. -- Once you have designated a target: -- -> the cannon will shoot -- -> the enemies will move -- -> new info will be available for you to read on the standard input. loop loop :: IO () loop = do input_line <- getLine let count = read input_line :: Int -- The number of current enemy ships within range let ennemies = replicateM count $ do input_line <- getLine let input = words input_line let enemy = input!!0 -- The name of this enemy let dist = read (input!!1) :: Int -- The distance to your cannon of this enemy return (enemy,dist) do en <- ennemies let (name,_) = head $ sortBy (\ (_,a) (_,b) -> compare a b ) en putStrLn name loop

Some thoughts

When you sort values you need to be able to compare them and I really like one implement type comparison in Haskell.

In most languages you will have to implement some interface like

int CompareTo(Object obj)

where the return value acts as following:

Less than zero this instance precedes obj in the sort order. Zero this instance occurs in the same position in the sort order as obj. Greater than zero this instance follows obj in the sort order.

For example if you want to compare two cars by year of make  you’d write some code like

int IComparer.Compare(object a, object b) { car c1=(car)a; car c2=(car)b; if (c1.year > c2.year) return 1; if (c1.year < c2.year) return -1; else return 0; }

I’ve never really liked the fact that you have to deal with that by using this kind of hack and it always takes me a few seconds to remember what it will mean if I return a negative number or a positive number.

Compare this to the Haskell implementation:

comparer Car {year=c1} Car {year=c2} | c1 >= c2 = GT | c1 == c2 = EQ | c1 <= c2 = LT

in Haskell you get to return meaningful values like: LT,GT and EQ.

Conclusion

I really enjoyed  solving this first puzzle, I’ve played around with the IDE and the user experience is really nice.

This is going to be a really fun way to keep learning more about Haskell since I finished my Edx course.

Share this:

  • X
  • Facebook
Like Loading...

Related

Tag » Codingame Solution Onboarding