48 lines
1.4 KiB
Haskell
48 lines
1.4 KiB
Haskell
module Day13_1 where
|
|
import System.IO (openFile)
|
|
import Data.Text (unpack, splitOn, pack)
|
|
import Prelude hiding (splitAt)
|
|
import Data.List (nub)
|
|
|
|
splitAt :: String -> String -> [String]
|
|
splitAt delimiter str = map unpack $ splitOn (pack delimiter) $ pack str
|
|
|
|
insert :: Int -> Char -> [Char] -> [Char]
|
|
insert 0 y xs = xs
|
|
insert n y [] = []
|
|
insert n y xs
|
|
| length xs < n = xs
|
|
| otherwise = take n xs ++ [y] ++ insert n y (drop n xs)
|
|
|
|
|
|
fold :: String -> (Int, Int) -> (Int, Int)
|
|
fold str (x, y)
|
|
| 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'
|
|
|
|
let result = map (\(a,b) -> (b,a))
|
|
$ nub
|
|
$ foldl (\acc x -> map (fold x) acc) coords folds
|
|
|
|
let width = maximum $ snd <$> result
|
|
height = maximum $ fst <$> result
|
|
field = (,) <$> [0..height] <*> [0..width]
|
|
|
|
let display x
|
|
| x `elem` result = '#'
|
|
| otherwise = ' '
|
|
in putStrLn $ insert (width+1) '\n' $ map display field
|