Day 5 Part 1

This commit is contained in:
Jens Kadenbach
2022-12-05 15:49:32 +01:00
parent 1267be4d00
commit 5af46b73dd
7 changed files with 708 additions and 2 deletions

View File

@@ -39,6 +39,8 @@ library
Day4.Part1 Day4.Part1
Day4.Part2 Day4.Part2
Day4.Shared Day4.Shared
Day5
Day5.Part1
Lib Lib
other-modules: other-modules:
Paths_aoc2022 Paths_aoc2022
@@ -55,6 +57,7 @@ library
, lens , lens
, split , split
, text , text
, these
default-language: Haskell2010 default-language: Haskell2010
executable aoc2022-exe executable aoc2022-exe
@@ -75,6 +78,7 @@ executable aoc2022-exe
, lens , lens
, split , split
, text , text
, these
default-language: Haskell2010 default-language: Haskell2010
test-suite aoc2022-test test-suite aoc2022-test
@@ -85,6 +89,7 @@ test-suite aoc2022-test
Day2Spec Day2Spec
Day3Spec Day3Spec
Day4Spec Day4Spec
Day5Spec
Paths_aoc2022 Paths_aoc2022
hs-source-dirs: hs-source-dirs:
test test
@@ -100,4 +105,5 @@ test-suite aoc2022-test
, lens , lens
, split , split
, text , text
, these
default-language: Haskell2010 default-language: Haskell2010

View File

@@ -30,6 +30,7 @@ dependencies:
- split - split
- lens - lens
- Ranged-sets - Ranged-sets
- these
ghc-options: ghc-options:
- -Wall - -Wall

511
ressources/day05-input Normal file
View File

