From 04ef70548a6c3b632288c03a626257723afdbd0c Mon Sep 17 00:00:00 2001 From: HeNine <> Date: Fri, 9 Dec 2022 11:43:30 +0100 Subject: [PATCH] day 9 --- aoc2022.cabal | 26 + data/input090.txt | 8 + data/input091.txt | 2000 +++++++++++++++++++++++++++++++++++++ data/input093.txt | 8 + src/day09/Day9Lib.hs | 91 ++ src/day09/test/AoCTest.hs | 42 + src/day09/test/Basic.hs | 61 ++ 7 files changed, 2236 insertions(+) create mode 100644 data/input090.txt create mode 100644 data/input091.txt create mode 100644 data/input093.txt create mode 100644 src/day09/Day9Lib.hs create mode 100644 src/day09/test/AoCTest.hs create mode 100644 src/day09/test/Basic.hs diff --git a/aoc2022.cabal b/aoc2022.cabal index d954cf7..41a9938 100644 --- a/aoc2022.cabal +++ b/aoc2022.cabal @@ -248,4 +248,30 @@ test-suite day08aoctest , HUnit , parsec ghc-options: -threaded -rtsopts -with-rtsopts=-N + default-language: Haskell2010 + +-- Day 9 + +test-suite day09basictest + type: exitcode-stdio-1.0 + hs-source-dirs: src/day09/test, src/day09 + main-is: Basic.hs + other-modules: + Day9Lib + build-depends: base + , HUnit + , parsec + ghc-options: -threaded -rtsopts -with-rtsopts=-N + default-language: Haskell2010 + +test-suite day09aoctest + type: exitcode-stdio-1.0 + hs-source-dirs: src/day09/test, src/day09 + main-is: AoCTest.hs + other-modules: + Day9Lib + build-depends: base + , HUnit + , parsec + ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010 \ No newline at end of file diff --git a/data/input090.txt b/data/input090.txt new file mode 100644 index 0000000..28cf97a --- /dev/null +++ b/data/input090.txt @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2 \ No newline at end of file diff --git a/data/input091.txt b/data/input091.txt new file mode 100644 index 0000000..e4d5663 --- /dev/null +++ b/data/input091.txt @@ -0,0 +1,2000 @@ +R 1 +U 1 +D 1 +U 1 +R 2 +D 2 +R 2 +D 2 +R 1 +U 1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U 8 +D 11 +L 15 +U 10 +D 10 +L 6 +D 14 +L 5 +U 3 +D 9 +U 2 +D 16 +U 6 +D 3 +R 15 +D 12 +R 9 +L 16 +R 4 +U 9 +L 8 +R 11 +L 14 +D 6 +U 11 +L 6 +R 7 +U 8 +R 7 +U 8 +D 7 +U 6 +L 15 +U 3 +D 6 +U 3 +R 6 +D 3 +L 10 +R 14 +L 17 +D 12 +L 15 +D 5 +R 6 +D 4 +R 6 +D 5 +U 8 +D 10 +R 13 +D 6 +U 3 +L 5 +U 3 +R 11 +D 11 +U 8 +L 5 +U 11 +L 7 +R 16 +L 11 +D 3 +L 16 +D 14 +R 1 +U 10 +L 7 +U 4 +D 11 +L 9 +R 3 +D 1 +L 7 +R 17 +D 17 +R 11 +U 4 +R 15 +L 10 +R 13 +U 17 +D 16 +L 16 +U 14 +D 11 +U 10 +D 12 +L 11 +R 5 +D 17 +U 1 +R 5 +L 14 +D 14 +R 5 +L 6 +R 11 +U 13 +R 10 +U 15 +L 2 +R 8 +D 3 +R 11 +L 8 +R 11 +U 5 +L 12 +U 2 +L 16 +D 5 +L 17 +R 10 +D 15 +L 16 +U 1 +D 10 +R 13 +L 6 +D 5 +R 13 +U 2 +D 16 +L 9 +U 11 +D 13 +U 16 +L 6 +R 15 +D 10 +U 17 +L 15 +D 7 +R 6 +D 15 +R 11 +L 14 +U 12 +D 18 +R 6 +U 16 +L 1 +U 10 +R 15 +U 3 +L 12 +D 9 +L 11 +D 18 +R 11 +L 12 +U 5 +L 13 +D 2 +R 11 +D 10 +L 15 +R 5 +D 17 +R 6 +L 16 +R 11 +D 1 +R 5 +U 13 +R 11 +D 13 +L 12 +U 10 +D 9 +R 1 +D 15 +R 1 +D 17 +R 10 +U 11 +R 14 +L 17 +R 2 +L 1 +D 12 +R 6 +U 18 +R 3 +L 8 +R 11 +L 15 +U 8 +L 7 +D 2 +L 9 +D 9 +L 11 +U 5 +D 8 +U 16 +R 13 +L 7 +D 6 +U 16 +L 9 +U 3 +R 4 +L 12 +U 18 +D 7 +L 10 +R 16 +D 1 +R 4 +U 4 +R 13 +D 6 +L 5 +R 3 +D 10 +U 4 +L 17 +D 9 +R 8 +L 1 +D 8 +L 16 +D 3 +U 3 +R 1 +U 9 +R 13 +D 3 +L 4 +R 6 +D 10 +U 16 +L 9 +U 5 +L 14 +U 8 +D 17 +L 18 +D 5 +U 10 +R 14 +L 8 +U 6 +R 5 +U 19 +L 9 +U 2 +D 7 +U 14 +R 17 +L 16 +D 15 +U 11 +D 4 +L 16 +U 14 +D 6 +L 3 +R 18 +L 17 +D 6 +L 10 +U 7 +D 11 +L 15 +U 12 +L 6 +D 6 +L 14 +U 17 +L 7 +D 2 +L 7 +U 12 +L 17 +R 16 +U 19 +L 12 +U 10 +L 6 +U 3 +D 16 +L 5 +D 15 +L 7 +R 6 +D 2 +R 19 +L 4 +D 18 +R 19 +D 9 +R 11 +L 17 +R 1 +U 15 +R 3 +D 18 +L 13 +U 7 +D 7 +R 15 +U 4 +D 16 +R 6 +U 4 +R 10 +U 10 +R 1 +D 19 +R 13 +U 7 +L 13 +D 15 +U 2 +L 18 +D 5 +U 5 +D 10 +L 8 +D 13 +U 8 +L 15 +D 12 +U 11 +D 17 +L 14 +R 4 +D 19 +L 6 +D 5 +L 13 +D 16 +U 1 +R 14 +U 15 +L 18 +D 7 +L 14 +D 11 +L 7 +D 18 +U 16 +L 1 +R 18 +L 1 +U 19 +R 3 +D 1 \ No newline at end of file diff --git a/data/input093.txt b/data/input093.txt new file mode 100644 index 0000000..39040bb --- /dev/null +++ b/data/input093.txt @@ -0,0 +1,8 @@ +R 5 +U 8 +L 8 +D 3 +R 17 +D 10 +L 25 +U 20 \ No newline at end of file diff --git a/src/day09/Day9Lib.hs b/src/day09/Day9Lib.hs new file mode 100644 index 0000000..4ac29aa --- /dev/null +++ b/src/day09/Day9Lib.hs @@ -0,0 +1,91 @@ +module Day9Lib + ( Rope (..), + newRope, + -- move, + follow, + -- + processInput1, + processInput2, + ) +where + +import Data.List +import Debug.Trace + +type Vector = (Int, Int) + +-- data Rope = Rope Vector Vector deriving (Show, Eq) +data Rope = Rope [Vector] deriving (Show, Eq) + +plus :: Vector -> Vector -> Vector +(x1, y1) `plus` (x2, y2) = (x1 + x2, y1 + y2) + +minus :: Vector -> Vector -> Vector +(x1, y1) `minus` (x2, y2) = (x1 - x2, y1 - y2) + +stimes :: Int -> Vector -> Vector +m `stimes` (x, y) = (m * x, m * y) + +vl1 :: Vector -> Int +vl1 (x, y) = abs x + abs y + +vlinf :: Vector -> Int +vlinf (x, y) = max (abs x) (abs y) + +vtruncate :: Vector -> Vector +vtruncate (x, y) = (signum x * min 1 (abs x), signum y * min 1 (abs y)) + +newRope :: Int -> Rope +newRope l = Rope $ replicate l (0, 0) + +move :: Vector -> Char -> Int -> Vector +move v 'L' d = v `plus` (d `stimes` (-1, 0)) +move v 'R' d = v `plus` (d `stimes` (1, 0)) +move v 'U' d = v `plus` (d `stimes` (0, 1)) +move v 'D' d = v `plus` (d `stimes` (0, -1)) +move v dir d = error $ "wrong direction:" ++ show dir + +follow :: Vector -> Vector -> [Vector] -> (Vector, [Vector]) +follow h t hist + | vlinf (h `minus` t) >= 2 = + let newt = (t `plus` vtruncate (h `minus` t)) + in follow h newt (newt : hist) + | otherwise = (t, hist) + +-- #################### + +moveRope :: Rope -> [Vector] -> (Rope, [Vector]) +moveRope (Rope [h, t]) hist = + let (newt, newhist) = follow h t hist + in (Rope [h, newt], newhist) +moveRope (Rope (h : k1 : ks)) hist = + let (newk1, _) = follow h k1 [] + (Rope (newks), newhist) = moveRope (Rope (newk1 : ks)) hist + in (Rope (h : newks), newhist) +moveRope _ _ = error "rope too short" + +-- #################### + +processInput1 :: String -> Int +processInput1 = length . nub . snd . foldl processLine1 (newRope 2, [(0, 0)]) . lines + +processLine1 :: (Rope, [Vector]) -> String -> (Rope, [Vector]) +processLine1 (r@(Rope (h : t : [])), hist) (dir : _ : dist) = + let newh = move h dir (read dist) + (newt, newhist) = follow newh t hist + in (Rope [newh, newt], newhist) +processLine1 _ s = error $ "processline1 error: " ++ show s + +processInput2 :: String -> Int +processInput2 = length . nub . snd . foldl processLine2 (newRope 10, [(0, 0)]) . lines + +processLine2 :: (Rope, [Vector]) -> String -> (Rope, [Vector]) +processLine2 (r@(Rope (h : ks)), hist) (dir : _ : dist) = + last . take (read dist + 1) . iterate (move2 dir) $ (r, hist) +processLine2 _ s = error $ "processline2 error: " ++ show s + +move2 dir (r@(Rope (h : ks)), hist) = + let newh = move h dir 1 + (newrope, newhist) = moveRope (Rope (newh : ks)) hist + in (newrope, newhist) +move2 _ _ = error "wut" diff --git a/src/day09/test/AoCTest.hs b/src/day09/test/AoCTest.hs new file mode 100644 index 0000000..3c7d064 --- /dev/null +++ b/src/day09/test/AoCTest.hs @@ -0,0 +1,42 @@ +import Day9Lib (processInput1, processInput2) +import System.IO +import Test.HUnit + +testCases1 = + [ ("data/input090.txt", 13), + ("data/input091.txt", 6384) + ] + +testCase1 (file, result) = do + withFile + file + ReadMode + ( \handle -> do + contents <- hGetContents handle + assertEqual "input test" result $ processInput1 contents + ) + +testCases2 = + [ ("data/input090.txt", 1), + ("data/input091.txt", 2734), + ("data/input093.txt", 36) + ] + +testCase2 (file, result) = do + withFile + file + ReadMode + ( \handle -> do + contents <- hGetContents handle + assertEqual ("input test: " ++ file) result $ processInput2 contents + ) + +tests = + TestList $ + [TestCase (testCase1 c) | c <- testCases1] + ++ [TestCase (testCase2 c) | c <- testCases2] + +main :: IO () +main = do + runTestTT tests + return () diff --git a/src/day09/test/Basic.hs b/src/day09/test/Basic.hs new file mode 100644 index 0000000..3c7ce85 --- /dev/null +++ b/src/day09/test/Basic.hs @@ -0,0 +1,61 @@ +import Day9Lib +import Test.HUnit + +followTests = + [ + -- TestCase $ + -- assertEqual + -- "follow 1 stationary" + -- (newRope, [(0, 0)]) + -- (follow newRope [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 2 in range" + -- (Rope (0, 1) (0, 0), [(0, 0)]) + -- (follow (Rope (0, 1) (0, 0)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 3 right" + -- (Rope (0, 2) (0, 1), [(0, 1), (0, 0)]) + -- (follow (Rope (0, 2) (0, 0)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 4 left" + -- (Rope (0, 2) (0, 3), [(0, 3), (0, 0)]) + -- (follow (Rope (0, 2) (0, 4)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 5 up" + -- (Rope (2, 0) (1, 0), [(1, 0), (0, 0)]) + -- (follow (Rope (2, 0) (0, 0)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 6 diagonal" + -- (Rope (2, 2) (1, 1), [(1, 1), (0, 0)]) + -- (follow (Rope (2, 2) (0, 0)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 7 L" + -- (Rope (1, 3) (1, 2), [(1, 2), (1, 1), (0, 0)]) + -- (follow (Rope (1, 3) (0, 0)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 7.1 L more" + -- (Rope (1, 6) (1, 5), [(1, 5), (1, 4), (1, 3), (1, 2), (1, 1), (0, 0)]) + -- (follow (Rope (1, 6) (0, 0)) [(0, 0)]), + -- TestCase $ + -- assertEqual + -- "follow 8 negative" + -- (Rope (-5, -5) (-5, -4), [(-5, -4), (0, 0)]) + -- (follow (Rope (-5, -5) (-5, -3)) [(0, 0)]) + ] + +tests = + TestList $ + followTests + ++ [] + +main :: IO () +main = do + runTestTT tests + return ()