Commit 0f78ee34 authored by Neil Smith's avatar Neil Smith
Browse files

Done day 12

parent 29632086
# This YAML file describes your package. Stack will automatically generate a
# Cabal file when you run `stack build`. See the hpack website for help with
# this file: <https://github.com/sol/hpack>.
name: advent12
synopsis: Advent of Code
version: '0.0.1'
default-extensions:
- AllowAmbiguousTypes
- ApplicativeDo
- BangPatterns
- BlockArguments
- DataKinds
- DeriveFoldable
- DeriveFunctor
- DeriveGeneric
- DeriveTraversable
- EmptyCase
- FlexibleContexts
- FlexibleInstances
- FunctionalDependencies
- GADTs
- GeneralizedNewtypeDeriving
- ImplicitParams
- KindSignatures
- LambdaCase
- MonadComprehensions
- MonoLocalBinds
- MultiParamTypeClasses
- MultiWayIf
- NamedFieldPuns
- NegativeLiterals
- NumDecimals
# - OverloadedLists
- OverloadedStrings
- PartialTypeSignatures
- PatternGuards
- PatternSynonyms
- PolyKinds
- RankNTypes
- RecordWildCards
- ScopedTypeVariables
- TemplateHaskell
- TransformListComp
- TupleSections
- TypeApplications
- TypeFamilies
- TypeInType
- TypeOperators
- ViewPatterns
executables:
advent12:
main: advent12.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
- text
- attoparsec
- containers
- vector
\ No newline at end of file
-- import Debug.Trace
import Data.Text (Text)
-- import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Data.Attoparsec.Text
-- import Data.Attoparsec.Combinator
-- import Control.Applicative
data Direction = North | East | South | West
deriving (Show, Eq, Ord, Enum, Bounded)
type Position = (Int, Int) -- (x, y)
data Action a = N a | S a | E a | W a | L a | R a | F a
deriving (Show, Eq, Ord)
data Ship = Ship { direction :: Direction, position :: Position }
deriving (Show, Eq, Ord)
data ShipW = ShipW { positionW :: Position
, waypoint :: Position
}
deriving (Show, Eq, Ord)
main :: IO ()
main =
do text <- TIO.readFile "data/advent12.txt"
let actions = successfulParse text
-- print actions
print $ part1 actions
print $ part2 actions
part1 actions = manhattan (position ship1) start
where start = (0, 0)
ship0 = Ship {position = start, direction = East }
ship1 = foldl act ship0 actions
part2 actions = manhattan (positionW ship1) start
where start = (0, 0)
ship0 = ShipW {positionW = start, waypoint = (10, 1)}
ship1 = foldl actW ship0 actions
apAc actions = ship1
where start = (0, 0)
ship0 = Ship {position = start, direction = East }
ship1 = foldl act ship0 actions
apAcW actions = ship1
where start = (0, 0)
ship0 = ShipW {positionW = start, waypoint = (10, 1) }
ship1 = foldl actW ship0 actions
act Ship{..} (N d) = Ship { position = dDelta d North position, ..}
act Ship{..} (S d) = Ship { position = dDelta d South position, ..}
act Ship{..} (W d) = Ship { position = dDelta d West position, ..}
act Ship{..} (E d) = Ship { position = dDelta d East position, ..}
act Ship{..} (L a) = Ship { direction = d, ..} where d = (iterate predW direction) !! (a `div` 90)
act Ship{..} (R a) = Ship { direction = d, ..} where d = (iterate succW direction) !! (a `div` 90)
act Ship{..} (F d) = Ship { position = dDelta d direction position, ..}
-- where (x, y) = position
-- (dx, dy) = (delta direction)
-- p' = (x + (d * dx), y + (d * dy))
actW ShipW{..} (N d) = ShipW { waypoint = dDelta d North waypoint, ..}
actW ShipW{..} (S d) = ShipW { waypoint = dDelta d South waypoint, ..}
actW ShipW{..} (W d) = ShipW { waypoint = dDelta d West waypoint, ..}
actW ShipW{..} (E d) = ShipW { waypoint = dDelta d East waypoint, ..}
actW ShipW{..} (L a) = ShipW { waypoint = d, ..} where d = (iterate rotL waypoint) !! (a `div` 90)
actW ShipW{..} (R a) = ShipW { waypoint = d, ..} where d = (iterate rotR waypoint) !! (a `div` 90)
actW ShipW{..} (F d) = ShipW { positionW = p', ..}
where (x, y) = positionW
(dx, dy) = waypoint
p' = (x + (d* dx), y + (d * dy))
rotL (x, y) = (-y, x)
rotR (x, y) = (y, -x)
delta North = ( 0, 1)
delta South = ( 0, -1)
delta East = ( 1, 0)
delta West = (-1, 0)
dDelta dist dir (x, y) = (x + (dist * dx), y + (dist * dy))
where (dx, dy) = delta dir
manhattan (x1, y1) (x2, y2) = abs (x1 - x2) + abs (y1 - y2)
-- | a `succ` that wraps
succW :: (Bounded a, Enum a, Eq a) => a -> a
succW dir | dir == maxBound = minBound
| otherwise = succ dir
-- | a `pred` that wraps
predW :: (Bounded a, Enum a, Eq a) => a -> a
predW dir | dir == minBound = maxBound
| otherwise = pred dir
-- Parse the input file
actionsP = actionP `sepBy` endOfLine
actionP = choice [nP, sP, eP, wP, lP, rP, fP]
nP = N <$> ("N" *> decimal)
sP = S <$> ("S" *> decimal)
eP = E <$> ("E" *> decimal)
wP = W <$> ("W" *> decimal)
lP = L <$> ("L" *> decimal)
rP = R <$> ("R" *> decimal)
fP = F <$> ("F" *> decimal)
successfulParse :: Text -> [Action Int]
successfulParse input =
case parseOnly actionsP input of
Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
Right actions -> actions
F77
E4
S2
W1
L180
N4
R180
S3
W5
F86
L90
E1
F16
R90
N1
E1
F86
S1
F36
E2
L180
N5
F46
N1
L90
F43
S5
R90
F41
W5
N1
F65
E4
N1
W3
F92
N5
F33
R90
S5
L90
W1
R180
L90
S5
F27
R90
N4
R90
F43
E5
S2
F68
N5
R90
F68
R180
S2
E2
S3
F41
L180
E3
R90
F73
R90
N1
L180
N3
L180
W3
S1
R180
N3
F26
N5
F27
L90
F30
R180
N4
R90
E5
N1
F70
E1
L90
N3
F100
L90
E5
L90
S2
F85
W5
R90
F85
E3
R90
E5
F41
R180
S1
L90
F93
S1
F7
N3
R270
W4
S1
F47
R90
N2
W4
F21
S1
W1
F44
L180
E5
N3
W4
F11
L180
E2
F36
W4
F34
R90
F30
N1
E1
F21
N4
F59
E3
F33
N1
L180
W1
R90
E3
F84
W3
R90
W1
N1
R90
F27
S3
L90
N4
E3
F97
N3
F30
W3
F77
E5
F1
R90
F96
E5
N5
W2
L90
S1
F46
N4
F41
W5
L90
S5
F79
R90
F32
S3
R90
F5
L90
E1
R180
W2
N3
L90
S1
R90
W1
F78
W5
N2
E2
R90
S4
S2
E4
F59
R270
W2
L180
S3
R90
W2
F41
N3
F21
L270
F73
N3
E4
L90
E3
F97
E5
N4
W4
F42
W5
S3
R180
N1
F56
E2
F23
R90
F37
L90
S5
W5
R270
E4
F43
W4
R90
E3
N2
R90
S4
L90
N5
F52
E3
L90
F18
F89
L90
W4
F18
E1
L90
E2
F40
F44
R90
N5
R90
S1
L90
F19
N5
L180
N5
W3
N4
F73
R90
E5
R180
F86
E5
S5
F71
W4
F76
S2
R180
S1
L90
S2
F67
R90
N5
E1
F100
S3
W3
N5
R90
F66
L90
E1
L90
W1
F93
S2
F62
F31
L180
F20
R180
F23
W3
F53
W3
R90
E5
R90
N3
R90
E5
F11
W4
R90
W2
R180
E3
N4
E3
F88
L90
N2
W1
R90
F13
N5
W4
F7
S4
W3
F34
E5
S3
F32
F27
N4
F49
S1
F86
S1
F91
S3
F80
R90
S5
E2
L90
F30
W1
R180
W3
S1
R90
F78
W5
N3
R90
S4
F47
F55
N5
L90
F86
S3
E3
F45
S1
F78
S3
F37
E2
F14
L180
E3
F49
N1
L180
F42
F3
N5
E5
F96
S2
L90
F27
E5
S3
W3
S5
F73
N1
W5
S4
L90
W1
S3
W3
R90
E3
L90
F44
L90
R180
F89
W3
R180
F34
E1
F35
N5
R90
N5
F68
L90
F82
S4
F36
W2
S1
S3
R90
N5
E5
F18
R180
S1
F87
R90
F34
R180
N5
E4
F12
E4
L90
S3
E1
S5
W2
F16
E2
F15
N3
W1
F17
S5
L180
F60
N3
F75
R90
F30
E4