-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.hs
53 lines (45 loc) · 1.42 KB
/
Day02.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
{-# LANGUAGE LambdaCase #-}
module Day02 where
import Data.List (foldl')
import Control.Monad (void)
import Text.ParserCombinators.ReadP
import ParseHelper
main :: IO ()
main = do
commands <- parseInput parseCommand <$> getContents
putStrLn $ "Part 1: " <> show (makeMoves commands)
putStrLn $ "Part 2: " <> show (makeAimedMoves commands)
data Command
= Forward Int
| Down Int
| Up Int
deriving (Show, Read, Eq)
parseCommand :: ReadP Command
parseCommand =
choice
[ intCmd "forward" Forward
, intCmd "down" Down
, intCmd "up" Up
]
where
intCmd :: String -> (Int -> Command) -> ReadP Command
intCmd label tag = do
void $ string label
skipSpaces
tag <$> parseInt
makeMoves :: [Command] -> Int
makeMoves = uncurry (*) . foldl' go (0, 0)
where
go :: (Int, Int) -> Command -> (Int, Int)
go (horiz, depth) = \case
Forward x -> (horiz + x, depth)
Down x -> (horiz, depth + x)
Up x -> (horiz, depth - x)
makeAimedMoves :: [Command] -> Int
makeAimedMoves = (\(x, y, _) -> x * y) . foldl' makeMove (0, 0, 0)
where
makeMove :: (Int, Int, Int) -> Command -> (Int, Int, Int)
makeMove (horiz, depth, aim) = \case
Down x -> (horiz, depth, aim + x)
Up x -> (horiz, depth, aim - x)
Forward x -> (horiz + x, depth + aim * x, aim)