From a95e9747540823e841a56466045db811cb732925 Mon Sep 17 00:00:00 2001 From: Gattix Date: Tue, 6 Dec 2022 01:37:04 +0100 Subject: [PATCH] day 5 --- 2022/day05/day05.hs | 31 +++ 2022/day05/input | 514 +++++++++++++++++++++++++++++++++++++++++++ 2022/day05/testinput | 9 + 3 files changed, 554 insertions(+) create mode 100644 2022/day05/day05.hs create mode 100644 2022/day05/input create mode 100644 2022/day05/testinput diff --git a/2022/day05/day05.hs b/2022/day05/day05.hs new file mode 100644 index 0000000..0d18b2f --- /dev/null +++ b/2022/day05/day05.hs @@ -0,0 +1,31 @@ +import Data.List.Split +import Data.List + +main :: IO () +main = do + input <- map lines <$> splitOn "\n\n" <$> readFile "input" + let field = parseField $ head input + let moves = parseMoves $ last input + print $ map last $ doMoves reverse moves field + print $ map last $ doMoves id moves field + +parseField :: [String] -> [String] +parseField = filter (not.null) . map (takeWhile (/=' ') . reverse) . filter (null . intersect "[]") . transpose . init + +parseMoves :: [String] -> [[Int]] +parseMoves = map (map (subtract 1 . read) . filter (not.null . intersect ['0'..'9']) . words) + +move :: (String -> String) -> [Int] -> [String] -> [String] +move f (p:a:b:[]) field = newFieldA ++ [new] ++ newFieldB + where line = field !! a + (rmn,splt) = splitAt (length line - 1 - p) line + new = field !! b ++ (f splt) + (fieldA,_) = splitAt a field + (_,fieldB) = splitAt (a+1) field + removedField = fieldA ++ [rmn] ++ fieldB + (newFieldA,_) = splitAt b removedField + (_,newFieldB) = splitAt (b+1) removedField + +doMoves :: (String -> String) -> [[Int]] -> [String] -> [String] +doMoves f [] field = field +doMoves f (m:ms) field = doMoves f ms (move f m field) diff --git a/2022/day05/input b/2022/day05/input new file mode 100644 index 0000000..198d0b7 --- /dev/null +++ b/2022/day05/input @@ -0,0 +1,514 @@ +[N] [C] [Q] +[W] [J] [L] [J] [V] +[F] [N] [D] [L] [S] [W] +[R] [S] [F] [G] [R] [V] [Z] +[Z] [G] [Q] [C] [W] [C] [F] [G] +[S] [Q] [V] [P] [S] [F] [D] [R] [S] +[M] [P] [R] [Z] [P] [D] [N] [N] [M] +[D] [W] [W] [F] [T] [H] [Z] [W] [R] + 1 2 3 4 5 6 7 8 9 + +move 1 from 3 to 9 +move 3 from 5 to 3 +move 4 from 2 to 5 +move 4 from 1 to 2 +move 3 from 5 to 7 +move 3 from 1 to 2 +move 4 from 8 to 7 +move 4 from 9 to 7 +move 4 from 2 to 7 +move 2 from 3 to 6 +move 3 from 6 to 2 +move 5 from 4 to 7 +move 7 from 3 to 7 +move 5 from 6 to 9 +move 2 from 4 to 8 +move 1 from 3 to 2 +move 4 from 2 to 7 +move 2 from 2 to 8 +move 8 from 8 to 5 +move 1 from 2 to 4 +move 1 from 2 to 9 +move 7 from 5 to 4 +move 31 from 7 to 1 +move 9 from 9 to 3 +move 12 from 1 to 9 +move 15 from 1 to 7 +move 4 from 3 to 8 +move 2 from 5 to 1 +move 12 from 7 to 5 +move 2 from 8 to 2 +move 12 from 5 to 4 +move 1 from 3 to 5 +move 6 from 1 to 3 +move 1 from 1 to 5 +move 1 from 8 to 7 +move 1 from 8 to 5 +move 7 from 7 to 8 +move 5 from 8 to 2 +move 11 from 4 to 2 +move 10 from 3 to 1 +move 1 from 7 to 5 +move 10 from 1 to 3 +move 5 from 4 to 2 +move 1 from 4 to 6 +move 7 from 2 to 3 +move 9 from 9 to 5 +move 15 from 2 to 3 +move 1 from 9 to 1 +move 7 from 5 to 3 +move 1 from 2 to 4 +move 2 from 9 to 2 +move 1 from 8 to 9 +move 5 from 5 to 3 +move 1 from 8 to 7 +move 1 from 2 to 4 +move 1 from 7 to 6 +move 1 from 1 to 6 +move 1 from 6 to 9 +move 1 from 5 to 4 +move 1 from 6 to 4 +move 1 from 6 to 8 +move 2 from 9 to 4 +move 12 from 3 to 1 +move 8 from 4 to 8 +move 1 from 9 to 8 +move 10 from 8 to 6 +move 1 from 6 to 7 +move 6 from 6 to 9 +move 1 from 2 to 7 +move 1 from 4 to 7 +move 2 from 7 to 3 +move 1 from 1 to 3 +move 6 from 9 to 1 +move 2 from 6 to 7 +move 12 from 1 to 3 +move 5 from 1 to 9 +move 1 from 7 to 3 +move 38 from 3 to 7 +move 19 from 7 to 8 +move 19 from 8 to 2 +move 1 from 9 to 6 +move 5 from 3 to 7 +move 2 from 6 to 7 +move 1 from 3 to 9 +move 2 from 3 to 6 +move 4 from 2 to 6 +move 6 from 2 to 4 +move 14 from 7 to 9 +move 8 from 2 to 5 +move 19 from 9 to 3 +move 6 from 4 to 1 +move 6 from 1 to 4 +move 4 from 4 to 3 +move 10 from 7 to 6 +move 1 from 6 to 4 +move 22 from 3 to 1 +move 5 from 1 to 6 +move 5 from 5 to 8 +move 1 from 7 to 4 +move 1 from 2 to 3 +move 15 from 6 to 9 +move 3 from 8 to 4 +move 2 from 3 to 1 +move 6 from 9 to 1 +move 1 from 3 to 9 +move 1 from 3 to 1 +move 1 from 5 to 9 +move 1 from 7 to 1 +move 1 from 8 to 2 +move 6 from 9 to 2 +move 2 from 9 to 1 +move 3 from 6 to 3 +move 2 from 9 to 5 +move 1 from 6 to 7 +move 2 from 2 to 7 +move 3 from 3 to 5 +move 1 from 8 to 9 +move 7 from 4 to 7 +move 1 from 6 to 3 +move 2 from 9 to 5 +move 10 from 1 to 5 +move 19 from 1 to 8 +move 9 from 7 to 1 +move 1 from 3 to 5 +move 2 from 2 to 4 +move 2 from 2 to 6 +move 2 from 6 to 4 +move 7 from 1 to 7 +move 3 from 7 to 3 +move 2 from 4 to 1 +move 3 from 3 to 4 +move 1 from 2 to 4 +move 2 from 4 to 1 +move 2 from 4 to 8 +move 20 from 8 to 2 +move 1 from 8 to 3 +move 4 from 7 to 8 +move 14 from 2 to 6 +move 3 from 1 to 2 +move 2 from 1 to 7 +move 1 from 4 to 6 +move 1 from 1 to 5 +move 4 from 2 to 8 +move 3 from 7 to 6 +move 1 from 4 to 6 +move 2 from 7 to 9 +move 1 from 2 to 6 +move 1 from 3 to 1 +move 3 from 5 to 8 +move 1 from 1 to 4 +move 2 from 9 to 5 +move 4 from 6 to 7 +move 1 from 4 to 1 +move 1 from 8 to 5 +move 1 from 7 to 6 +move 1 from 2 to 9 +move 2 from 7 to 1 +move 1 from 1 to 3 +move 1 from 7 to 2 +move 4 from 2 to 7 +move 1 from 1 to 3 +move 2 from 3 to 2 +move 9 from 8 to 3 +move 1 from 8 to 6 +move 2 from 7 to 3 +move 1 from 7 to 4 +move 1 from 9 to 7 +move 1 from 7 to 2 +move 2 from 2 to 8 +move 6 from 5 to 2 +move 5 from 3 to 7 +move 1 from 4 to 7 +move 3 from 7 to 1 +move 11 from 5 to 8 +move 2 from 1 to 6 +move 2 from 1 to 8 +move 2 from 5 to 9 +move 1 from 7 to 2 +move 2 from 5 to 4 +move 17 from 6 to 7 +move 1 from 4 to 1 +move 1 from 1 to 7 +move 1 from 6 to 5 +move 1 from 6 to 2 +move 9 from 2 to 5 +move 1 from 6 to 7 +move 9 from 7 to 4 +move 3 from 7 to 8 +move 3 from 3 to 4 +move 8 from 7 to 9 +move 11 from 8 to 1 +move 1 from 4 to 3 +move 1 from 7 to 4 +move 9 from 9 to 4 +move 5 from 1 to 7 +move 8 from 5 to 1 +move 3 from 3 to 4 +move 6 from 7 to 9 +move 3 from 8 to 5 +move 1 from 3 to 8 +move 1 from 5 to 8 +move 2 from 9 to 1 +move 3 from 9 to 7 +move 2 from 7 to 9 +move 3 from 9 to 8 +move 1 from 7 to 3 +move 1 from 3 to 9 +move 7 from 4 to 3 +move 18 from 4 to 2 +move 8 from 1 to 6 +move 1 from 6 to 7 +move 2 from 3 to 1 +move 14 from 2 to 6 +move 5 from 1 to 6 +move 5 from 3 to 2 +move 2 from 9 to 5 +move 3 from 1 to 8 +move 1 from 7 to 9 +move 3 from 5 to 1 +move 4 from 8 to 4 +move 1 from 2 to 7 +move 6 from 2 to 5 +move 2 from 1 to 6 +move 14 from 6 to 1 +move 2 from 4 to 7 +move 2 from 4 to 6 +move 12 from 1 to 6 +move 8 from 8 to 3 +move 11 from 6 to 1 +move 1 from 1 to 6 +move 15 from 6 to 9 +move 3 from 7 to 3 +move 11 from 1 to 4 +move 3 from 5 to 3 +move 10 from 9 to 5 +move 2 from 6 to 9 +move 2 from 2 to 5 +move 6 from 3 to 7 +move 7 from 9 to 3 +move 2 from 1 to 8 +move 1 from 9 to 6 +move 12 from 3 to 4 +move 13 from 5 to 6 +move 2 from 7 to 4 +move 3 from 7 to 5 +move 2 from 8 to 4 +move 15 from 6 to 5 +move 22 from 4 to 5 +move 2 from 3 to 6 +move 1 from 7 to 8 +move 2 from 1 to 2 +move 13 from 5 to 3 +move 1 from 8 to 6 +move 1 from 6 to 4 +move 1 from 2 to 7 +move 7 from 5 to 2 +move 4 from 4 to 8 +move 1 from 6 to 3 +move 3 from 5 to 6 +move 2 from 8 to 9 +move 4 from 5 to 1 +move 1 from 9 to 8 +move 4 from 2 to 5 +move 1 from 7 to 6 +move 4 from 6 to 3 +move 1 from 6 to 9 +move 1 from 9 to 6 +move 4 from 1 to 6 +move 1 from 9 to 4 +move 4 from 6 to 3 +move 1 from 6 to 4 +move 14 from 5 to 6 +move 23 from 3 to 1 +move 2 from 5 to 6 +move 1 from 4 to 2 +move 6 from 5 to 7 +move 16 from 6 to 5 +move 2 from 2 to 6 +move 2 from 6 to 1 +move 2 from 2 to 4 +move 1 from 2 to 8 +move 15 from 1 to 3 +move 4 from 8 to 2 +move 9 from 1 to 8 +move 12 from 5 to 7 +move 2 from 5 to 1 +move 1 from 4 to 6 +move 1 from 5 to 6 +move 3 from 7 to 3 +move 2 from 8 to 6 +move 1 from 2 to 3 +move 2 from 3 to 5 +move 3 from 1 to 9 +move 12 from 3 to 9 +move 4 from 9 to 7 +move 2 from 9 to 5 +move 4 from 8 to 5 +move 8 from 7 to 2 +move 6 from 5 to 8 +move 2 from 5 to 7 +move 12 from 7 to 1 +move 2 from 6 to 7 +move 11 from 2 to 4 +move 1 from 6 to 5 +move 1 from 5 to 8 +move 10 from 8 to 6 +move 7 from 1 to 9 +move 3 from 3 to 8 +move 2 from 7 to 4 +move 1 from 5 to 3 +move 9 from 4 to 7 +move 16 from 9 to 6 +move 2 from 1 to 6 +move 1 from 7 to 8 +move 2 from 4 to 1 +move 1 from 1 to 5 +move 1 from 5 to 7 +move 2 from 3 to 9 +move 5 from 4 to 6 +move 1 from 3 to 6 +move 1 from 4 to 5 +move 1 from 5 to 8 +move 16 from 6 to 5 +move 2 from 7 to 6 +move 21 from 6 to 2 +move 3 from 8 to 7 +move 1 from 9 to 1 +move 7 from 7 to 1 +move 14 from 2 to 5 +move 1 from 9 to 3 +move 1 from 3 to 1 +move 1 from 8 to 3 +move 2 from 2 to 6 +move 15 from 5 to 1 +move 20 from 1 to 8 +move 1 from 3 to 5 +move 4 from 2 to 8 +move 2 from 1 to 2 +move 2 from 6 to 8 +move 3 from 7 to 6 +move 2 from 6 to 7 +move 1 from 7 to 2 +move 6 from 5 to 6 +move 3 from 5 to 9 +move 2 from 9 to 6 +move 1 from 9 to 4 +move 2 from 2 to 3 +move 1 from 3 to 2 +move 2 from 1 to 4 +move 1 from 3 to 9 +move 2 from 4 to 7 +move 4 from 8 to 4 +move 8 from 8 to 6 +move 5 from 6 to 9 +move 6 from 6 to 7 +move 6 from 6 to 3 +move 5 from 3 to 2 +move 2 from 2 to 3 +move 10 from 7 to 1 +move 2 from 5 to 2 +move 2 from 4 to 1 +move 5 from 5 to 9 +move 2 from 3 to 5 +move 2 from 9 to 4 +move 5 from 4 to 8 +move 8 from 9 to 6 +move 16 from 1 to 9 +move 7 from 2 to 7 +move 10 from 9 to 4 +move 10 from 4 to 8 +move 1 from 7 to 3 +move 1 from 2 to 5 +move 3 from 5 to 7 +move 2 from 3 to 6 +move 5 from 7 to 4 +move 4 from 4 to 5 +move 17 from 8 to 3 +move 9 from 6 to 2 +move 17 from 3 to 9 +move 9 from 8 to 3 +move 2 from 5 to 6 +move 1 from 5 to 8 +move 5 from 2 to 4 +move 1 from 6 to 9 +move 3 from 9 to 5 +move 3 from 7 to 4 +move 13 from 9 to 3 +move 3 from 9 to 2 +move 1 from 9 to 8 +move 2 from 6 to 4 +move 9 from 3 to 4 +move 3 from 9 to 3 +move 1 from 8 to 1 +move 2 from 5 to 2 +move 5 from 4 to 7 +move 1 from 9 to 2 +move 6 from 7 to 2 +move 1 from 9 to 6 +move 9 from 2 to 4 +move 1 from 1 to 7 +move 1 from 6 to 5 +move 1 from 7 to 4 +move 4 from 4 to 2 +move 12 from 3 to 6 +move 7 from 2 to 5 +move 1 from 2 to 1 +move 1 from 1 to 9 +move 2 from 2 to 6 +move 5 from 8 to 2 +move 8 from 6 to 3 +move 1 from 9 to 3 +move 4 from 2 to 7 +move 1 from 3 to 2 +move 2 from 2 to 7 +move 1 from 2 to 5 +move 3 from 6 to 3 +move 10 from 5 to 8 +move 1 from 5 to 3 +move 1 from 6 to 5 +move 5 from 8 to 7 +move 1 from 5 to 8 +move 2 from 6 to 3 +move 5 from 7 to 4 +move 3 from 3 to 6 +move 2 from 8 to 6 +move 3 from 8 to 2 +move 1 from 3 to 7 +move 15 from 4 to 5 +move 10 from 4 to 1 +move 7 from 3 to 5 +move 1 from 2 to 9 +move 5 from 5 to 7 +move 8 from 5 to 9 +move 4 from 3 to 6 +move 3 from 9 to 6 +move 3 from 1 to 4 +move 10 from 7 to 4 +move 2 from 2 to 4 +move 2 from 3 to 5 +move 1 from 7 to 2 +move 1 from 7 to 6 +move 6 from 6 to 5 +move 7 from 5 to 3 +move 1 from 8 to 3 +move 5 from 1 to 6 +move 9 from 5 to 3 +move 14 from 4 to 7 +move 1 from 2 to 8 +move 1 from 8 to 2 +move 1 from 6 to 4 +move 2 from 4 to 7 +move 1 from 2 to 9 +move 1 from 4 to 8 +move 2 from 1 to 4 +move 8 from 6 to 1 +move 1 from 4 to 3 +move 1 from 5 to 8 +move 12 from 7 to 3 +move 1 from 4 to 8 +move 7 from 9 to 1 +move 3 from 6 to 2 +move 3 from 8 to 7 +move 1 from 2 to 9 +move 4 from 7 to 1 +move 6 from 1 to 7 +move 2 from 2 to 8 +move 7 from 7 to 3 +move 10 from 1 to 6 +move 20 from 3 to 1 +move 2 from 6 to 7 +move 1 from 9 to 1 +move 8 from 6 to 8 +move 6 from 8 to 9 +move 5 from 3 to 7 +move 2 from 7 to 3 +move 2 from 9 to 2 +move 5 from 1 to 3 +move 2 from 9 to 8 +move 8 from 3 to 7 +move 6 from 8 to 3 +move 1 from 9 to 8 +move 19 from 1 to 6 +move 17 from 3 to 6 +move 2 from 2 to 4 +move 1 from 3 to 5 +move 1 from 4 to 1 +move 1 from 4 to 8 +move 2 from 8 to 5 +move 1 from 5 to 1 +move 1 from 5 to 4 +move 1 from 5 to 7 +move 2 from 1 to 3 +move 15 from 7 to 4 +move 1 from 9 to 7 +move 2 from 7 to 6 +move 21 from 6 to 4 +move 17 from 6 to 8 +move 2 from 3 to 5 +move 29 from 4 to 9 +move 15 from 9 to 7 +move 1 from 5 to 1 +move 9 from 8 to 2 +move 10 from 9 to 3 +move 8 from 2 to 6 diff --git a/2022/day05/testinput b/2022/day05/testinput new file mode 100644 index 0000000..84933bb --- /dev/null +++ b/2022/day05/testinput @@ -0,0 +1,9 @@ + [D] +[N] [C] +[Z] [M] [P] + 1 2 3 + +move 1 from 2 to 1 +move 3 from 1 to 3 +move 2 from 2 to 1 +move 1 from 1 to 2