diff options
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day12.hs | 12 |
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 @@ | |||
1 | module Main where | 1 | module Main where |
2 | 2 | ||
3 | import Utils | ||
4 | |||
3 | parseLine s = (head s, read (tail s) :: Float) | 5 | parseLine s = (head s, read (tail s) :: Float) |
4 | 6 | ||
5 | moves ls = sum $ map (round . abs) [fx, fy] | 7 | moves 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 | |||
17 | rotate (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 | ||
21 | waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy] | 19 | waypoint (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 | |||
35 | main :: IO () | 31 | main :: IO () |
36 | main = do | 32 | main = do |
37 | n <- map parseLine . lines <$> readFile "input/12" | 33 | n <- map parseLine . lines <$> readFile "input/12" |