Day 9 - Part 2

This commit is contained in:
Jens Kadenbach
2022-12-09 18:53:29 +01:00
parent dc5a757eb6
commit 08306ed57d
2 changed files with 53 additions and 34 deletions

View File

@@ -29,6 +29,17 @@ expectedPositions = sort [
(0,0),(1,0),(2,0),(3,0)
]
testInput2 :: String
testInput2 = [str|R 5
|U 8
|L 8
|D 3
|R 17
|D 10
|L 25
|U 20
|]
spec :: Spec
spec =
describe "Day9" $ do
@@ -51,7 +62,7 @@ spec =
step (0, 1) (Step (0, -1)) `shouldBe` (0, 0)
it "normalizes movement" $ do
normalizeMovement still `shouldBe` []
normalizeMovement (left 1) `shouldBe` [Step (-1, 0)]
normalizeMovement (left 2) `shouldBe` [Step (-1, 0),Step (-1, 0)]
normalizeMovement (right 1) `shouldBe` [Step (1, 0)]
normalizeMovement (up 1) `shouldBe` [Step (0, 1)]
normalizeMovement (down 2) `shouldBe` [Step (0, -1), Step (0, -1)]
@@ -74,6 +85,18 @@ spec =
positions `shouldBe` expectedPositions
it "solves the riddle" $ do
input <- readFile "ressources/day09-input"
let movements = parseMovements input
let positions = concatMap normalizeMovement >>> recordPositions >>> fst >>> length $ movements
let headSteps = parseMovements >>> concatMap normalizeMovement $ input
let allTails = buildTails headSteps
let positions = (!! 1) >>> fst >>> length $ allTails
positions `shouldBe` 5878
it "solves example of part 2" $ do
let headSteps = parseMovements >>> concatMap normalizeMovement $ testInput2
let allTails = buildTails headSteps
let tail9Positions = fst $ allTails !! 9
length tail9Positions `shouldBe` 36
it "solves the riddle part 2" $ do
input <- readFile "ressources/day09-input"
let headSteps = parseMovements >>> concatMap normalizeMovement $ input
let allTails = buildTails headSteps
let positions = (!! 9) >>> fst >>> length $ allTails
positions `shouldBe` 2405