blob: aa8008db49b679cfb5657a52829268ab57446c5e (
plain)
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
|
-- requires cabal install --lib megaparsec parser-combinators heap vector
module Main (main) where
import Control.Monad (void, when)
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import Data.Void (Void)
import Text.Megaparsec
import Text.Megaparsec.Char
exampleExpectedOutput = 9
type Line = VU.Vector Char
type Input = V.Vector Line
type Parser = Parsec Void String
parseLine :: Parser Line
parseLine = do
line <- some letterChar <* eol
return $ VU.generate (length line) (line !!)
parseInput' :: Parser Input
parseInput' = do
line <- some parseLine <* eof
return $ V.generate (length line) (line !!)
parseInput :: String -> IO Input
parseInput filename = do
input <- readFile filename
case runParser parseInput' filename input of
Left bundle -> error $ errorBundlePretty bundle
Right input' -> return input'
compute :: Input -> Int
compute g = length $ filter xmas as
where
l = V.length g
as = [(x, y) | x<-[1..l-2], y<-[1..l-2], g V.! x VU.! y == 'A']
xmas :: (Int, Int) -> Bool
xmas (x, y) = d1 && d2
where
tl = g V.! (x-1) VU.! (y-1)
tr = g V.! (x+1) VU.! (y-1)
bl = g V.! (x-1) VU.! (y+1)
br = g V.! (x+1) VU.! (y+1)
d1 = tl == 'M' && br == 'S' || tl == 'S' && br == 'M'
d2 = bl == 'M' && tr == 'S' || bl == 'S' && tr == 'M'
main :: IO ()
main = do
example <- parseInput "example"
let exampleOutput = compute example
when (exampleOutput /= exampleExpectedOutput) (error $ "example failed: got " ++ show exampleOutput ++ " instead of " ++ show exampleExpectedOutput)
input <- parseInput "input"
print $ compute input
|