You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
Haskell
40 lines
1.3 KiB
Haskell
module Main where
|
|
|
|
import Control.Applicative
|
|
import Data.List
|
|
|
|
import qualified Data.Text as T
|
|
import qualified Data.Text.IO as TIO
|
|
import qualified Data.Text.Read as TR
|
|
|
|
import Data.Attoparsec.Text
|
|
|
|
main :: IO ()
|
|
main = do
|
|
input <- TIO.readFile "input.txt"
|
|
let theMap = treeMap $ map T.unpack $ T.lines input
|
|
let trajectories = map (trajectory theMap) $ [head slopes]
|
|
let hits = map (map (hitTree theMap)) trajectories
|
|
|
|
let answer = product $ map sum $ map (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) -> [(Int, Int)]
|
|
tobogganLocations (x,y) (dx,dy) (h,w) = let y' = (y + dy) `mod` w
|
|
x' = x + dx
|
|
in if x' >= h || x == h
|
|
then [(x' `mod` h, y')]
|
|
else (x, y): tobogganLocations (x',y') (dx,dy) (h,w)
|
|
|
|
hitTree :: [[Bool]] -> (Int, Int) -> Bool
|
|
hitTree bbs (x, y) = (bbs !! x) !! y
|
|
|
|
slopes :: [(Int, Int)]
|
|
slopes = [(1,3),(1,1),(1,5),(1,7),(2,1)]
|
|
|
|
trajectory :: [[Bool]] -> (Int, Int) -> [(Int, Int)]
|
|
trajectory theMap (x,y) = tobogganLocations (x,y) (x,y) (length theMap, length $ head theMap)
|