Day 5! This was a though one

This commit is contained in:
Steph 2022-12-05 13:00:51 +01:00
parent 5758ad46d8
commit d30b80d5ca
4 changed files with 577 additions and 2 deletions

View File

@ -29,7 +29,7 @@ executable aoc2022
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
build-depends: base ^>=4.16.3.0, split ^>=0.2.3.5
build-depends: base ^>=4.16.3.0, split ^>=0.2.3.5, regex-tdfa ^>=1.3.2, containers ^>=0.6.6
hs-source-dirs: app
default-language: Haskell2010
other-modules: Day1.Main Day2.Main Day3.Main Day4.Main
other-modules: Day1.Main Day2.Main Day3.Main Day4.Main Day5.Main

59
2022/app/Day5/Main.hs Normal file
View File

@ -0,0 +1,59 @@
module Day5.Main where
import System.IO
import Data.List
import Data.List.Split
import Text.Regex.TDFA
main :: IO ()
main = do
putStrLn "Day 5"
handle <- openFile "app/Day5/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ r2
return ()
trim :: String -> String
trim = unwords . words
type Stack = [Char]
type Instruction = (Int, Int, Int) -- count, from, to
-- yea I'm not cleaning this up, I just glued this together in a repl
parseData :: String -> ([Stack], [Instruction])
parseData contents = (stacks, instructions)
where
parts = splitOn "\n\n" contents
stacks = map (trim . snd) $ filter (\(i, _) -> i `mod` 4 == 2) $ zip [1..] (transpose $ init $ lines $ parts !! 0)
instructions = map (\(a:b:c:_) -> (a,b-1,c-1)) $ map (\(_, _, _, m) -> map (read :: String -> Int) m) $ map ((=~ "move ([[:digit:]]+) from ([[:digit:]]+) to ([[:digit:]]+)") :: String -> (String, String, String, [String])) (lines $ parts !! 1)
update :: Int -> a -> [a] -> [a]
update n item ls = a ++ (item:b)
where (a, (_:b)) = splitAt n ls
executeInstructions :: Bool -> [Stack] -> [Instruction] -> [Stack]
executeInstructions is9001 stacks [] = stacks
executeInstructions is9001 stacks ((c,f,t):is) = executeInstructions is9001 stacks' is
where
from = stacks !! f
to = stacks !! t
items = (if is9001 then id else reverse) $ take c from
from' = drop c $ from
to' = items ++ to
stacks' = update f from' $ update t to' stacks
crane :: Bool -> String -> String
crane is9001 contents = map (!! 0) stacks'
where
(stacks, instructions) = parseData contents
stacks' = executeInstructions is9001 stacks instructions
part1, part2 :: String -> String
part1 = crane False
part2 = crane True

514
2022/app/Day5/input Normal file
View File

