From 15e54117f32d225f6a89320c0d80f45e55cc43bd Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 12 Dec 2020 11:40:35 +0530 Subject: add initial day12 solution --- execs/Day12.hs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 execs/Day12.hs (limited to 'execs') 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 @@ +module Main where + +parseLine s = (head s, read (tail s) :: Float) + +moves ls = sum $ map (round . abs) [fx, fy] + where (fx, fy, _) = foldl fn (0.0, 0.0, 0.0) ls + fn (x, y, rot) ('N', v) = (x, y + v, rot) + fn (x, y, rot) ('E', v) = (x + v, y, rot) + fn (x, y, rot) ('W', v) = (x - v, y, rot) + fn (x, y, rot) ('S', v) = (x, y - v, rot) + fn (x, y, rot) ('R', v) = (x, y, rot - v) + 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) + +waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy] + where (fx, fy, _, _) = foldl fn (0.0, 0.0, sx, sy) ls + fn (x, y, wx, wy) ('N', v) = (x, y, wx, wy+v) + fn (x, y, wx, wy) ('E', v) = (x, y, wx+v, wy) + 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 + 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" + print $ moves n + print $ waypoint (10.0, 1.0) n -- cgit v1.2.3