X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent02.hs;h=d4abc555865e2af68e70d161f441f9edd7e6a199;hb=1d5f2538057b07e5b5b144a5353f21f10eeb836c;hp=ee3f381762a3ed59b327d4993d7c0cf44b974086;hpb=e89c41eb95bbdee3c69e51a055706f37ac78c5de;p=advent-of-code-16.git diff --git a/advent02.hs b/advent02.hs index ee3f381..d4abc55 100644 --- a/advent02.hs +++ b/advent02.hs @@ -1,17 +1,16 @@ -import Data.List (sort) -import Data.List.Split (splitOn) -import Data.Array.IArray +module Main(main) where +import Data.Array.IArray +-- Row 1 is top, column 1 is left type Position = (Int, Int) type Keyboard = Array Position Char -kb1 = [['x', 'x', 'x', 'x', 'x'], - ['x', '1', '2', '3', 'x'], - ['x', '4', '5', '6', 'x'], - ['x', '7', '8', '9', 'x'], - ['x', 'x', 'x', 'x', 'x']] - +kb1 = ["xxxxx", + "x123x", + "x456x", + "x789x", + "xxxxx"] kb2 = ["xxxxxxx", "xxx1xxx", @@ -24,14 +23,14 @@ kb2 = ["xxxxxxx", enumerate = zip [0..] mkKeyboard :: [String] -> Keyboard -mkKeyboard kb = array ((0, 0), (length kb - 1, length kb - 1)) +mkKeyboard kb = array ((0, 0), (length kb - 1, length (kb!!0) - 1)) [((i, j), c) | (i, r) <- enumerate kb, (j, c) <- enumerate r] keyboard1 = mkKeyboard kb1 keyboard2 = mkKeyboard kb2 findKey :: Keyboard -> Char-> Position -findKey kb c = fst $ head $ filter (\(i, e) -> e == c) $ assocs kb +findKey kb c = fst $ head $ filter (\a -> (snd a) == c) $ assocs kb -- data Coord = One | Two | Three -- deriving (Read, Show, Eq, Ord, Enum, Bounded) @@ -39,7 +38,6 @@ findKey kb c = fst $ head $ filter (\(i, e) -> e == c) $ assocs kb -- -- minBound = Coord 1 -- -- maxBound = Coord 3 --- -- Row 1 is top, column 1 is left -- data Position = Position Coord Coord -- deriving (Show, Eq) @@ -81,34 +79,12 @@ safeMove :: Keyboard -> Position -> Char -> Position safeMove kb pos dir = maybeRevert kb pos (move pos dir) move :: Position -> Char -> Position -move (r, c) 'U' = (dec r, c) -move (r, c) 'D' = (inc r, c) -move (r, c) 'L' = (r, dec c) -move (r, c) 'R' = (r, inc c) +move (r, c) 'U' = (r-1, c) +move (r, c) 'D' = (r+1, c) +move (r, c) 'L' = (r, c-1) +move (r, c) 'R' = (r, c+1) maybeRevert :: Keyboard -> Position -> Position -> Position maybeRevert kb oldPos newPos | kb ! newPos == 'x' = oldPos | otherwise = newPos - -numberOf p = '1' --- numberOf :: Position -> Char --- numberOf (Position One One) = '1' --- numberOf (Position One Two) = '2' --- numberOf (Position One Three) = '3' --- numberOf (Position Two One) = '4' --- numberOf (Position Two Two) = '5' --- numberOf (Position Two Three) = '6' --- numberOf (Position Three One) = '7' --- numberOf (Position Three Two) = '8' --- numberOf (Position Three Three) = '9' - --- | a `succ` that stops -inc :: (Bounded a, Enum a, Eq a) => a -> a -inc dir | dir == maxBound = maxBound - | otherwise = succ dir - --- | a `pred` that stops -dec :: (Bounded a, Enum a, Eq a) => a -> a -dec dir | dir == minBound = minBound - | otherwise = pred dir \ No newline at end of file