2020-12-13 14:15:04 +01:00
|
|
|
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
|
|
|
#! nix-shell -p ghcid
|
|
|
|
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple linear])"
|
|
|
|
#! nix-shell -i "ghcid -c 'ghci' -T main"
|
|
|
|
|
|
|
|
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
|
|
|
|
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports -Wno-type-defaults #-}
|
|
|
|
{-# OPTIONS_GHC -Wno-unused-matches #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
|
|
|
|
2020-12-13 15:28:56 +01:00
|
|
|
import Debug.Trace (trace, traceShowId, traceShow)
|
2020-12-13 14:15:04 +01:00
|
|
|
import Text.Pretty.Simple
|
|
|
|
import Control.Monad
|
|
|
|
import Linear.V2
|
|
|
|
import Data.Monoid
|
|
|
|
|
|
|
|
exampleData :: [ String ]
|
|
|
|
exampleData =
|
|
|
|
[ "F10"
|
|
|
|
, "N3"
|
|
|
|
, "F7"
|
|
|
|
, "R90"
|
|
|
|
, "F11"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-13 15:28:56 +01:00
|
|
|
data Ship = Ship { position :: V2 Double, orientation :: V2 Double }
|
2020-12-13 14:15:04 +01:00
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
-- At first, ship is at (0,0) facing East
|
|
|
|
ship0 :: Ship
|
|
|
|
ship0 = Ship (pure 0) (V2 1 0)
|
|
|
|
|
|
|
|
-- instance Semigroup Ship
|
|
|
|
-- where
|
|
|
|
-- (<>) s1 s2 = Ship (getSum <$> ((Sum <$> (position s1)) <> (Sum <$> (position s2))))
|
|
|
|
-- (getSum <$> ((Sum <$> (orientation s1)) <> (Sum <$> (orientation s2))))
|
|
|
|
--
|
|
|
|
-- instance Monoid Ship
|
|
|
|
-- where
|
|
|
|
-- mempty = Ship (pure 0) (pure 0)
|
|
|
|
|
2020-12-13 15:28:56 +01:00
|
|
|
data Instruction = InsMoveForward !Double
|
|
|
|
| InsMoveNorth !Double
|
|
|
|
| InsMoveSouth !Double
|
|
|
|
| InsMoveEast !Double
|
|
|
|
| InsMoveWest !Double
|
|
|
|
| InsTurnL !Double
|
|
|
|
| InsTurnR !Double
|
2020-12-13 14:15:04 +01:00
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
parseIns :: String -> Instruction
|
|
|
|
-- Action N means to move north by the given value.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('N':x) = InsMoveNorth (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
-- Action S means to move south by the given value.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('S':x) = InsMoveSouth (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
-- Action E means to move east by the given value.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('E':x) = InsMoveEast (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
-- Action W means to move west by the given value.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('W':x) = InsMoveWest (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
-- Action L means to turn left the given number of degrees.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('L':x) = InsTurnL (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
-- Action R means to turn right the given number of degrees.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('R':x) = InsTurnR (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
-- Action F means to move forward by the given value in the direction the ship is currently facing.
|
2020-12-13 15:28:56 +01:00
|
|
|
parseIns ('F':x) = InsMoveForward (read x)
|
2020-12-13 14:15:04 +01:00
|
|
|
parseIns e = trace ("Unknown instruction: "<>show e) undefined
|
|
|
|
|
|
|
|
|
2020-12-13 15:28:56 +01:00
|
|
|
-- 2*PI rad = 360 deg
|
|
|
|
-- 1 rad = 180/PI deg
|
|
|
|
-- PI/180 rad = 1 deg
|
|
|
|
|
|
|
|
runIns :: Ship -> Instruction -> Ship
|
|
|
|
runIns s (InsMoveForward n) = s { position = (position s) + (pure n) * (orientation s) }
|
|
|
|
runIns s (InsMoveNorth n) = s { position = (position s) + (pure n) * V2 0 1 }
|
|
|
|
runIns s (InsMoveSouth n) = s { position = (position s) + (pure n) * V2 0 (-1) }
|
|
|
|
runIns s (InsMoveEast n) = s { position = (position s) + (pure n) * V2 1 0 }
|
|
|
|
runIns s (InsMoveWest n) = s { position = (position s) + (pure n) * V2 (-1) 0 }
|
|
|
|
runIns s (InsTurnL n) = s { orientation = angle ((unangle (orientation s)) + n * pi / 180) }
|
|
|
|
runIns s (InsTurnR n) = s { orientation = angle ((unangle (orientation s)) - n * pi / 180) }
|
|
|
|
|
|
|
|
manhattanV2 :: (Num a) => V2 a -> a
|
|
|
|
manhattanV2 (V2 x y) = (abs x) + (abs y)
|
|
|
|
|
|
|
|
solvePart1 :: [String] -> Double
|
|
|
|
solvePart1 = manhattanV2 . position . foldl runIns ship0 . map parseIns
|
2020-12-13 14:15:04 +01:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
|
|
|
input <- lines <$> readFile "day12/input"
|
|
|
|
putStrLn ":: Tests"
|
|
|
|
pPrint exampleData
|
|
|
|
pPrint $ map parseIns exampleData
|
2020-12-13 15:28:56 +01:00
|
|
|
pPrint $ angle (90 * pi / 180)
|
2020-12-13 14:15:04 +01:00
|
|
|
putStrLn ":: Day 12 - Part 1"
|
|
|
|
pPrint $ solvePart1 exampleData
|
2020-12-13 15:28:56 +01:00
|
|
|
pPrint $ solvePart1 input
|