@@ -0,0 +1,511 @@
[J] [B] [T]
[M] [L] [Q] [L] [R]
[G] [Q] [W] [S] [B] [L]
[D] [D] [T] [M] [G] [V] [P]
[T] [N] [N] [N] [D] [J] [G] [N]
[W] [H] [H] [S] [C] [N] [R] [W] [D]
[N] [P] [P] [W] [H] [H] [B] [N] [G]
[L] [C] [W] [C] [P] [T] [M] [Z] [W]
1 2 3 4 5 6 7 8 9
move 6 from 6 to 5
move 2 from 5 to 9
move 8 from 9 to 1
move 3 from 5 to 4
move 9 from 1 to 8
move 2 from 1 to 5
move 1 from 1 to 8
move 14 from 8 to 2
move 1 from 1 to 2
move 2 from 6 to 8
move 2 from 5 to 7
move 6 from 8 to 6
move 4 from 4 to 2
move 2 from 4 to 9
move 5 from 7 to 4
move 2 from 7 to 5
move 6 from 2 to 4
move 2 from 4 to 7
move 4 from 5 to 8
move 1 from 5 to 2
move 3 from 3 to 5
move 3 from 8 to 3
move 4 from 3 to 7
move 2 from 9 to 8
move 1 from 3 to 7
move 1 from 6 to 8
move 5 from 7 to 1
move 3 from 7 to 2
move 1 from 6 to 3
move 2 from 5 to 9
move 5 from 4 to 2
move 3 from 5 to 9
move 5 from 9 to 6
move 2 from 1 to 3
move 4 from 4 to 1
move 2 from 8 to 1
move 18 from 2 to 5
move 3 from 4 to 1
move 1 from 1 to 2
move 1 from 6 to 8
move 1 from 7 to 1
move 10 from 1 to 5
move 1 from 1 to 5
move 3 from 8 to 1
move 2 from 1 to 5
move 3 from 6 to 5
move 8 from 2 to 9
move 2 from 9 to 7
move 3 from 3 to 8
move 1 from 4 to 8
move 3 from 5 to 3
move 15 from 5 to 8
move 4 from 6 to 1
move 2 from 7 to 4
move 9 from 5 to 7
move 1 from 6 to 8
move 5 from 3 to 5
move 5 from 7 to 5
move 3 from 1 to 5
move 2 from 4 to 8
move 3 from 1 to 6
move 20 from 5 to 4
move 1 from 7 to 6
move 21 from 8 to 2
move 1 from 3 to 7
move 2 from 4 to 2
move 1 from 7 to 1
move 18 from 2 to 8
move 3 from 9 to 2
move 1 from 6 to 4
move 1 from 1 to 9
move 8 from 8 to 6
move 4 from 8 to 2
move 1 from 2 to 6
move 7 from 8 to 5
move 2 from 5 to 3
move 1 from 9 to 5
move 5 from 2 to 4
move 1 from 3 to 7
move 2 from 5 to 7
move 4 from 4 to 9
move 2 from 5 to 9
move 6 from 2 to 8
move 3 from 7 to 3
move 2 from 5 to 4
move 4 from 8 to 2
move 2 from 7 to 4
move 7 from 6 to 4
move 1 from 8 to 4
move 3 from 6 to 7
move 2 from 7 to 2
move 7 from 9 to 7
move 1 from 9 to 2
move 3 from 3 to 6
move 3 from 7 to 4
move 2 from 7 to 9
move 6 from 4 to 1
move 3 from 7 to 9
move 1 from 8 to 5
move 1 from 3 to 6
move 3 from 9 to 4
move 2 from 6 to 4
move 3 from 9 to 1
move 4 from 2 to 8
move 1 from 8 to 5
move 9 from 1 to 2
move 1 from 6 to 5
move 1 from 7 to 2
move 1 from 8 to 1
move 2 from 8 to 9
move 1 from 9 to 8
move 1 from 5 to 7
move 1 from 7 to 6
move 1 from 9 to 8
move 1 from 6 to 3
move 26 from 4 to 3
move 1 from 5 to 8
move 3 from 6 to 3
move 7 from 4 to 3
move 1 from 1 to 3
move 1 from 4 to 8
move 13 from 3 to 1
move 1 from 3 to 4
move 12 from 2 to 5
move 20 from 3 to 2
move 1 from 4 to 1
move 4 from 5 to 7
move 1 from 7 to 8
move 9 from 5 to 2
move 5 from 1 to 5
move 21 from 2 to 8
move 5 from 8 to 4
move 4 from 5 to 2
move 6 from 1 to 7
move 1 from 5 to 4
move 4 from 3 to 1
move 6 from 1 to 3
move 1 from 1 to 9
move 6 from 8 to 7
move 4 from 8 to 2
move 4 from 2 to 7
move 5 from 4 to 1
move 8 from 8 to 4
move 1 from 9 to 6
move 18 from 7 to 6
move 15 from 6 to 5
move 2 from 6 to 8
move 2 from 6 to 3
move 8 from 3 to 7
move 15 from 5 to 7
move 3 from 4 to 9
move 12 from 2 to 3
move 3 from 9 to 4
move 6 from 7 to 9
move 9 from 4 to 5
move 10 from 3 to 5
move 9 from 5 to 2
move 14 from 7 to 8
move 14 from 8 to 5
move 4 from 2 to 4
move 1 from 4 to 6
move 2 from 8 to 4
move 3 from 8 to 9
move 18 from 5 to 1
move 1 from 5 to 9
move 1 from 7 to 4
move 5 from 5 to 9
move 3 from 2 to 4
move 13 from 9 to 2
move 13 from 2 to 6
move 1 from 7 to 3
move 3 from 3 to 1
move 9 from 6 to 5
move 1 from 7 to 8
move 20 from 1 to 8
move 2 from 2 to 8
move 5 from 6 to 9
move 15 from 8 to 7
move 3 from 5 to 3
move 5 from 1 to 3
move 2 from 3 to 4
move 3 from 9 to 5
move 4 from 5 to 2
move 4 from 5 to 7
move 3 from 4 to 9
move 10 from 7 to 8
move 2 from 9 to 4
move 1 from 5 to 6
move 8 from 7 to 9
move 1 from 6 to 7
move 6 from 3 to 4
move 12 from 9 to 8
move 1 from 1 to 5
move 2 from 7 to 8
move 1 from 7 to 5
move 1 from 9 to 5
move 2 from 2 to 9
move 11 from 8 to 1
move 7 from 1 to 5
move 3 from 1 to 6
move 5 from 8 to 9
move 8 from 4 to 3
move 4 from 4 to 6
move 5 from 9 to 3
move 4 from 4 to 5
move 2 from 6 to 7
move 1 from 9 to 5
move 2 from 7 to 4
move 12 from 5 to 2
move 8 from 8 to 9
move 8 from 8 to 6
move 9 from 6 to 2
move 4 from 9 to 2
move 1 from 5 to 1
move 5 from 2 to 1
move 2 from 5 to 4
move 5 from 2 to 5
move 5 from 5 to 6
move 3 from 4 to 7
move 11 from 2 to 7
move 2 from 2 to 1
move 4 from 3 to 7
move 2 from 2 to 4
move 6 from 1 to 4
move 1 from 2 to 8
move 2 from 9 to 5
move 4 from 4 to 3
move 5 from 4 to 1
move 2 from 2 to 1
move 1 from 8 to 5
move 14 from 7 to 6
move 3 from 9 to 2
move 15 from 6 to 8
move 4 from 1 to 3
move 2 from 2 to 3
move 1 from 1 to 7
move 2 from 3 to 5
move 4 from 5 to 4
move 1 from 3 to 5
move 5 from 1 to 6
move 12 from 6 to 7
move 7 from 8 to 4
move 12 from 7 to 9
move 4 from 7 to 9
move 1 from 2 to 8
move 12 from 9 to 4
move 23 from 4 to 3
move 1 from 6 to 5
move 3 from 9 to 3
move 1 from 7 to 9
move 1 from 9 to 1
move 1 from 9 to 7
move 42 from 3 to 1
move 3 from 5 to 4
move 5 from 1 to 3
move 3 from 4 to 7
move 1 from 1 to 9
move 4 from 3 to 8
move 1 from 3 to 7
move 1 from 9 to 1
move 2 from 7 to 8
move 8 from 1 to 6
move 2 from 7 to 5
move 9 from 1 to 2
move 5 from 2 to 3
move 3 from 2 to 4
move 20 from 1 to 2
move 1 from 1 to 5
move 1 from 6 to 7
move 3 from 4 to 7
move 2 from 3 to 6
move 3 from 6 to 1
move 1 from 6 to 4
move 2 from 1 to 6
move 3 from 5 to 9
move 1 from 4 to 3
move 2 from 7 to 4
move 6 from 8 to 4
move 1 from 1 to 9
move 1 from 2 to 9
move 2 from 8 to 7
move 3 from 6 to 2
move 5 from 7 to 5
move 4 from 2 to 5
move 4 from 4 to 6
move 3 from 9 to 6
move 4 from 3 to 1
move 1 from 9 to 2
move 7 from 8 to 9
move 4 from 2 to 4
move 2 from 1 to 7
move 3 from 4 to 5
move 4 from 2 to 4
move 1 from 7 to 4
move 4 from 2 to 9
move 7 from 4 to 3
move 1 from 7 to 3
move 6 from 2 to 3
move 2 from 1 to 5
move 10 from 3 to 6
move 2 from 6 to 1
move 2 from 2 to 7
move 2 from 3 to 1
move 1 from 7 to 8
move 11 from 5 to 3
move 2 from 3 to 1
move 4 from 6 to 1
move 1 from 4 to 6
move 8 from 3 to 4
move 2 from 5 to 6
move 3 from 3 to 5
move 1 from 8 to 4
move 1 from 4 to 9
move 2 from 6 to 1
move 1 from 5 to 1
move 9 from 4 to 3
move 5 from 6 to 9
move 5 from 6 to 7
move 13 from 9 to 3
move 5 from 1 to 8
move 4 from 8 to 4
move 10 from 3 to 2
move 3 from 6 to 1
move 2 from 7 to 9
move 1 from 8 to 3
move 1 from 7 to 3
move 1 from 9 to 5
move 1 from 6 to 3
move 7 from 2 to 4
move 3 from 5 to 2
move 8 from 3 to 5
move 7 from 4 to 3
move 5 from 9 to 7
move 1 from 7 to 1
move 9 from 1 to 8
move 9 from 5 to 8
move 2 from 7 to 8
move 3 from 8 to 1
move 10 from 3 to 6
move 1 from 1 to 6
move 5 from 1 to 7
move 3 from 2 to 8
move 7 from 8 to 6
move 7 from 8 to 6
move 1 from 3 to 5
move 5 from 7 to 9
move 4 from 8 to 4
move 3 from 2 to 8
move 1 from 7 to 8
move 3 from 3 to 9
move 3 from 7 to 4
move 1 from 7 to 2
move 9 from 9 to 1
move 5 from 1 to 9
move 4 from 8 to 6
move 1 from 2 to 7
move 1 from 5 to 3
move 1 from 3 to 7
move 1 from 1 to 9
move 1 from 1 to 7
move 5 from 9 to 6
move 2 from 7 to 6
move 10 from 4 to 8
move 1 from 4 to 2
move 1 from 4 to 1
move 1 from 9 to 2
move 3 from 1 to 2
move 1 from 7 to 3
move 1 from 2 to 1
move 16 from 6 to 3
move 9 from 6 to 1
move 6 from 6 to 1
move 5 from 6 to 1
move 3 from 8 to 1
move 11 from 3 to 4
move 1 from 6 to 2
move 3 from 8 to 2
move 4 from 1 to 6
move 5 from 3 to 2
move 1 from 2 to 5
move 1 from 8 to 5
move 5 from 8 to 3
move 4 from 6 to 9
move 2 from 9 to 6
move 3 from 3 to 9
move 1 from 5 to 7
move 5 from 1 to 6
move 3 from 6 to 4
move 2 from 2 to 9
move 8 from 4 to 2
move 9 from 1 to 7
move 3 from 3 to 5
move 3 from 5 to 7
move 12 from 7 to 1
move 5 from 4 to 6
move 1 from 4 to 5
move 7 from 1 to 8
move 5 from 9 to 3
move 1 from 7 to 4
move 10 from 1 to 8
move 1 from 4 to 8
move 4 from 6 to 8
move 1 from 6 to 9
move 2 from 5 to 1
move 4 from 3 to 4
move 1 from 1 to 8
move 4 from 4 to 7
move 2 from 1 to 8
move 4 from 6 to 1
move 3 from 9 to 5
move 1 from 6 to 5
move 1 from 3 to 7
move 24 from 8 to 6
move 3 from 6 to 5
move 4 from 6 to 7
move 1 from 1 to 7
move 7 from 7 to 6
move 7 from 5 to 3
move 13 from 6 to 8
move 3 from 1 to 2
move 7 from 6 to 3
move 12 from 2 to 4
move 4 from 6 to 9
move 6 from 3 to 1
move 1 from 2 to 4
move 2 from 8 to 7
move 2 from 2 to 9
move 6 from 3 to 4
move 12 from 8 to 2
move 18 from 2 to 5
move 10 from 4 to 3
move 4 from 7 to 3
move 5 from 4 to 7
move 3 from 5 to 2
move 4 from 7 to 9
move 1 from 5 to 4
move 3 from 2 to 1
move 4 from 3 to 6
move 7 from 5 to 6
move 2 from 5 to 7
move 5 from 1 to 7
move 9 from 7 to 6
move 8 from 9 to 8
move 1 from 1 to 3
move 1 from 3 to 1
move 10 from 3 to 9
move 8 from 8 to 4
move 1 from 3 to 8
move 1 from 1 to 3
move 6 from 9 to 1
move 5 from 5 to 3
move 5 from 3 to 6
move 1 from 8 to 9
move 19 from 6 to 2
move 13 from 4 to 1
move 4 from 1 to 5
move 6 from 2 to 1
move 2 from 9 to 4
move 1 from 3 to 1
move 9 from 2 to 3
move 4 from 5 to 1
move 5 from 9 to 6
move 4 from 3 to 4
move 3 from 2 to 7
move 2 from 4 to 8
move 6 from 1 to 9
move 1 from 8 to 6
move 4 from 1 to 5
move 3 from 4 to 5
move 1 from 7 to 2
move 11 from 1 to 6
move 1 from 2 to 7
move 5 from 3 to 7
move 1 from 3 to 4
move 1 from 4 to 8
move 3 from 5 to 6
move 8 from 1 to 7
move 1 from 8 to 9
move 1 from 6 to 9
move 1 from 8 to 5
move 11 from 6 to 5
move 12 from 5 to 2
move 1 from 5 to 2
move 8 from 7 to 3
move 1 from 5 to 6
move 2 from 5 to 6
move 3 from 7 to 1
move 6 from 2 to 6
move 1 from 3 to 1
move 1 from 4 to 1
move 4 from 6 to 2
move 5 from 1 to 5
move 10 from 2 to 3
move 2 from 9 to 4
move 4 from 5 to 8
move 2 from 2 to 7
move 12 from 6 to 7
move 1 from 8 to 2
move 10 from 3 to 4
move 2 from 3 to 5
move 1 from 3 to 1

