-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvode.hs
75 lines (55 loc) · 1.59 KB
/
Advode.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Control.Applicative
import Control.Monad
import Data.Bifunctor
import Data.Bits
import Data.Char
import Data.List
import qualified Data.Map as Map
import Data.Maybe
import Data.Monoid
import qualified Data.Set as Set
import System.IO
import Text.Read
--- string parsing ---
split :: (a -> Bool) -> [a] -> [[a]]
split p s = case break p s of
(a, []) -> [a]
(a, b) -> a : split p (drop 1 b)
strip :: String -> String
strip = dropWhile isSpace . dropWhileEnd isSpace
parseLines :: String -> [String]
parseLines = dropWhileEnd null . split (=='\n')
parseGroups :: String -> [[String]]
parseGroups = split null . parseLines
parseNumbers :: String -> [Int]
parseNumbers s = read <$> filter (/="") (split isSpace s)
--- utility ---
enumerate :: [a] -> [(Int, a)]
enumerate = zip [0..]
enumerate2d :: [[a]] -> [(Point, a)]
enumerate2d grid = [((x, y), c)
| (x, row) <- enumerate grid
, (y, c) <- enumerate row
]
--- coordinates ---
type Point = (Int, Int)
d4, d8 :: [Point]
d4 = [(1, 0), (0, 1), (-1, 0), (0, -1)]
d8 = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
zeroPoint :: Point
zeroPoint = (0, 0)
addPoint :: Point -> Point -> Point
addPoint (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
subPoint :: Point -> Point -> Point
subPoint (x1, y1) (x2, y2) = (x1 - x2, y1 - y2)
mulPoint :: Int -> Point -> Point
mulPoint c (x, y) = (c*x, c*y)
neighbors :: [Point] -> Point -> [Point]
neighbors d p = addPoint p <$> d
bounds :: Int -> Int -> Point -> Bool
bounds m n (x, y) = x >= 0 && x < m && y >= 0 && y < n
---
main :: IO ()
main = do
input <- getContents
print ""