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
|
module Main where
import Utils
parseLine s = (head s, read (tail s) :: Float)
moves ls = sum $ map (round . abs) [fx, fy]
where (fx, fy, _) = foldl fn (0.0, 0.0, 0.0) ls
fn (x, y, rot) ('N', v) = (x, y + v, rot)
fn (x, y, rot) ('E', v) = (x + v, y, rot)
fn (x, y, rot) ('W', v) = (x - v, y, rot)
fn (x, y, rot) ('S', v) = (x, y - v, rot)
fn (x, y, rot) ('R', v) = (x, y, rot - v)
fn (x, y, rot) ('L', v) = (x, y, rot + v)
fn (x, y, rot) ('F', v) = (nx, ny, rot)
where nx = x + v * cos (pi * rot / 180)
ny = y + v * sin (pi * rot / 180)
waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy]
where (fx, fy, _, _) = foldl fn (0.0, 0.0, sx, sy) ls
fn (x, y, wx, wy) ('N', v) = (x, y, wx, wy+v)
fn (x, y, wx, wy) ('E', v) = (x, y, wx+v, wy)
fn (x, y, wx, wy) ('W', v) = (x, y, wx-v, wy)
fn (x, y, wx, wy) ('S', v) = (x, y, wx, wy-v)
fn (x, y, wx, wy) ('R', v) = (x, y, nwx, nwy)
where (nwx, nwy) = rotate (wx, wy) (negate v)
fn (x, y, wx, wy) ('L', v) = (x, y, nwx, nwy)
where (nwx, nwy) = rotate (wx, wy) v
fn (x, y, wx, wy) ('F', v) = (x+v*wx, y+v*wy, wx, wy)
main :: IO ()
main = do
n <- map parseLine . lines <$> readFile "input/12"
print $ moves n
print $ waypoint (10.0, 1.0) n
|