about summary refs log tree commit diff
path: root/users/wpcarro/assessments/dotted-squares/Main.hs
blob: 44f91e2b231193f4e378743d43b867c12a9689fb (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
{-# LANGUAGE DeriveGeneric #-}
--------------------------------------------------------------------------------
module Main where
--------------------------------------------------------------------------------
import Data.Hashable
import Data.Function ((&))
import GHC.Generics
import Text.ParserCombinators.ReadP
import Control.Applicative

import qualified Data.HashSet as HS
--------------------------------------------------------------------------------

data Direction
  = DirLeft
  | DirRight
  | DirUp
  | DirDown
  deriving (Eq, Show)

data Point = Point Int Int
  deriving (Eq, Show, Ord, Generic)
instance Hashable Point

data Orientation
  = Horizontal
  | Vertical
  deriving (Eq, Show)

data Anchor
  = Beg
  | End
  deriving (Eq, Show)

data Rotation
  = CW
  | CCW
  deriving (Eq, Show)

data Line = Line Point Point
  deriving (Show, Generic)
instance Hashable Line

instance Eq Line where
  Line begA endA == Line begB endB =
    (begA == begB && endA == endB) ||
    (begA == endB && endA == begB)

data Game = Game (HS.HashSet Line) [Line]
  deriving (Eq, Show)

data Scoreboard = Scoreboard Int Int
  deriving (Eq)

instance Semigroup Scoreboard where
  (Scoreboard a b) <> (Scoreboard x y) =
    Scoreboard (a + x) (b + y)

instance Monoid Scoreboard where
  mempty = Scoreboard 0 0

data Turn
  = Player1
  | Player2
  deriving (Eq, Show)

next :: Turn -> Turn
next Player1 = Player2
next Player2 = Player1

instance Show Scoreboard where
  show (Scoreboard p1 p2) =
    "Player 1: " ++ show (p1) ++ " Player 2: " ++ show (p2)

digit :: ReadP Char
digit = satisfy (\c -> c >= '0' && c <= '9')

int :: ReadP Int
int = read <$> many1 digit

inputLine :: ReadP String
inputLine = manyTill get (char '\n')

direction :: ReadP Direction
direction = do
  c <- char 'L' <|> char 'R' <|> char 'U' <|> char 'D'
  case c of
    'L' -> pure DirLeft
    'R' -> pure DirRight
    'U' -> pure DirUp
    'D' -> pure DirDown
    _   -> fail $ "Unexpected direction: " ++ show c

validMove :: Int -> Int -> ReadP Line
validMove w h = do
  x <- int
  skipSpaces
  y <- int
  skipSpaces
  dir <- direction
  _ <- char '\n'
  if x >= 0 && x <= w &&  y >= 0 && y <= h then do
    let beg = Point x y
    pure $ mkLine beg (shiftPoint dir beg)
  else
    fail "Expected a move on the game board"

game :: ReadP Game
game = do
  w <- read <$> inputLine
  h <- read <$> inputLine
  locs <- read <$> inputLine
  moves <- count locs (validMove w h)
  eof
  pure $ Game mempty moves

parseInput :: String -> Maybe Game
parseInput x = do
  case readP_to_S game x of
    [(res, "")] -> Just res
    _ -> Nothing

-- | Smart constructor to ensure that beg is always < end.
mkLine :: Point -> Point -> Line
mkLine beg end =
  if beg < end then Line beg end else Line end beg

mkLineDir :: Int -> Int -> Direction -> Line
mkLineDir x y dir =
  let beg = Point x y
  in mkLine beg (shiftPoint dir beg)

mkLineDir' :: Point -> Direction -> Line
mkLineDir' (Point x y) dir = mkLineDir x y dir

shiftPoint :: Direction -> Point -> Point
shiftPoint DirLeft  (Point x y) = Point (x - 1) y
shiftPoint DirRight (Point x y) = Point (x + 1) y
shiftPoint DirUp    (Point x y) = Point x (y + 1)
shiftPoint DirDown  (Point x y) = Point x (y - 1)

shiftLine :: Direction -> Line -> Line
shiftLine dir (Line beg end) =
  mkLine (shiftPoint dir beg) (shiftPoint dir end)

rotateLine :: Anchor -> Rotation -> Line -> Line
rotateLine anchor rotation line =
  doRotateLine (classifyOrientation line) anchor rotation line

doRotateLine :: Orientation -> Anchor -> Rotation -> Line -> Line
doRotateLine Horizontal Beg CW  (Line beg _) = mkLineDir' beg DirDown
doRotateLine Horizontal Beg CCW (Line beg _) = mkLineDir' beg DirUp
doRotateLine Horizontal End CW  (Line _ end) = mkLineDir' end DirUp
doRotateLine Horizontal End CCW (Line _ end) = mkLineDir' end DirDown
doRotateLine Vertical   Beg CW  (Line beg _) = mkLineDir' beg DirRight
doRotateLine Vertical   Beg CCW (Line beg _) = mkLineDir' beg DirLeft
doRotateLine Vertical   End CW  (Line _ end) = mkLineDir' end DirLeft
doRotateLine Vertical   End CCW (Line _ end) = mkLineDir' end DirRight

classifyOrientation :: Line -> Orientation
classifyOrientation (Line (Point _ y1) (Point _ y2)) =
  if y1 == y2 then Horizontal else Vertical

closesAnySquare :: HS.HashSet Line -> Line -> Bool
closesAnySquare allMoves line = do
  let alreadyDrawn x = HS.member x allMoves
  case classifyOrientation line of
    Horizontal ->
      all alreadyDrawn
        [ shiftLine DirUp line
        , rotateLine Beg CCW line
        , rotateLine End CW line
        ] ||
      all alreadyDrawn
        [ shiftLine DirDown line
        , rotateLine Beg CW line
        , rotateLine End CCW line
        ]
    Vertical ->
      all alreadyDrawn
        [ shiftLine DirLeft line
        , rotateLine Beg CCW line
        , rotateLine End CW line
        ] ||
      all alreadyDrawn
        [ shiftLine DirRight line
        , rotateLine Beg CW line
        , rotateLine End CCW line
        ]

incScoreboard :: Turn -> Scoreboard -> Scoreboard
incScoreboard Player1 score = score <> Scoreboard 1 0
incScoreboard Player2 score = score <> Scoreboard 0 1

scoreGame :: Turn -> Game -> Scoreboard -> Maybe Scoreboard
scoreGame _ (Game _ []) score = Just $ score
scoreGame player (Game allMoves (line:rest)) score =
  if HS.member line allMoves then
    Nothing
  else do
    let allMoves' = HS.insert line allMoves
        score' = if closesAnySquare allMoves line then
                   incScoreboard player score
                 else score
    scoreGame (next player) (Game allMoves' rest) score'

(|>) :: a -> (a -> b) -> b
(|>) = (&)

main :: IO ()
main = do
  input <- readFile "game.txt"
  case parseInput input of
    Nothing -> putStrLn "invalid"
    Just parsedGame ->
      case scoreGame Player1 parsedGame mempty of
        Nothing -> putStrLn "invalid"
        Just score -> print score