diff options
Diffstat (limited to '2021/day13/13a.hs')
-rw-r--r-- | 2021/day13/13a.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/2021/day13/13a.hs b/2021/day13/13a.hs new file mode 100644 index 0000000..79af7ae --- /dev/null +++ b/2021/day13/13a.hs @@ -0,0 +1,34 @@ +{-# LANGUAGE ViewPatterns #-} +{-# LANGUAGE OverloadedStrings #-} +module Main where + +import qualified Data.Text as T +import Data.Set (Set) +import Data.List +import qualified Data.Set as S + +type Coord = (Int, Int) +data FoldInstr = Fold Axis Int deriving (Show, Eq, Ord) +data Axis = XAxis | YAxis deriving (Show, Eq, Ord) + +parseCoords :: String -> Coord +parseCoords s = (read x, read y) + where [x, y] = map T.unpack $ T.splitOn "," (T.pack s) + +parseFold :: String -> FoldInstr +parseFold (stripPrefix "fold along y=" -> Just cs) = Fold YAxis (read cs) +parseFold (stripPrefix "fold along x=" -> Just cs) = Fold XAxis (read cs) + +parseFile :: String -> (Set Coord, [FoldInstr]) +parseFile s = (S.fromList $ map parseCoords (lines coordSection), map parseFold (lines foldSection)) + where [coordSection, foldSection] = map T.unpack $ T.splitOn "\n\n" (T.pack s) + +performFold :: Set Coord -> FoldInstr -> Set Coord +performFold coords (Fold YAxis ye) = S.map (\(x, y) -> (x, -(abs (y - ye)) + ye)) coords +performFold coords (Fold XAxis xe) = S.map (\(x, y) -> (-(abs (x - xe)) + xe, y)) coords + +main :: IO () +main = do + f <- readFile "./input" + let (coords, instrs) = parseFile f; + print $ length $ performFold coords (head instrs) |