Skip to content

Instantly share code, notes, and snippets.

@paolino
Created November 12, 2022 18:33
Show Gist options
  • Save paolino/bc2aefb3bb4d9bf56f9606b453c0f681 to your computer and use it in GitHub Desktop.
Save paolino/bc2aefb3bb4d9bf56f9606b453c0f681 to your computer and use it in GitHub Desktop.
kata track from codewars
module CodewarRanking (incProgress, newUser, rank, progress) where
import Data.Foldable (find)
-- just for differentiate the 2 Ints in the game
-- deriving Enum and Num would improve the code, how ?
newtype Progress = Progress Int
deriving (Eq, Show)
-- just for differentiate the 2 Ints in the game
-- deriving Enum and Num would improve the code, how ? kind of different ;-)
newtype Rank = Rank Int
deriving (Eq, Show)
-- a cursor focused on the actual rank
-- ranks on the right are the next one to reach
-- on the left the one reached stored in reversed order
-- (comment on why we keep them after the exercise is done :-) )
data Cursor = Zip [Rank] Rank [Rank]
deriving (Show)
-- this is required from the exercise but set for this specific solution
data User = User Cursor Progress
deriving (Show)
newUser :: User
newUser = User
(Zip [] (Rank (-8)) $ fmap Rank ([-7 .. (-1)] <> [1 .. 8]))
(Progress 0)
rank :: User -> Int
rank (User (Zip _ (Rank r) _) _) = r
progress :: User -> Int
progress (User _ (Progress p)) = p
-- the solution depends on 2 functions for you to implement
incProgress :: Int -> User -> User
incProgress r' (User r p) = rankUp $ User r $ deltaProgress (Rank r') r p
-- raise progress with what results from reaching the given rank from the cursor
-- (hint: use function find from Data.List)
deltaProgress :: Rank -> Cursor -> Progress -> Progress
deltaProgress w (Zip os r rs) (Progress p) = Progress (p + dp)
where
dp = error "implement me"
-- raise the rank until progress is not possible or rank is 8
-- (hint: this is recursive process that consumes the progress)
-- (hint: take care of the enforced 0 for rank 8)
rankUp :: User -> User
rankUp = error "implement me"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment