From: Mart Lubbers Date: Sat, 12 Dec 2020 08:44:33 +0000 (+0100) Subject: buggy 2 with n 90degree instead of one rotation X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=754773056f0764224f62ab7757dfafb6828146d3;p=aoc20.git buggy 2 with n 90degree instead of one rotation --- diff --git a/12/two.icl b/12/two.icl new file mode 100644 index 0000000..2078f63 --- /dev/null +++ b/12/two.icl @@ -0,0 +1,41 @@ +module two + +import StdEnv +import Data.Func +import Data.Tuple + +read :: *File -> [(Char, Int)] +read f + # (l, f) = freadline f + | l.[size l - 1] <> '\n' = [] + = [(l.[0], toInt (l % (1, size l - 2))):read f] + +Start w + # (io, w) = stdio w + # ls = read io + = (\s->abs s.sx+abs s.sy) + $ foldl move {sx=0,sy=0,wx=10,wy=1} ls + +:: Ship = { sx :: Int, sy :: Int, wx :: Int, wy :: Int } + +move s ('N', n) = {s & wy=s.wy+n} +move s ('S', n) = {s & wy=s.wy-n} +move s ('E', n) = {s & wx=s.wx+n} +move s ('W', n) = {s & wx=s.wx-n} +move s ('F', n) + # dx = s.wx-s.sx + # dy = s.wy-s.sy + # s = {s & sx=s.sx+dx*n, sy=s.sy+dy*n} + = {s & wx=s.sx+dx, wy=s.sy+dy} +move s ('R', n) = rot (n/90) s +move s ('L', n) = rot (3*(n/90)) s + +//https://stackoverflow.com/questions/2259476/rotating-a-point-about-another-point-2d/25196651#25196651 +rot :: Int Ship -> Ship +rot 0 ship = ship +rot n ship=:{sx, sy, wx, wy} + # s = toInt (sin 90.0) + # c = toInt (cos 90.0) + # nx = sx + ( (wx-sx) * c + (wy-sy) * s) + # ny = sy + (~(wx-sx) * s + (wy-sy) * c) + = rot (n-1) {ship & wx=nx, wy=ny}