Compare commits
4 Commits
935a8300ca
...
3cf311b1af
| Author | SHA1 | Date | |
|---|---|---|---|
| 3cf311b1af | |||
| 6a1a49bc96 | |||
| 257e929dd2 | |||
| 58a28c8b60 |
72
day5.hs
72
day5.hs
@@ -1,44 +1,44 @@
|
|||||||
import Data.Char (isDigit)
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
import Data.List (transpose)
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
import Data.Maybe (catMaybes)
|
|
||||||
import Text.ParserCombinators.ReadP
|
import Data.List (foldl')
|
||||||
|
import Data.Map (Map)
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import Lib (readFile')
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- lines <$> readFile "day5.in"
|
input <- break (== T.empty) . T.lines <$> readFile' "day5.in"
|
||||||
let (crates, cmds) = break null input
|
let (crates, cmds) = parse input
|
||||||
let cratesList = map catMaybes $ transpose $ map (fst . last . readP_to_S parseCrates) $ init crates
|
|
||||||
let cmdsList = map ((\(n, a, b) -> (n, a - 1, b - 1)) . fst . last . readP_to_S parseCmd) $ tail cmds
|
|
||||||
print cratesList
|
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $ q1 cratesList cmdsList
|
print $ moveCrates T.reverse crates cmds
|
||||||
putStr "Q2: "
|
putStr "Q2: "
|
||||||
print $ q2 cratesList cmdsList
|
print $ moveCrates id crates cmds
|
||||||
|
|
||||||
q1, q2 :: [[Char]] -> [(Int, Int, Int)] -> [Char]
|
moveCrates :: (Text -> Text) -> [Text] -> [(Int, Int, Int)] -> Text
|
||||||
q1 crates cmds = map head $ foldl (\x xs -> moveCrates x xs True) crates cmds
|
moveCrates f crates' cmds = head . T.transpose . M.elems $ foldl' move crates cmds
|
||||||
q2 crates cmds = map head $ foldl (\x xs -> moveCrates x xs False) crates cmds
|
|
||||||
|
|
||||||
-- Computers are fast, really, not optimising right now
|
|
||||||
moveCrates :: [[Char]] -> (Int, Int, Int) -> Bool -> [[Char]]
|
|
||||||
moveCrates crates (n, a, b) rev =
|
|
||||||
let (head, tail) = splitAt n $ crates !! a
|
|
||||||
in replace b (replace a crates tail) ((if rev then reverse head else head) ++ crates !! b)
|
|
||||||
where
|
where
|
||||||
replace :: Int -> [a] -> a -> [a]
|
crates :: Map Int Text
|
||||||
replace i xs x = take i xs ++ [x] ++ drop (i + 1) xs
|
crates = M.fromList $ zip [0 ..] crates'
|
||||||
|
move :: Map Int Text -> (Int, Int, Int) -> Map Int Text
|
||||||
|
move c (n, x, y) =
|
||||||
|
let (h, t) = T.splitAt n $ c M.! x
|
||||||
|
in M.update (Just . (<>) (f h)) y $ M.insert x t c
|
||||||
|
|
||||||
parseCrates :: ReadP [Maybe Char]
|
-- ugly code vs line length <= 90 🤔
|
||||||
parseCrates = sepBy parseRow (char ' ')
|
parse :: ([Text], [Text]) -> ([Text], [(Int, Int, Int)])
|
||||||
where
|
parse (crates, cmds) =
|
||||||
parseRow :: ReadP (Maybe Char)
|
( [ T.filter (/= ' ') x
|
||||||
parseRow = (Just <$> (char '[' *> get <* char ']')) +++ (Nothing <$ string " ")
|
| (i, x) <- zip [0 ..] (T.transpose $ init crates),
|
||||||
|
i `mod` 4 == 1
|
||||||
parseCmd :: ReadP (Int, Int, Int)
|
],
|
||||||
parseCmd =
|
map
|
||||||
(,,) <$> (string "move " *> getInt)
|
( ( \["move", T.unpack -> n, "from", T.unpack -> x, "to", T.unpack -> y] ->
|
||||||
<*> (string " from " *> getInt)
|
(read n, read x - 1, read y - 1)
|
||||||
<*> (string " to " *> getInt)
|
)
|
||||||
where
|
. T.words
|
||||||
getInt :: ReadP Int
|
)
|
||||||
getInt = read <$> many1 (satisfy isDigit)
|
$ tail cmds
|
||||||
|
)
|
||||||
|
|||||||
17
day6.hs
17
day6.hs
@@ -1,16 +1,21 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
import Data.List (nub)
|
import Data.List (nub)
|
||||||
|
import Lib (readFile')
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- readFile "day6.in"
|
input <- readFile' "day6.in"
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $ parse 4 input
|
print $ parse 4 input
|
||||||
putStr "Q2: "
|
putStr "Q2: "
|
||||||
print $ parse 14 input
|
print $ parse 14 input
|
||||||
|
|
||||||
group :: Int -> [Char] -> [[Char]]
|
group :: Int -> Text -> [Text]
|
||||||
group _ [] = []
|
group _ "" = []
|
||||||
group n xs = take n xs : group n (tail xs)
|
group n xs = T.take n xs : group n (T.tail xs)
|
||||||
|
|
||||||
parse :: Int -> String -> Int
|
parse :: Int -> Text -> Int
|
||||||
parse n = (+ n) . length . takeWhile ((< n) . length) . map nub . group n
|
parse n = (+ n) . length . takeWhile ((< n) . length) . map (nub . T.unpack) . group n
|
||||||
|
|||||||
27
day7'.hs
27
day7'.hs
@@ -1,8 +1,15 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
import Data.Either (rights)
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T (head, lines, words)
|
||||||
|
import qualified Data.Text.Read as T (decimal)
|
||||||
import Data.Tree
|
import Data.Tree
|
||||||
|
import Lib (readFile')
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
tree <- snd . flip parse (emptyFs "/") . map words . lines <$> readFile "day7.in"
|
tree <- snd . flip parse (emptyFs "/") . map T.words . T.lines <$> readFile' "day7.in"
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $
|
print $
|
||||||
foldTree
|
foldTree
|
||||||
@@ -22,24 +29,26 @@ main = do
|
|||||||
)
|
)
|
||||||
tree
|
tree
|
||||||
|
|
||||||
type Filesystem = Tree (String, Int)
|
type Filesystem = Tree (Text, Int)
|
||||||
|
|
||||||
tail' :: [a] -> [a]
|
tail' :: [a] -> [a]
|
||||||
tail' = drop 1
|
tail' = drop 1
|
||||||
|
|
||||||
emptyFs :: String -> Filesystem
|
emptyFs :: Text -> Filesystem
|
||||||
emptyFs n = Node (n, 0) []
|
emptyFs n = Node (n, 0) []
|
||||||
|
|
||||||
insertFs :: Filesystem -> String -> Filesystem -> Filesystem
|
insertFs :: Filesystem -> Text -> Filesystem -> Filesystem
|
||||||
insertFs (Node n xs) name fs = Node (fst n, snd n + snd (rootLabel fs)) (fs : xs)
|
insertFs (Node n xs) name fs = Node (fst n, snd n + snd (rootLabel fs)) (fs : xs)
|
||||||
|
|
||||||
|
ls :: [[Text]] -> [Filesystem]
|
||||||
|
ls =
|
||||||
|
map (\[a, b] -> Node (b, fst . head $ rights [T.decimal a]) [])
|
||||||
|
. filter ((/= "dir") . head)
|
||||||
|
|
||||||
totalSize :: [Filesystem] -> Int
|
totalSize :: [Filesystem] -> Int
|
||||||
totalSize = sum . map (snd . rootLabel)
|
totalSize = sum . map (snd . rootLabel)
|
||||||
|
|
||||||
ls :: [[String]] -> [Filesystem]
|
parse :: [[Text]] -> Filesystem -> ([[Text]], Filesystem)
|
||||||
ls = map (\[a, b] -> Node (b, read a) []) . filter ((/= "dir") . head)
|
|
||||||
|
|
||||||
parse :: [[String]] -> Filesystem -> ([[String]], Filesystem)
|
|
||||||
parse input fs
|
parse input fs
|
||||||
| null input || cur == ["$", "cd", ".."] = (tail' input, fs)
|
| null input || cur == ["$", "cd", ".."] = (tail' input, fs)
|
||||||
| cur == ["$", "ls"] =
|
| cur == ["$", "ls"] =
|
||||||
@@ -50,7 +59,7 @@ parse input fs
|
|||||||
| otherwise =
|
| otherwise =
|
||||||
let name = cur !! 2
|
let name = cur !! 2
|
||||||
(nextInput, newFs) = parse (tail' input) (emptyFs name)
|
(nextInput, newFs) = parse (tail' input) (emptyFs name)
|
||||||
replaced = insertFs fs name newFs -- replace empty directory
|
replaced = insertFs fs name newFs
|
||||||
in parse nextInput replaced
|
in parse nextInput replaced
|
||||||
where
|
where
|
||||||
cur = head input
|
cur = head input
|
||||||
|
|||||||
31
day7.hs
31
day7.hs
@@ -1,12 +1,21 @@
|
|||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
|
import Data.Either (rights)
|
||||||
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T (head, lines, words)
|
||||||
|
import qualified Data.Text.Read as T (decimal)
|
||||||
import Data.Tree
|
import Data.Tree
|
||||||
|
import Lib (readFile')
|
||||||
|
|
||||||
-- this solution assumes that empty directories can exist,
|
-- this solution assumes that empty directories can exist,
|
||||||
-- if you want to ignore that, there is a slightly different code in day7'.hs
|
-- if you want to ignore that, there is a slightly different code in day7'.hs
|
||||||
-- needs cleaning and improvement
|
|
||||||
|
-- needs cleaning and improvement, although i like the fact
|
||||||
|
-- that the entire FS can be printed as a tree
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
tree <- snd . flip parse (emptyFs "/") . map words . lines <$> readFile "day7.in"
|
tree <- snd . flip parse (emptyFs "/") . map T.words . T.lines <$> readFile' "day7.in"
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $
|
print $
|
||||||
foldTree
|
foldTree
|
||||||
@@ -26,18 +35,18 @@ main = do
|
|||||||
)
|
)
|
||||||
tree
|
tree
|
||||||
|
|
||||||
type Filesystem = Tree (String, Int)
|
type Filesystem = Tree (Text, Int)
|
||||||
|
|
||||||
tail' :: [a] -> [a]
|
tail' :: [a] -> [a]
|
||||||
tail' = drop 1
|
tail' = drop 1
|
||||||
|
|
||||||
-- wrapper to create an empty tree
|
-- wrapper to create an empty tree
|
||||||
emptyFs :: String -> Filesystem
|
emptyFs :: Text -> Filesystem
|
||||||
emptyFs n = Node (n, 0) []
|
emptyFs n = Node (n, 0) []
|
||||||
|
|
||||||
-- This function is to replace a filesystem by name
|
-- This function is to replace a filesystem by name
|
||||||
-- Used to replace empty directories with parsed directories in code
|
-- Used to replace empty directories with parsed directories in code
|
||||||
replaceFs :: Filesystem -> String -> Filesystem -> Filesystem
|
replaceFs :: Filesystem -> Text -> Filesystem -> Filesystem
|
||||||
replaceFs (Node n xs) name fs =
|
replaceFs (Node n xs) name fs =
|
||||||
let (h, t) = span ((/= name) . fst . rootLabel) xs
|
let (h, t) = span ((/= name) . fst . rootLabel) xs
|
||||||
in Node
|
in Node
|
||||||
@@ -50,11 +59,17 @@ totalSize :: [Filesystem] -> Int
|
|||||||
totalSize = sum . map (snd . rootLabel)
|
totalSize = sum . map (snd . rootLabel)
|
||||||
|
|
||||||
-- All directories/files are parsed here into leaves
|
-- All directories/files are parsed here into leaves
|
||||||
ls :: [[String]] -> [Filesystem]
|
ls :: [[Text]] -> [Filesystem]
|
||||||
ls = map (\[a, b] -> if a == "dir" then Node (b, 0) [] else Node (b, read a) [])
|
ls =
|
||||||
|
map
|
||||||
|
( \[a, b] ->
|
||||||
|
if a == "dir"
|
||||||
|
then Node (b, 0) []
|
||||||
|
else Node (b, fst . head $ rights [T.decimal a]) []
|
||||||
|
)
|
||||||
|
|
||||||
-- main function where stuff happens
|
-- main function where stuff happens
|
||||||
parse :: [[String]] -> Filesystem -> ([[String]], Filesystem)
|
parse :: [[Text]] -> Filesystem -> ([[Text]], Filesystem)
|
||||||
parse input fs
|
parse input fs
|
||||||
| null input || cur == ["$", "cd", ".."] = (tail' input, fs)
|
| null input || cur == ["$", "cd", ".."] = (tail' input, fs)
|
||||||
| cur == ["$", "ls"] =
|
| cur == ["$", "ls"] =
|
||||||
|
|||||||
80
day8.hs
80
day8.hs
@@ -1,52 +1,48 @@
|
|||||||
import Data.Char (digitToInt)
|
import Data.List (scanl, tails, transpose, zip4, zipWith4)
|
||||||
import Data.List (transpose, zip4, zipWith4)
|
import Data.Text (Text)
|
||||||
|
import qualified Data.Text as T (lines, unpack)
|
||||||
|
import Lib (readFile')
|
||||||
|
|
||||||
|
-- new approach was hinted by an anon but I cannot quite get it right
|
||||||
-- a fun approach, not necessarily the fastest (or cleanest (or smartest))
|
-- a fun approach, not necessarily the fastest (or cleanest (or smartest))
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- map (map digitToInt) . lines <$> readFile "day8.in"
|
input <- map T.unpack . T.lines <$> readFile' "day8.in"
|
||||||
let (e, w, n, s) = trees input
|
|
||||||
let obscured = zipWith4 zip4 e w n s
|
|
||||||
putStr "Q1: "
|
putStr "Q1: "
|
||||||
print $
|
print $ q1 input
|
||||||
length $
|
|
||||||
filter
|
|
||||||
(not . (\((w, _), (x, _), (y, _), (z, _)) -> w && y && x && z))
|
|
||||||
$ concat obscured
|
|
||||||
putStr "Q2: "
|
putStr "Q2: "
|
||||||
print $
|
print $ q2 input
|
||||||
maximum $
|
|
||||||
map (\((_, w), (_, x), (_, y), (_, z)) -> w * x * y * z) $
|
|
||||||
concat obscured
|
|
||||||
|
|
||||||
type Forest = [[(Bool, Int)]]
|
layer :: [[Char]] -> (a -> a -> a) -> ([[Char]] -> [[a]]) -> [[a]]
|
||||||
|
layer input f f' =
|
||||||
trees :: [[Int]] -> (Forest, Forest, Forest, Forest)
|
zipWith4 ( zipWith4 (\w x y z -> f w . f x $ f y z) )
|
||||||
trees input =
|
(f' e)
|
||||||
( trees' input,
|
(map reverse $ f' w)
|
||||||
map reverse $ trees' $ map reverse input,
|
(transpose $ f' n)
|
||||||
transpose $ trees' $ transpose input,
|
(reverse . transpose $ f' s)
|
||||||
reverse $ transpose $ trees' $ transpose $ reverse input
|
|
||||||
) -- (east, west, north, south) traversals
|
|
||||||
where
|
where
|
||||||
trees' :: [[Int]] -> Forest
|
(e, w, n, s) =
|
||||||
trees' =
|
( input,
|
||||||
foldr
|
map reverse input,
|
||||||
( \x xs ->
|
transpose input,
|
||||||
let (_, dist) =
|
transpose $ reverse input
|
||||||
foldr
|
) -- (east, west, north, south]
|
||||||
( \(cur, y) ((old, m), ys) ->
|
|
||||||
( if y > m then (cur, y) else (old, m),
|
q1 :: [[Char]] -> Int
|
||||||
( y <= m,
|
q1 input = length . filter not . concat $ layer input (&&) q1'
|
||||||
let s = length $ takeWhile (< y) $ reverse $ take cur x
|
where
|
||||||
in if s == cur then s else s + 1
|
q1' :: [[Char]] -> [[Bool]]
|
||||||
) :
|
q1' = map (\x -> zipWith (<=) x $ scanl max minBound x)
|
||||||
ys
|
|
||||||
)
|
q2 :: [[Char]] -> Int
|
||||||
)
|
q2 input = maximum . concat $ layer input (*) q2'
|
||||||
((-1, -1), [])
|
where
|
||||||
(zip [0 ..] x)
|
q2' :: [[Char]] -> [[Int]]
|
||||||
in dist : xs
|
q2' =
|
||||||
|
map
|
||||||
|
( \a ->
|
||||||
|
[ let l = length (takeWhile (< x) xs) in (+ l) . fromEnum $ length xs /= l
|
||||||
|
| (x : xs) <- tails a
|
||||||
|
]
|
||||||
)
|
)
|
||||||
[]
|
|
||||||
|
|||||||
Reference in New Issue
Block a user