aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--execs/Day12.hs12
-rw-r--r--lib/Utils.hs4
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 @@
1module Main where 1module Main where
2 2
3import Utils
4
3parseLine s = (head s, read (tail s) :: Float) 5parseLine s = (head s, read (tail s) :: Float)
4 6
5moves ls = sum $ map (round . abs) [fx, fy] 7moves 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
17rotate (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
21waypoint (sx, sy) ls = sum $ map (round . abs) [fx, fy] 19waypoint (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
35main :: IO () 31main :: IO ()
36main = do 32main = do
37 n <- map parseLine . lines <$> readFile "input/12" 33 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)
52inside (p, q) (r, s) (a, b) = bet a (p, r) && bet b (q, s) 52inside (p, q) (r, s) (a, b) = bet a (p, r) && bet b (q, s)
53inside' (p, q) (r, s) (a, b) = bet' a (p, r) && bet' b (q, s) 53inside' (p, q) (r, s) (a, b) = bet' a (p, r) && bet' b (q, s)
54 54
55rotate (x, y) t = (nx, ny)
56 where nx = x*cos(pi*t/180) - y*sin(pi*t/180)
57 ny = x*sin(pi*t/180) + y*cos(pi*t/180)
58
55-- [f, f.f, f.f.f, ...] 59-- [f, f.f, f.f.f, ...]
56repeatF f = f : map (f .) (repeatF f) 60repeatF f = f : map (f .) (repeatF f)
57 61