From: Neil Smith Date: Mon, 21 Dec 2020 11:42:26 +0000 (+0000) Subject: Done day 12 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=0f78ee34867c196e4ac5d35c96334f0b644c23d9;p=advent-of-code-20.git Done day 12 --- diff --git a/advent12/package.yaml b/advent12/package.yaml new file mode 100644 index 0000000..5dbbf31 --- /dev/null +++ b/advent12/package.yaml @@ -0,0 +1,63 @@ +# 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: . + +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 diff --git a/advent12/src/advent12.hs b/advent12/src/advent12.hs new file mode 100644 index 0000000..fb75491 --- /dev/null +++ b/advent12/src/advent12.hs @@ -0,0 +1,123 @@ +-- 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 diff --git a/data/advent12.txt b/data/advent12.txt new file mode 100644 index 0000000..a1b531b --- /dev/null +++ b/data/advent12.txt @@ -0,0 +1,787 @@ +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 +R90 +F90 +S2 +F13 +E2 +F3 +E1 +F60 +S5 +E2 +F74 +L90 +S3 +W3 +F57 +N4 +E2 +F33 +S4 +F64 +N5 +L90 +F29 +N5 +S2 +R90 +F46 +S4 +E5 +L90 +F68 +W5 +S2 +R180 +F27 +W3 +L90 +S5 +R90 +E1 +R180 +F100 +S4 +W3 +R90 +E1 +S4 +W5 +F20 +W1 +N1 +R90 +E1 +N4 +E1 +F54 +N3 +F77 +L270 +N1 +F26 +N4 +E5 +S5 +N1 +F98 +E4 +F52 +W1 +F6 +R180 +N1 +F31 +W3 +N2 +F100 +L180 +E3 +F43 +R180 +E4 +F8 +L90 +W3 +L270 +N2 +R90 +N2 +E4 +L90 +E5 +E4 +S3 +F89 +L180 +S3 +N5 +R90 +F53 +F43 +R180 +E5 +N5 +F88 +W1 +E4 +L90 +W2 +N5 +F75 +L90 +E1 +S4 +F65 +N3 +W3 +F88 +E2 +S3 +E2 +N2 +R90 +S2 +F98 +N4 +S2 +F13 +R90 +N3 +F74 +R90 +F56 +S2 +E3 +S4 +F72 +N2 +R90 +F21 +E4 +N4 +L90 +F72 +L90 +N1 +N2 +F61 +W2 +L90 +F28 +S3 +W5 +S5 +F81 +S1 +E5 +N3 +F49 +N1 +F4 +N3 +F78 +E1 +F81 +N3 +W4 +F12 +L90 +S3 +E4 +F2 +W2 +R90 +S1 +W2 +F40 +S1 +W1 +W4 +N4 +L90 +N2 +E1 +L180 +N5 +F30 +L90 +N3 +F84 +W1 +F6 +S3 +F72 +N2 +W4 +S4 +W4 +E5 +L90 +L90 +N4 +S2 +F19 +N1 +W5 +N4 +L90 +N2 +F54 +L90 +W4 +F96 +N4 +R180 +S2 +F53 +R90 +S3 +E5 +N5 +L180 +E5 +S4 +L90 +N5 +L90 +E3 +L90 +F63 +S2 +L90 +F35 +N2 +F52 +W1 +L90 +F94 +N5 +E5 +R270 +N2 +R180 +S2 +E2 +N2 +S3 +F86 +N1 +F54 +N1 +L90 +W2 +R90 +E4 +R90 +N3 +R90 +F30 +S4 +F98 +W2 +S5 +R90 +N5 +W2 +S1 +F36 +S3 +R90 +S1 +F84 +S5 +N5 +L180 +F16 +N1 +F55 +L90 +N4 +S2 +S3 +R90 +S5 +W5 +N1 +E4 +S5 +W1 +L180 +F100 +E4 +S3 +E3 +N3 +E1 +L90 +W4 +F60 +W5 +L270 +W1 +S2 +L90 +R90 +F52 +S1 +W3 +N4 +F30 +E2 +F9 +F87 +S5 +R90 +S4 +W5 +R180 +S3 +E4 +S4 +L90 +W3 +F94 +F85 +R90 +E4 +W2 +S2 +L180 +W4 +F28 +E3 +N5 +F53 diff --git a/problems/day12.html b/problems/day12.html new file mode 100644 index 0000000..a6e479d --- /dev/null +++ b/problems/day12.html @@ -0,0 +1,173 @@ + + + + +Day 12 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 24*

      /*2020*/