11
src/Day5.hs Normal file
View File

@@ -0,0 +1,11 @@
module Day5 (day5) where
import Day5.Part1
day5 :: IO ()
day5 = do
input <- readFile "ressources/day05-input"
putStrLn "Day5"
let message = day5_1 input
putStrLn ("Message from crates: " ++ message)

102
src/Day5/Part1.hs Normal file
View File

@@ -0,0 +1,102 @@
module Day5.Part1 (
StackIndex (..),
Operation (..),
Stack (..),
parseOperation,
parseStacks,
parseRow,
combineRows,
parseInput,
executeOperation,
zipWithPadding,
findTopOfStacks,
day5_1
) where
import qualified Data.Sequence as S
import Data.Sequence (Seq (..))
import Data.Foldable (toList)
newtype StackIndex = StackIndex Int deriving (Show, Eq, Ord)
asInt :: StackIndex -> Int
asInt (StackIndex i) = i - 1
data Operation = Operation
{ count :: Int
, from :: StackIndex
, to :: StackIndex} deriving (Show, Eq)
type Content = Char
newtype Stack = Stack [Content] deriving (Show, Eq)
content :: Stack -> [Content]
content (Stack c) = c
instance Semigroup Stack where
(Stack x) <> (Stack y) = Stack (x ++ y)
instance Monoid Stack where
mempty = Stack []
type Row = S.Seq Stack
parseOperation :: String -> Operation
parseOperation line = Operation {
count = read $ parts !! 1,
from = StackIndex (read $ parts !! 3),
to = StackIndex (read $ parts !! 5)
}
where
parts = words line
parseStacks :: [String] -> Row
parseStacks input = foldl combineRows S.empty rows
where
rows = map parseRow input
parseRow :: String -> Row
parseRow ('[':c:']':' ':cs) = Stack [c] :<| parseRow cs
parseRow (' ':' ':' ':' ':cs) = Stack [] :<| parseRow cs
parseRow ['[', c, ']'] = S.singleton $ Stack [c]
parseRow [' ', ' ', ' '] = S.singleton $ Stack []
parseRow (' ':'1':_) = S.empty -- skip last line
parseRow cs = error ("parseRow: unexpected Row \"" ++ cs ++ "\"")
combineRows :: Row -> Row -> Row
combineRows S.Empty row = row
combineRows row S.Empty = row
combineRows up down = S.fromList [x <> y | (x,y) <- zipWithPadding up down]
zipWithPadding :: (Monoid a, Monoid b) => S.Seq a -> S.Seq b -> [(a,b)]
zipWithPadding (x :<| xs) (y :<| ys) = (x,y) : zipWithPadding xs ys
zipWithPadding S.Empty (y :<| ys) = (mempty, y) : zipWithPadding S.empty ys
zipWithPadding (x :<| xs) S.Empty = (x, mempty) : zipWithPadding xs S.empty
zipWithPadding S.Empty S.Empty = []
parseInput :: String -> (Row, [Operation])
parseInput input = (parseStacks stackInput, map parseOperation (drop 1 operationInput))
where
(stackInput, operationInput) = break ("" ==) $ lines input
executeOperation :: Row -> Operation -> Row
executeOperation row op
| count op == 1 = updatedRow
| otherwise = executeOperation updatedRow (Operation { count = count op - 1, from = from op, to = to op })
where
originStack = row `S.index` asInt (from op)
targetStack = row `S.index` asInt (to op)
movedContent = head $ content originStack
updatedOrigin = Stack (drop 1 $ content originStack)
updatedTarget = Stack (movedContent : content targetStack)
updatedRow = S.update (asInt $ to op) updatedTarget $ S.update (asInt $ from op) updatedOrigin row
findTopOfStacks :: Row -> Seq Content
findTopOfStacks row = head . content <$> row
executeAllOperations :: Row -> [Operation] -> Row
executeAllOperations = foldl executeOperation
day5_1 :: String -> String
day5_1 input = toList . findTopOfStacks $ executeAllOperations row operations
where
(row, operations) = parseInput input

