-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
53 lines (43 loc) · 1.21 KB
/
run.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 BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeApplications #-}
import AoC
import AoC.Grid
import Data.Bifunctor
import Data.Bits (xor)
import Data.Function ((&))
import Data.List
import Data.Maybe
import Data.Ord (comparing)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
data Cmd = F Int | U Int | D Int
deriving Show
parse :: String -> Cmd
parse line = break (== ' ') line & \case
("forward", v) -> F (read v)
("up", v) -> U (read v)
("down", v) -> D (read v)
parseAll = map parse . lines
toV2 = \case
F v -> v2 (v, 0)
U v -> v2 (0, negate v)
D v -> v2 (0, v)
part1 = product . sum . map toV2
toAimCmd = \case
F v -> (\(V3 (h, d, a)) -> v3 (h + v, d + a * v, a))
U v -> (+ v3 (0, 0, negate v))
D v -> (+ v3 (0, 0, v))
part2 = product . dropZ . foldl (flip ($)) 0 . map toAimCmd
main = main' "input.txt"
exampleMain = main' "example.txt"
main' file = do
input <- parseAll <$> readFile file
print (part1 input)
print (part2 input)