+ + + +
+ +

--- Day 12: Rain Risk ---

Your ferry made decent progress toward the island, but the storm came in faster than anyone expected. The ferry needs to take evasive actions!

+

Unfortunately, the ship's navigation computer seems to be malfunctioning; rather than giving a route directly to safety, it produced extremely circuitous instructions. When the captain uses the PA system to ask if anyone can help, you quickly volunteer.

+

The navigation instructions (your puzzle input) consists of a sequence of single-character actions paired with integer input values. After staring at them for a few minutes, you work out what they probably mean:

+
    +
  • Action N means to move north by the given value.
  • +
  • Action S means to move south by the given value.
  • +
  • Action E means to move east by the given value.
  • +
  • Action W means to move west by the given value.
  • +
  • Action L means to turn left the given number of degrees.
  • +
  • Action R means to turn right the given number of degrees.
  • +
  • Action F means to move forward by the given value in the direction the ship is currently facing.
  • +
+

The ship starts by facing east. Only the L and R actions change the direction the ship is facing. (That is, if the ship is facing east and the next instruction is N10, the ship would move north 10 units, but would still move east if the following action were F.)

+

For example:

+
F10
+N3
+F7
+R90
+F11
+
+

These instructions would be handled as follows:

+
    +
  • F10 would move the ship 10 units east (because the ship starts by facing east) to east 10, north 0.
  • +
  • N3 would move the ship 3 units north to east 10, north 3.
  • +
  • F7 would move the ship another 7 units east (because the ship is still facing east) to east 17, north 3.
  • +
  • R90 would cause the ship to turn right by 90 degrees and face south; it remains at east 17, north 3.
  • +
  • F11 would move the ship 11 units south to east 17, south 8.
  • +
+

At the end of these instructions, the ship's Manhattan distance (sum of the absolute values of its east/west position and its north/south position) from its starting position is 17 + 8 = 25.

+

Figure out where the navigation instructions lead. What is the Manhattan distance between that location and the ship's starting position?

+
+

Your puzzle answer was 1106.

--- Part Two ---

Before you can give the destination to the captain, you realize that the actual action meanings were printed on the back of the instructions the whole time.

+

Almost all of the actions indicate how to move a waypoint which is relative to the ship's position:

+
    +
  • Action N means to move the waypoint north by the given value.
  • +
  • Action S means to move the waypoint south by the given value.
  • +
  • Action E means to move the waypoint east by the given value.
  • +
  • Action W means to move the waypoint west by the given value.
  • +
  • Action L means to rotate the waypoint around the ship left (counter-clockwise) the given number of degrees.
  • +
  • Action R means to rotate the waypoint around the ship right (clockwise) the given number of degrees.
  • +
  • Action F means to move forward to the waypoint a number of times equal to the given value.
  • +
+

The waypoint starts 10 units east and 1 unit north relative to the ship. The waypoint is relative to the ship; that is, if the ship moves, the waypoint moves with it.

+

For example, using the same instructions as above:

+
    +
  • F10 moves the ship to the waypoint 10 times (a total of 100 units east and 10 units north), leaving the ship at east 100, north 10. The waypoint stays 10 units east and 1 unit north of the ship.
  • +
  • N3 moves the waypoint 3 units north to 10 units east and 4 units north of the ship. The ship remains at east 100, north 10.
  • +
  • F7 moves the ship to the waypoint 7 times (a total of 70 units east and 28 units north), leaving the ship at east 170, north 38. The waypoint stays 10 units east and 4 units north of the ship.
  • +
  • R90 rotates the waypoint around the ship clockwise 90 degrees, moving it to 4 units east and 10 units south of the ship. The ship remains at east 170, north 38.
  • +
  • F11 moves the ship to the waypoint 11 times (a total of 44 units east and 110 units south), leaving the ship at east 214, south 72. The waypoint stays 4 units east and 10 units south of the ship.
  • +
+

After these operations, the ship's Manhattan distance from its starting position is 214 + 72 = 286.

+

Figure out where the navigation instructions actually lead. What is the Manhattan distance between that location and the ship's starting position?

+
+

Your puzzle answer was 107281.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index daaaab9..33cb0a8 100644 --- a/stack.yaml +++ b/stack.yaml @@ -46,6 +46,7 @@ packages: - advent09 - advent10 - advent11 +- advent12 # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as