DEV Community

loading...

Discussion on: Daily Challenge #125 - 23 Matches or More

Collapse
craigmc08 profile image
Craig McIlwrath

This is my solution in Haskell. The "object" containing the function is pretty awkward in Haskell. It would probably be better to just use the makeMove function directly or partially applied.

Quick note: In my exploration of this game, it seems like if the matches remaining at the beginning of your turn is in a specific arithmetic sequence, then you have to rely on the opponent making a mistake. With my bot, this can only happen if the starting number of matches is in this sequence. If this happen, it just picks up as many matches as possible to lose as fast as possible (hence, "Pessimist Bot").

data Bot = Bot { getName :: String, getMakeMove :: Integer -> Integer }

makeMove :: Integer -> Integer -> Integer
makeMove l m
  | m == 1    = 1
  | m < l + 2 = m - 1
  | otherwise = let d = minimum [m - z | t <- [1..m], let z = (l + 1) * t + 1, z <= m]
                in  if d > l || d == 0
                      then l -- Give up, the only way to win is the opponent messes up
                      else d

createBot :: Integer -> Integer -> Bot
createBot n l = Bot "Pessimist Bot" $ makeMove l