X-Git-Url: https://git.njae.me.uk/?p=advent-of-code-20.git;a=blobdiff_plain;f=advent12%2Fsrc%2Fadvent12.hs;h=4d0e39d5eebe71b7d7eda22081151df60b8a5173;hp=fb75491e4c8e3e80065d06cd0a53c4e21b790bc3;hb=98771bc642f4f5d28b8374944bd9398631d126fd;hpb=0f78ee34867c196e4ac5d35c96334f0b644c23d9 diff --git a/advent12/src/advent12.hs b/advent12/src/advent12.hs index fb75491..4d0e39d 100644 --- a/advent12/src/advent12.hs +++ b/advent12/src/advent12.hs @@ -17,12 +17,13 @@ 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 Ship = Ship { direction :: Direction, position :: Position } +-- deriving (Show, Eq, Ord) -data ShipW = ShipW { positionW :: Position - , waypoint :: Position - } +data Ship = Ship { position :: Position + , direction :: Direction + , waypoint :: Position + } deriving (Show, Eq, Ord) @@ -38,24 +39,25 @@ main = part1 actions = manhattan (position ship1) start where start = (0, 0) - ship0 = Ship {position = start, direction = East } + ship0 = Ship {position = start, direction = East, waypoint = (10, 1)} ship1 = foldl act ship0 actions -part2 actions = manhattan (positionW ship1) start +part2 actions = manhattan (position ship1) start where start = (0, 0) - ship0 = ShipW {positionW = start, waypoint = (10, 1)} + ship0 = Ship {position = start, direction = East, 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 +-- 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 +-- apAcW actions = ship1 +-- where start = (0, 0) +-- ship0 = ShipW {positionW = start, waypoint = (10, 1) } +-- ship1 = foldl actW ship0 actions +act :: Ship -> Action Int -> Ship 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, ..} @@ -63,20 +65,19 @@ 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 + + +actW :: Ship -> Action Int -> Ship +actW Ship{..} (N d) = Ship { waypoint = dDelta d North waypoint, ..} +actW Ship{..} (S d) = Ship { waypoint = dDelta d South waypoint, ..} +actW Ship{..} (W d) = Ship { waypoint = dDelta d West waypoint, ..} +actW Ship{..} (E d) = Ship { waypoint = dDelta d East waypoint, ..} +actW Ship{..} (L a) = Ship { waypoint = d, ..} where d = (iterate rotL waypoint) !! (a `div` 90) +actW Ship{..} (R a) = Ship { waypoint = d, ..} where d = (iterate rotR waypoint) !! (a `div` 90) +actW Ship{..} (F d) = Ship { position = p', ..} + where (x, y) = position (dx, dy) = waypoint - p' = (x + (d* dx), y + (d * dy)) + p' = (x + (d * dx), y + (d * dy)) rotL (x, y) = (-y, x) rotR (x, y) = (y, -x)