aoc2021/Day13.hs

29 lines
929 B
Haskell

module Day13 where
import System.IO (openFile)
import Data.Text (unpack, splitOn, pack)
import Prelude hiding (splitAt)
import Data.Char (digitToInt)
import Data.List (nub)
splitAt :: String -> String -> [String]
splitAt delimiter str = map unpack $ splitOn (pack delimiter) $ pack str
fold :: (Int, Int) -> String -> (Int, Int)
fold (x, y) str
| head str == 'x' = (fold' x, y)
| head str == 'y' = (x, fold' y)
where coord = read . last $ splitAt "=" str
fold' num
| num > coord = coord - (num - coord)
| otherwise = num
main :: IO ()
main = do
input <- readFile "inputs/13.txt"
let (coords', folds') = (\[a,b] -> (lines a, lines b)) $ splitAt "\n\n" input
--
let coords = (\[a,b] -> (read a, read b) :: (Int, Int)) <$> map (splitAt ",") coords'
folds = map (last . splitAt " ") folds'
print . length . nub $ [fold pos (head folds) | pos <- coords ]