diff options
author | Akshay <[email protected]> | 2020-12-12 06:10:35 +0000 |
---|---|---|
committer | Akshay <[email protected]> | 2020-12-12 06:10:35 +0000 |
commit | 15e54117f32d225f6a89320c0d80f45e55cc43bd (patch) | |
tree | 641569ae5b85bf7e6d6bf32beee217361ae7ecf0 /execs | |
parent | 42523f2455fb30181efc29e3a47799283b05fa80 (diff) |
add initial day12 solution
Diffstat (limited to 'execs')
-rw-r--r-- | execs/Day12.hs | 39 |
1 files changed, 39 insertions, 0 deletions
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 @@ | |||
1 | module Main where | ||
2 | |||
3 | parseLine s = (head s, read (tail s) :: Float) | ||
4 | |||
5 | moves ls = sum $ map (round . abs) [fx, fy] | ||
6 | where (fx, fy, _) = foldl fn (0.0, 0.0, 0.0) ls | ||
7 | fn (x, y, rot) ('N', v) = (x, y + v, rot) | ||
8 | fn (x, y, rot) ('E', v) = (x + v, y, rot) | ||
9 | fn (x, y, rot) ('W', v) = (x - v, y, rot) | ||
10 | fn (x, y, rot) ('S', v) = (x, y - v, rot) | ||
11 | fn (x, y, rot) ('R', v) = (x, y, rot - v) | ||
12 | fn (x, y, rot) ('L', v) = (x, y, rot + v) | ||
13 | fn (x, y, rot) ('F', v) = (nx, ny, rot) | ||
14 | where nx = x + v * cos (pi * rot / 180) | ||
15 | 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 | |||
21 | waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy] | ||
22 | where (fx, fy, _, _) = foldl fn (0.0, 0.0, sx, sy) ls | ||
23 | fn (x, y, wx, wy) ('N', v) = (x, y, wx, wy+v) | ||
24 | fn (x, y, wx, wy) ('E', v) = (x, y, wx+v, wy) | ||
25 | 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) | ||
27 | fn (x, y, wx, wy) ('R', v) = (x, y, nwx, nwy) | ||
28 | where p = negate v | ||
29 | (nwx, nwy) = rotate (wx, wy) p | ||
30 | fn (x, y, wx, wy) ('L', v) = (x, y, nwx, nwy) | ||
31 | where (nwx, nwy) = rotate (wx, wy) v | ||
32 | fn (x, y, wx, wy) ('F', v) = (x+v*wx, y+v*wy, wx, wy) | ||
33 | |||
34 | |||
35 | main :: IO () | ||
36 | main = do | ||
37 | n <- map parseLine . lines <$> readFile "input/12" | ||
38 | print $ moves n | ||
39 | print $ waypoint (10.0, 1.0) n | ||