|
|
|
@ -11,52 +11,23 @@ import Data.Attoparsec.Text
|
|
|
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
|
main = do
|
|
|
|
|
inputs <- TIO.readFile "input.txt"
|
|
|
|
|
let Just entries = maybeResult $ parse entriesParser inputs
|
|
|
|
|
TIO.putStrLn $ T.pack $ show $ sum $ entries2ints entries
|
|
|
|
|
|
|
|
|
|
data Entry = Entry
|
|
|
|
|
{ policy :: Policy
|
|
|
|
|
, password :: String
|
|
|
|
|
} deriving (Show,Read)
|
|
|
|
|
|
|
|
|
|
data Policy = Policy
|
|
|
|
|
{ first :: Int
|
|
|
|
|
, second :: Int
|
|
|
|
|
, character :: Char
|
|
|
|
|
} deriving (Show, Read)
|
|
|
|
|
|
|
|
|
|
policyParser :: Parser Policy
|
|
|
|
|
policyParser = do
|
|
|
|
|
frst <- many digit
|
|
|
|
|
string "-"
|
|
|
|
|
scnd <- many digit
|
|
|
|
|
space
|
|
|
|
|
character <- letter
|
|
|
|
|
let Right (first, _) = TR.decimal $ T.pack $ frst
|
|
|
|
|
let Right (second, _) = TR.decimal $ T.pack $ scnd
|
|
|
|
|
return Policy { first = first - 1, second = second - 1, character = character }
|
|
|
|
|
|
|
|
|
|
entryParser :: Parser Entry
|
|
|
|
|
entryParser = do
|
|
|
|
|
policy <- policyParser
|
|
|
|
|
char ':'
|
|
|
|
|
space
|
|
|
|
|
password <- many letter
|
|
|
|
|
endOfLine
|
|
|
|
|
return Entry { policy = policy, password = password }
|
|
|
|
|
|
|
|
|
|
checkEntry :: Entry -> Bool
|
|
|
|
|
checkEntry e = let p = policy e
|
|
|
|
|
frst = first p
|
|
|
|
|
scnd = second p
|
|
|
|
|
indicies = findIndices ((character p)==) (password e)
|
|
|
|
|
in ((frst `elem` indicies) && (scnd `notElem` indicies)) || ((frst `notElem` indicies) && (scnd `elem` indicies))
|
|
|
|
|
|
|
|
|
|
entriesParser :: Parser [Entry]
|
|
|
|
|
entriesParser = do
|
|
|
|
|
entries <- many entryParser
|
|
|
|
|
return entries
|
|
|
|
|
|
|
|
|
|
entries2ints :: [Entry] -> [Int]
|
|
|
|
|
entries2ints es = map (\e -> if checkEntry e then 1 else 0) es
|
|
|
|
|
input <- TIO.readFile "input.txt"
|
|
|
|
|
let slope = treeMap $ map T.unpack $ T.lines input
|
|
|
|
|
let hits = map (hitTree slope) $ tobogganLocations 1 3 (length slope) (length $ head slope)
|
|
|
|
|
let answer = sum $ map (\x -> if x then 1 else 0) hits
|
|
|
|
|
TIO.putStrLn $ T.pack $ show answer
|
|
|
|
|
|
|
|
|
|
treeMap :: [String] -> [[Bool]]
|
|
|
|
|
treeMap tts = map (map ('#' ==)) tts
|
|
|
|
|
|
|
|
|
|
tobogganLocations :: Int -> Int -> Int -> Int -> [(Int, Int)]
|
|
|
|
|
tobogganLocations x y h w = if x == h
|
|
|
|
|
then []
|
|
|
|
|
else (x, y): let y' = (y + 3) `mod` w
|
|
|
|
|
x' = x + 1
|
|
|
|
|
in tobogganLocations x' y' h w
|
|
|
|
|
|
|
|
|
|
hitTree :: [[Bool]] -> (Int, Int) -> Bool
|
|
|
|
|
hitTree bbs (x, y) = (bbs !! x) !! y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|