aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
Diffstat (limited to 'execs')
-rw-r--r--execs/Day12.hs39
1 files changed, 39 insertions, 0 deletions
diff --git a/execs/Day12.hs b/execs/Day12.hs
new file mode 100644
index 0000000..72e0124
--- /dev/null
+++ b/execs/Day12.hs
@@ -0,0 +1,39 @@
1module Main where
2
3parseLine s = (head s, read (tail s) :: Float)
4
5moves ls = sum $ map (round . abs) [fx, fy]
6 where (fx, fy, _) = foldl fn (0.0, 0.0, 0.0) ls
7 fn (x, y, rot) ('N', v) = (x, y + v, rot)
8 fn (x, y, rot) ('E', v) = (x + v, y, rot)
9 fn (x, y, rot) ('W', v) = (x - v, y, rot)
10 fn (x, y, rot) ('S', v) = (x, y - v, rot)
11 fn (x, y, rot) ('R', v) = (x, y, rot - v)
12 fn (x, y, rot) ('L', v) = (x, y, rot + v)
13 fn (x, y, rot) ('F', v) = (nx, ny, rot)
14 where nx = x + v * cos (pi * rot / 180)
15 ny = y + v*sin (pi * rot / 180)
16
17rotate (x, y) t = (nx, ny)
18 where nx = x*cos(pi*t/180) - y*sin(pi*t/180)
19 ny = x*sin(pi*t/180) + y*cos(pi*t/180)
20
21waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy]
22 where (fx, fy, _, _) = foldl fn (0.0, 0.0, sx, sy) ls
23 fn (x, y, wx, wy) ('N', v) = (x, y, wx, wy+v)
24 fn (x, y, wx, wy) ('E', v) = (x, y, wx+v, wy)
25 fn (x, y, wx, wy) ('W', v) = (x, y, wx-v, wy)
26 fn (x, y, wx, wy) ('S', v) = (x, y, wx, wy-v)
27 fn (x, y, wx, wy) ('R', v) = (x, y, nwx, nwy)
28 where p = negate v
29 (nwx, nwy) = rotate (wx, wy) p
30 fn (x, y, wx, wy) ('L', v) = (x, y, nwx, nwy)
31 where (nwx, nwy) = rotate (wx, wy) v
32 fn (x, y, wx, wy) ('F', v) = (x+v*wx, y+v*wy, wx, wy)
33
34
35main :: IO ()
36main = do
37 n <- map parseLine . lines <$> readFile "input/12"
38 print $ moves n
39 print $ waypoint (10.0, 1.0) n