diff options
author | Tom Smeding <tom.smeding@gmail.com> | 2020-12-16 18:45:47 +0100 |
---|---|---|
committer | Tom Smeding <tom.smeding@gmail.com> | 2020-12-16 18:45:47 +0100 |
commit | ac45449f8882a76612dcca699f84f54f08edcff4 (patch) | |
tree | 42820b6236286c9a081a5cbc80893f5a55d2fdc8 /2020 | |
parent | b524b905c29a01660663223b24a1ece82ebd48b6 (diff) |
Day 12
Diffstat (limited to '2020')
-rw-r--r-- | 2020/12.hs | 34 |
1 files changed, 32 insertions, 2 deletions
@@ -72,7 +72,37 @@ sail (Sail offset dirm posm) (State pos dir) = manhattan :: Num a => V2 a -> a manhattan (V2 a b) = abs a + abs b +------------ +-- PART 2 -- +------------ + +data State2 = State2 { sPos2 :: V2 Int, sWP :: V2 Int } + deriving (Show) + +runInstr2 :: String -> State2 -> State2 +runInstr2 (c : ns) = case (c, read ns :: Int) of + ('N', n) -> addWP (V2 0 n) + ('S', n) -> addWP (V2 0 (-n)) + ('E', n) -> addWP (V2 n 0) + ('W', n) -> addWP (V2 (-n) 0) + ('F', n) -> \(State2 ship wp) -> State2 (ship !+! (wp *$ n)) wp + ('R', 90) -> mulWP rotRightMat + ('R', 180) -> mulWP mirrorMat + ('R', 270) -> mulWP rotLeftMat + ('L', 90) -> mulWP rotLeftMat + ('L', 180) -> mulWP mirrorMat + ('L', 270) -> mulWP rotRightMat + _ -> error "Cannot parse instr" + where + rotRightMat = M2 0 1 (-1) 0 + rotLeftMat = rotRightMat *$ (-1) + mirrorMat = M2 (-1) 0 0 (-1) + addWP v (State2 ship wp) = State2 ship (wp !+! v) + mulWP m (State2 ship wp) = State2 ship (m *! wp) +runInstr2 _ = error "Cannot parse empty instr" + main :: IO () main = do - input <- map parseInstr <$> getInput 12 - print (manhattan . sPos $ foldl' (flip sail) (State (V2 0 0) (V2 1 0)) input) + input <- getInput 12 + print (manhattan . sPos $ foldl' (flip sail) (State (V2 0 0) (V2 1 0)) (map parseInstr input)) + print (manhattan . sPos2 $ foldl' (flip runInstr2) (State2 (V2 0 0) (V2 10 1)) input) |