@ -0,0 +1,514 @@
[Q] [N] [N]
[H] [B] [D] [S] [M]
[C] [Q] [J] [V] [Q] [D]
[T] [S] [Z] [F] [J] [J] [W]
[N] [G] [T] [S] [V] [B] [C] [C]
[S] [B] [R] [W] [D] [J] [Q] [R] [Q]
[V] [D] [W] [G] [P] [W] [N] [T] [S]
[B] [W] [F] [L] [M] [F] [L] [G] [J]
1 2 3 4 5 6 7 8 9
move 3 from 6 to 2
move 2 from 8 to 7
move 3 from 3 to 8
move 2 from 5 to 3
move 5 from 9 to 7
move 5 from 3 to 5
move 1 from 4 to 2
move 3 from 2 to 1
move 2 from 9 to 6
move 4 from 1 to 4
move 6 from 5 to 8
move 1 from 6 to 3
move 8 from 8 to 9
move 5 from 9 to 2
move 1 from 3 to 4
move 11 from 7 to 2
move 1 from 4 to 1
move 1 from 5 to 9
move 1 from 3 to 9
move 1 from 9 to 5
move 21 from 2 to 6
move 2 from 8 to 4
move 5 from 8 to 6
move 4 from 9 to 7
move 2 from 5 to 6
move 5 from 4 to 2
move 4 from 7 to 2
move 20 from 6 to 9
move 7 from 2 to 7
move 1 from 2 to 6
move 7 from 9 to 6
move 3 from 7 to 9
move 7 from 1 to 9
move 3 from 7 to 4
move 1 from 2 to 5
move 1 from 5 to 2
move 1 from 1 to 9
move 23 from 9 to 1
move 1 from 2 to 4
move 1 from 9 to 6
move 1 from 1 to 5
move 20 from 1 to 7
move 1 from 5 to 9
move 12 from 4 to 2
move 2 from 1 to 3
move 1 from 3 to 5
move 4 from 2 to 9
move 2 from 6 to 4
move 9 from 7 to 4
move 11 from 6 to 7
move 7 from 2 to 8
move 1 from 5 to 7
move 2 from 9 to 7
move 1 from 6 to 4
move 6 from 8 to 2
move 3 from 7 to 1
move 6 from 2 to 4
move 1 from 3 to 2
move 7 from 4 to 3
move 1 from 8 to 5
move 3 from 6 to 7
move 1 from 2 to 9
move 1 from 6 to 7
move 4 from 4 to 1
move 1 from 3 to 1
move 22 from 7 to 6
move 3 from 7 to 6
move 4 from 7 to 6
move 5 from 4 to 5
move 26 from 6 to 2
move 8 from 1 to 9
move 2 from 6 to 5
move 9 from 9 to 5
move 2 from 9 to 4
move 1 from 5 to 3
move 1 from 9 to 5
move 1 from 5 to 6
move 1 from 4 to 3
move 3 from 5 to 8
move 1 from 6 to 2
move 1 from 6 to 1
move 1 from 1 to 8
move 4 from 5 to 2
move 7 from 2 to 4
move 8 from 5 to 3
move 1 from 5 to 7
move 12 from 2 to 8
move 6 from 3 to 8
move 1 from 7 to 6
move 10 from 3 to 4
move 11 from 8 to 7
move 6 from 8 to 3
move 11 from 7 to 4
move 1 from 6 to 3
move 6 from 3 to 1
move 6 from 1 to 5
move 15 from 4 to 7
move 1 from 3 to 5
move 7 from 2 to 3
move 5 from 5 to 9
move 2 from 3 to 8
move 1 from 9 to 4
move 1 from 9 to 7
move 1 from 4 to 5
move 5 from 7 to 8
move 13 from 4 to 1
move 8 from 8 to 2
move 2 from 2 to 7
move 7 from 7 to 4
move 1 from 5 to 1
move 1 from 5 to 9
move 3 from 8 to 9
move 7 from 9 to 8
move 1 from 5 to 2
move 6 from 8 to 2
move 6 from 7 to 6
move 2 from 2 to 7
move 2 from 8 to 3
move 3 from 4 to 5
move 1 from 7 to 1
move 3 from 3 to 5
move 4 from 4 to 6
move 3 from 6 to 3
move 11 from 2 to 9
move 5 from 3 to 4
move 1 from 1 to 4
move 7 from 9 to 4
move 1 from 6 to 4
move 5 from 5 to 4
move 9 from 1 to 3
move 4 from 6 to 3
move 2 from 1 to 7
move 3 from 9 to 8
move 1 from 9 to 5
move 5 from 3 to 4
move 3 from 4 to 6
move 3 from 7 to 5
move 4 from 2 to 4
move 10 from 3 to 1
move 2 from 8 to 9
move 1 from 8 to 4
move 2 from 2 to 9
move 5 from 5 to 8
move 2 from 6 to 3
move 4 from 9 to 4
move 2 from 3 to 7
move 2 from 6 to 3
move 1 from 6 to 1
move 1 from 8 to 5
move 1 from 5 to 8
move 1 from 4 to 8
move 17 from 4 to 2
move 11 from 4 to 2
move 1 from 8 to 6
move 28 from 2 to 3
move 10 from 3 to 1
move 3 from 8 to 1
move 1 from 7 to 8
move 1 from 7 to 1
move 1 from 6 to 5
move 10 from 1 to 5
move 20 from 3 to 5
move 3 from 1 to 6
move 3 from 8 to 1
move 18 from 5 to 1
move 4 from 4 to 6
move 4 from 5 to 1
move 1 from 6 to 8
move 7 from 5 to 8
move 2 from 5 to 3
move 34 from 1 to 8
move 4 from 1 to 7
move 36 from 8 to 6
move 6 from 8 to 4
move 3 from 6 to 4
move 1 from 1 to 2
move 1 from 3 to 2
move 1 from 3 to 5
move 1 from 1 to 8
move 1 from 7 to 2
move 3 from 2 to 8
move 3 from 8 to 1
move 2 from 7 to 5
move 5 from 6 to 4
move 31 from 6 to 4
move 1 from 7 to 3
move 13 from 4 to 7
move 2 from 5 to 9
move 1 from 1 to 9
move 1 from 3 to 1
move 11 from 4 to 9
move 12 from 4 to 3
move 4 from 9 to 1
move 1 from 9 to 8
move 1 from 5 to 9
move 3 from 6 to 5
move 3 from 5 to 1
move 11 from 7 to 8
move 6 from 4 to 8
move 3 from 3 to 8
move 5 from 1 to 6
move 1 from 7 to 3
move 5 from 8 to 3
move 2 from 4 to 7
move 8 from 8 to 4
move 5 from 8 to 2
move 2 from 2 to 1
move 7 from 9 to 2
move 5 from 6 to 7
move 6 from 2 to 4
move 3 from 9 to 1
move 3 from 1 to 4
move 2 from 2 to 1
move 5 from 1 to 2
move 6 from 2 to 9
move 4 from 7 to 6
move 2 from 9 to 6
move 1 from 2 to 5
move 1 from 6 to 5
move 5 from 3 to 1
move 1 from 5 to 3
move 2 from 6 to 1
move 1 from 9 to 7
move 3 from 7 to 3
move 4 from 8 to 4
move 1 from 5 to 6
move 9 from 1 to 4
move 4 from 6 to 8
move 2 from 7 to 4
move 2 from 1 to 9
move 10 from 3 to 1
move 7 from 1 to 3
move 1 from 1 to 2
move 1 from 2 to 4
move 2 from 3 to 8
move 6 from 8 to 9
move 2 from 1 to 2
move 30 from 4 to 3
move 29 from 3 to 7
move 2 from 2 to 4
move 7 from 9 to 5
move 6 from 4 to 8
move 5 from 8 to 9
move 5 from 5 to 7
move 1 from 5 to 4
move 17 from 7 to 9
move 6 from 3 to 9
move 4 from 3 to 7
move 1 from 8 to 6
move 17 from 9 to 8
move 8 from 9 to 3
move 1 from 5 to 6
move 9 from 8 to 7
move 3 from 9 to 5
move 1 from 4 to 5
move 2 from 6 to 1
move 3 from 3 to 8
move 2 from 3 to 5
move 1 from 3 to 8
move 10 from 8 to 4
move 2 from 1 to 9
move 1 from 8 to 1
move 1 from 1 to 5
move 1 from 8 to 6
move 4 from 4 to 5
move 1 from 3 to 9
move 3 from 9 to 6
move 1 from 9 to 8
move 2 from 9 to 1
move 2 from 1 to 7
move 1 from 9 to 1
move 3 from 4 to 6
move 2 from 4 to 9
move 1 from 1 to 8
move 2 from 8 to 1
move 5 from 6 to 2
move 2 from 1 to 4
move 2 from 9 to 1
move 2 from 6 to 3
move 2 from 3 to 1
move 2 from 4 to 7
move 4 from 1 to 5
move 15 from 5 to 4
move 4 from 2 to 5
move 7 from 4 to 2
move 4 from 4 to 5
move 1 from 3 to 9
move 3 from 5 to 2
move 9 from 2 to 1
move 3 from 5 to 4
move 1 from 5 to 3
move 1 from 9 to 7
move 1 from 5 to 8
move 4 from 1 to 6
move 1 from 3 to 2
move 2 from 1 to 2
move 3 from 2 to 8
move 14 from 7 to 2
move 2 from 6 to 4
move 19 from 7 to 8
move 1 from 7 to 1
move 23 from 8 to 2
move 33 from 2 to 1
move 1 from 7 to 1
move 7 from 4 to 3
move 1 from 6 to 2
move 15 from 1 to 7
move 6 from 2 to 8
move 1 from 8 to 2
move 1 from 2 to 8
move 2 from 3 to 8
move 3 from 8 to 5
move 1 from 6 to 1
move 2 from 4 to 7
move 1 from 5 to 9
move 3 from 8 to 3
move 1 from 2 to 6
move 18 from 1 to 4
move 1 from 6 to 3
move 2 from 5 to 1
move 2 from 8 to 2
move 5 from 1 to 9
move 15 from 4 to 9
move 5 from 9 to 5
move 1 from 1 to 5
move 1 from 1 to 3
move 1 from 1 to 2
move 3 from 2 to 8
move 9 from 9 to 8
move 11 from 8 to 4
move 1 from 8 to 3
move 4 from 7 to 8
move 3 from 3 to 1
move 3 from 3 to 7
move 3 from 5 to 8
move 3 from 5 to 3
move 5 from 9 to 7
move 9 from 4 to 3
move 1 from 8 to 9
move 9 from 3 to 7
move 2 from 3 to 2
move 1 from 4 to 1
move 1 from 8 to 6
move 10 from 7 to 1
move 2 from 2 to 6
move 2 from 6 to 8
move 2 from 9 to 4
move 14 from 1 to 9
move 3 from 4 to 7
move 1 from 6 to 3
move 2 from 8 to 4
move 8 from 7 to 5
move 6 from 7 to 5
move 12 from 9 to 3
move 3 from 9 to 8
move 8 from 8 to 2
move 7 from 2 to 1
move 1 from 7 to 2
move 6 from 7 to 2
move 7 from 3 to 6
move 1 from 6 to 3
move 7 from 2 to 1
move 5 from 4 to 8
move 2 from 7 to 9
move 1 from 2 to 7
move 4 from 6 to 1
move 2 from 8 to 1
move 1 from 7 to 6
move 2 from 6 to 1
move 3 from 3 to 7
move 1 from 4 to 6
move 7 from 3 to 8
move 6 from 8 to 1
move 1 from 9 to 7
move 22 from 1 to 9
move 2 from 7 to 2
move 3 from 3 to 2
move 5 from 1 to 3
move 2 from 2 to 7
move 2 from 6 to 9
move 3 from 9 to 4
move 2 from 4 to 5
move 1 from 4 to 7
move 1 from 1 to 9
move 13 from 9 to 7
move 3 from 9 to 5
move 14 from 5 to 3
move 5 from 9 to 5
move 2 from 9 to 7
move 9 from 5 to 3
move 15 from 3 to 2
move 12 from 7 to 3
move 3 from 2 to 7
move 8 from 7 to 5
move 4 from 8 to 9
move 1 from 9 to 6
move 1 from 7 to 5
move 14 from 2 to 7
move 2 from 9 to 4
move 1 from 6 to 5
move 18 from 3 to 2
move 5 from 3 to 9
move 2 from 3 to 6
move 2 from 4 to 8
move 15 from 7 to 6
move 1 from 9 to 1
move 2 from 8 to 3
move 1 from 7 to 9
move 6 from 9 to 6
move 2 from 3 to 7
move 3 from 5 to 8
move 8 from 5 to 3
move 2 from 7 to 9
move 22 from 6 to 9
move 12 from 2 to 3
move 1 from 1 to 9
move 1 from 2 to 6
move 1 from 6 to 5
move 6 from 2 to 6
move 7 from 6 to 3
move 20 from 9 to 4
move 5 from 9 to 3
move 7 from 3 to 5
move 14 from 4 to 6
move 2 from 4 to 1
move 2 from 8 to 3
move 2 from 1 to 5
move 9 from 6 to 1
move 20 from 3 to 4
move 5 from 6 to 8
move 1 from 5 to 9
move 1 from 9 to 6
move 9 from 5 to 7
move 1 from 6 to 5
move 2 from 3 to 4
move 4 from 8 to 2
move 2 from 8 to 4
move 3 from 3 to 7
move 5 from 1 to 7
move 4 from 2 to 7
move 1 from 1 to 3
move 3 from 3 to 6
move 4 from 7 to 3
move 1 from 1 to 4
move 3 from 3 to 5
move 1 from 1 to 7
move 28 from 4 to 3
move 20 from 3 to 5
move 16 from 5 to 6
move 3 from 3 to 2
move 2 from 3 to 6
move 6 from 7 to 5
move 1 from 3 to 6
move 1 from 2 to 1
move 10 from 6 to 8
move 2 from 1 to 5
move 1 from 4 to 8
move 1 from 6 to 9
move 2 from 2 to 5
move 10 from 7 to 4
move 2 from 3 to 4
move 1 from 3 to 8
move 1 from 9 to 4
move 6 from 4 to 1
move 10 from 8 to 6
move 1 from 1 to 4
move 8 from 4 to 9
move 3 from 1 to 5
move 14 from 5 to 8
move 2 from 7 to 5
move 3 from 9 to 7
move 5 from 9 to 5
move 2 from 7 to 3
move 16 from 6 to 9
move 3 from 6 to 3
move 1 from 1 to 5
move 1 from 1 to 4
move 1 from 7 to 3
move 2 from 6 to 1
move 2 from 5 to 7
move 2 from 7 to 1
move 3 from 3 to 8
move 12 from 5 to 4
move 1 from 5 to 8
move 1 from 1 to 4
move 9 from 4 to 1
move 11 from 1 to 7
move 10 from 7 to 4
move 3 from 3 to 7
move 1 from 1 to 7
move 5 from 4 to 5
move 8 from 4 to 1
move 1 from 4 to 1
move 5 from 5 to 4
move 2 from 7 to 5
move 2 from 7 to 3
move 9 from 1 to 7
move 16 from 8 to 5
move 3 from 8 to 7
move 6 from 4 to 3
move 17 from 5 to 1
move 14 from 1 to 2
move 7 from 2 to 4
move 5 from 2 to 6

View File

@ -4,6 +4,7 @@ import qualified Day1.Main as Day1
import qualified Day2.Main as Day2
import qualified Day3.Main as Day3
import qualified Day4.Main as Day4
import qualified Day5.Main as Day5
main :: IO ()
main = do
@ -11,3 +12,4 @@ main = do
Day2.main
Day3.main
Day4.main
Day5.main