This commit is contained in:
Quinten Kock 2021-12-13 06:51:01 +01:00
parent aedbb3434e
commit c26ae17d4c
1 changed files with 42 additions and 0 deletions

42
2021/day13.hs Normal file
View File

@ -0,0 +1,42 @@
module Day13 where
import Data.List.Split (splitOn)
import Data.List (nub, sort)
import Debug.Trace (trace)
type Dot = (Int,Int)
data Fold = Vertical Int | Horizontal Int deriving Show
parse :: String -> Dot
parse s = (read x, read y) where [x,y] = splitOn "," s
parseFold :: String -> Fold
parseFold s = if axis == "x" then Horizontal (read line) else Vertical (read line) where [axis,line] = splitOn "=" $ last (words s)
fold :: Fold -> Dot -> Dot
fold fold@(Vertical f) (x,y) = (x, if y > f then f - (y-f) else y)
fold fold@(Horizontal f) (x,y) = (if x > f then f - (x-f) else x, y)
main :: IO ()
main = do
input <- lines <$> getContents
let [dots',folds'] = splitOn [""] input
let dots = nub $ map parse dots'
let folds = map parseFold folds'
putStr "part 1: "
let dots1 = nub $ sort $ map (fold (head folds)) dots
print $ length dots1
putStr "part 2: "
let dots2 = nub $ sort $ map (\d -> foldl (flip fold) d folds) dots
print dots2
let min_x = minimum $ map fst dots2
let max_x = maximum $ map fst dots2
let min_y = minimum $ map snd dots2
let max_y = maximum $ map snd dots2
mapM_ (\y -> do
mapM_ (\x -> putChar (if (x,y) `elem` dots2 then '#' else '.')) [0..max_x]
putStrLn ""
) [0..max_y]
-- print dots2
-- print $ length dots2