View File

@@ -2,8 +2,8 @@ module Lib
( someFunc ( someFunc
) where ) where
import Day4 import Day5
someFunc :: IO () someFunc :: IO ()
someFunc = day4 someFunc = day5

75
test/Day5Spec.hs Normal file
View File

@@ -0,0 +1,75 @@
{-# LANGUAGE QuasiQuotes #-}
module Day5Spec (spec) where
import Test.Hspec
import Text.Heredoc
import qualified Data.Sequence as S
import Day5.Part1
inputPart1 :: String
inputPart1 = [str| [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
|]
inputStacks :: String
inputStacks = [str| [D]
|[N] [C]
|[Z] [M] [P]
| 1 2 3
|]
inputOperations :: String
inputOperations = [str|move 1 from 2 to 1
|move 3 from 1 to 3
|move 2 from 2 to 1
|move 1 from 1 to 2
|]
spec :: Spec
spec =
describe "Day5" $ do
describe "Part1" $ do
it "parses Operations" $ do
let operation = head . lines $ inputOperations
parseOperation operation `shouldBe` Operation { count = 1, from = StackIndex 2, to = StackIndex 1 }
it "parses stacks" $ do
parseStacks (lines inputStacks) `shouldBe` S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
it "parses a single line of stacks" $ do
let row = head . lines $ inputStacks
parseRow row `shouldBe` S.fromList [Stack "", Stack "D"]
it "combines stacks" $ do
(Stack "AB" <> Stack "CD") `shouldBe` Stack "ABCD"
it "combines rows" $ do
S.fromList [Stack "", Stack "A"] `combineRows` S.fromList [Stack "B", Stack "B"] `shouldBe` S.fromList [Stack "B", Stack "AB"]
S.empty `combineRows` S.fromList [Stack "B", Stack "B"] `shouldBe` S.fromList [Stack "B", Stack "B"]
S.fromList [Stack "A", Stack "B"] `combineRows` S.empty `shouldBe` S.fromList [Stack "A", Stack "B"]
S.singleton (Stack "") `combineRows` S.fromList[Stack "B", Stack "B"] `shouldBe` S.fromList [Stack "B", Stack "B"]
S.fromList [Stack "A", Stack "B"] `combineRows` S.singleton (Stack "") `shouldBe` S.fromList [Stack "A", Stack "B"]
it "splits input into stacks and operations" $ do
parseInput inputPart1 `shouldBe`
(S.fromList [Stack "NZ", Stack "DCM", Stack "P"],
[ Operation { count = 1, from = StackIndex 2, to = StackIndex 1},
Operation { count = 3, from = StackIndex 1, to = StackIndex 3},
Operation { count = 2, from = StackIndex 2, to = StackIndex 1},
Operation { count = 1, from = StackIndex 1, to = StackIndex 2}])
it "exeutes a count=1 operation on a row" $ do
let op = Operation { count = 1, from = StackIndex 2, to = StackIndex 3}
let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
executeOperation row op `shouldBe` S.fromList [Stack "NZ", Stack "CM", Stack "DP"]
it "exeutes a count > 1 operation on a row" $ do
let op = Operation { count = 2, from = StackIndex 2, to = StackIndex 3}
let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
executeOperation row op `shouldBe` S.fromList [Stack "NZ", Stack "M", Stack "CDP"]
it "finds the top items on stacks" $ do
let row = S.fromList [Stack "NZ", Stack "DCM", Stack "P"]
findTopOfStacks row `shouldBe` S.fromList "NDP"
it "solves the demo" $ do
day5_1 inputPart1 `shouldBe` "CMZ"