This commit is contained in:
Agatha Lovelace 2021-12-13 19:08:27 +02:00
parent d15e8ec49c
commit 018bae2654
2 changed files with 76 additions and 0 deletions

29
Day13.hs Normal file
View File

@ -0,0 +1,29 @@
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 ]

47
Day13_1.hs Normal file
View File

@ -0,0 +1,47 @@
module Day13_1 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
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 = nub $ foldl (\acc x -> map (fold x) acc) coords folds
let width = maximum $ fst <$> result
height = maximum $ snd <$> result
field = (,) <$> [0..width] <*> [0..height]
let display x
| x `elem` result = '#'
| otherwise = '.'
-- the text is actually vertical, because of course it is
in putStrLn $ insert (height+1) '\n' $ map display field