Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jrheard/euler
base: 4a2989ee30
...
head fork: jrheard/euler
compare: 54ddf5234a
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Feb 27, 2012
@jrheard initial progress on routes' da658a7
Commits on Feb 29, 2012
@jrheard ok, routes' is 2500 times faster than routes, but it still didn't fin…
…ish executing after 2 days. gotta make it a couple thousand times faster still.
54ddf52
Showing with 18 additions and 2 deletions.
  1. +18 −2 haskell/15.hs
View
20 haskell/15.hs
@@ -1,10 +1,26 @@
-- Starting in the top left corner of a 2x2 grid, there are 6 routes (without backtracking) to the bottom right corner.
-- How many routes are there through a 20x20 grid?
-import Data.List (nub, permutations)
+import Data.List (delete, nub, permutations)
+
+
+data Direction = D | R deriving (Eq, Show)
routes :: Int -> Int -> [[Int]]
routes x y = nub $ permutations $ concat [(take x (repeat 0)), (take y (repeat 1))]
-main = print $ length (routes 2 2)
+routesLeft :: [Direction] -> [[Direction]]
+routesLeft remaining
+ | D `elem` remaining && R `elem` remaining = concat [downBranch, rightBranch]
+ | D `elem` remaining = downBranch
+ | R `elem` remaining = rightBranch
+ | otherwise = [[]]
+ where downBranch = [D:route | route <- routesLeft (delete D remaining)]
+ rightBranch = [R:route | route <- routesLeft (delete R remaining)]
+
+routes' :: Int -> Int -> [[Direction]]
+routes' x y = routesLeft initialDirections where
+ initialDirections = concat [(take x (repeat R)), (take y (repeat D))]
+
+main = print $ length (routes' 20 20)

No commit comments for this range

Something went wrong with that request. Please try again.