From e3d58e974a07b39922696697f23727c6ae333d04 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 12 Dec 2020 19:53:11 +0530 Subject: rotate is now a util --- execs/Day12.hs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) (limited to 'execs') 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 @@ module Main where +import Utils + parseLine s = (head s, read (tail s) :: Float) moves ls = sum $ map (round . abs) [fx, fy] @@ -12,11 +14,7 @@ moves ls = sum $ map (round . abs) [fx, fy] 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) - -rotate (x, y) t = (nx, ny) - where nx = x*cos(pi*t/180) - y*sin(pi*t/180) - ny = x*sin(pi*t/180) + y*cos(pi*t/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 @@ -25,13 +23,11 @@ waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy] 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 p = negate v - (nwx, nwy) = rotate (wx, wy) p + 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" -- cgit v1.2.3