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 ++++-------- lib/Utils.hs | 4 ++++ 2 files changed, 8 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 @@ 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" diff --git a/lib/Utils.hs b/lib/Utils.hs index 6358230..56d9c69 100644 --- a/lib/Utils.hs +++ b/lib/Utils.hs @@ -52,6 +52,10 @@ shear c (x, y) = (c * x, c * y) inside (p, q) (r, s) (a, b) = bet a (p, r) && bet b (q, s) inside' (p, q) (r, s) (a, b) = bet' a (p, r) && bet' b (q, s) +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) + -- [f, f.f, f.f.f, ...] repeatF f = f : map (f .) (repeatF f) -- cgit v1.2.3