aboutsummaryrefslogtreecommitdiff
path: root/execs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-12-12 14:23:11 +0000
committerAkshay <[email protected]>2020-12-12 14:23:11 +0000
commite3d58e974a07b39922696697f23727c6ae333d04 (patch)
tree254d6ed6243f835303bb16356641d70f1e2726f2 /execs
parent15e54117f32d225f6a89320c0d80f45e55cc43bd (diff)
rotate is now a util
Diffstat (limited to 'execs')
-rw-r--r--execs/Day12.hs12
1 files changed, 4 insertions, 8 deletions
diff --git a/execs/Day12.hs b/execs/Day12.hs
index 72e0124..11d2aa6 100644
--- a/execs/Day12.hs
+++ b/execs/Day12.hs
@@ -1,5 +1,7 @@
1module Main where 1module Main where
2 2
3import Utils
4
3parseLine s = (head s, read (tail s) :: Float) 5parseLine s = (head s, read (tail s) :: Float)
4 6
5moves ls = sum $ map (round . abs) [fx, fy] 7moves ls = sum $ map (round . abs) [fx, fy]
@@ -12,11 +14,7 @@ moves ls = sum $ map (round . abs) [fx, fy]
12 fn (x, y, rot) ('L', v) = (x, y, rot + v) 14 fn (x, y, rot) ('L', v) = (x, y, rot + v)
13 fn (x, y, rot) ('F', v) = (nx, ny, rot) 15 fn (x, y, rot) ('F', v) = (nx, ny, rot)
14 where nx = x + v * cos (pi * rot / 180) 16 where nx = x + v * cos (pi * rot / 180)
15 ny = y + v*sin (pi * rot / 180) 17 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 18
21waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy] 19waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy]
22 where (fx, fy, _, _) = foldl fn (0.0, 0.0, sx, sy) ls 20 where (fx, fy, _, _) = foldl fn (0.0, 0.0, sx, sy) ls
@@ -25,13 +23,11 @@ waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy]
25 fn (x, y, wx, wy) ('W', v) = (x, y, wx-v, wy) 23 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) 24 fn (x, y, wx, wy) ('S', v) = (x, y, wx, wy-v)
27 fn (x, y, wx, wy) ('R', v) = (x, y, nwx, nwy) 25 fn (x, y, wx, wy) ('R', v) = (x, y, nwx, nwy)
28 where p = negate v 26 where (nwx, nwy) = rotate (wx, wy) (negate v)
29 (nwx, nwy) = rotate (wx, wy) p
30 fn (x, y, wx, wy) ('L', v) = (x, y, nwx, nwy) 27 fn (x, y, wx, wy) ('L', v) = (x, y, nwx, nwy)
31 where (nwx, nwy) = rotate (wx, wy) v 28 where (nwx, nwy) = rotate (wx, wy) v
32 fn (x, y, wx, wy) ('F', v) = (x+v*wx, y+v*wy, wx, wy) 29 fn (x, y, wx, wy) ('F', v) = (x+v*wx, y+v*wy, wx, wy)
33 30
34
35main :: IO () 31main :: IO ()
36main = do 32main = do
37 n <- map parseLine . lines <$> readFile "input/12" 33 n <- map parseLine . lines <$> readFile "input/12"