Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Efficiency etc...

Name: Anonymous 2008-12-18 16:29

So, I find myself solving the Project Euler problems and I come across http://projecteuler.net/index.php?section=problems&id=220
I've got a working solution, but I was wondering how I could make it more efficient...

Name: Anonymous 2008-12-19 11:14

forward = (1, 0, 0)
left = (0, 0, 1)
right = (0, 0, -1)

(x, y, a) <+> (dx, dy, da) =
  case a of
    0 -> (x + dx, y + dy, a')
    1 -> (x - dy, y + dx, a')
    2 -> (x - dx, y - dy, a')
    3 -> (x + dy, y - dx, a')
  where a' = (a + da) `mod` 4

dragons n = reverse $ take (n + 1) $ iterate f (0, (0, 0, 0), (0, 0, 0))
  where f (l, a, b) = (2 * l + 1,
                       (a <+> right <+> b <+> forward <+> right),
                       (left <+> forward <+> a <+> left <+> b))

position steps n = loop steps (0, 0, 1) "Fa" n (dragons n)
  where loop 0 pos _ _ _ = pos
        loop steps pos (x:xs) n ds@((l, a, b):ds') =
          case x of
            'F' -> loop (steps - 1) (pos <+> forward) xs n ds
            'L' -> loop steps (pos <+> left) xs n ds
            'R' -> loop steps (pos <+> right) xs n ds
            _ | steps < l -> loop steps pos (expand x) (n - 1) ds'
              | x == 'a'  -> loop (steps - l) (pos <+> a) xs n ds
              | x == 'b'  -> loop (steps - l) (pos <+> b) xs n ds

expand 'a' = "aRbFR"
expand 'b' = "LFaLb"

answer = let (x, y, a) = position (10^12) 50 in (x, y)


Gives the answer instantaneously.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List