-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Gabi Volpe
committed
Aug 17, 2016
1 parent
ac4b58b
commit 94e37d4
Showing
3 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
50 | ||
10 | ||
30 | ||
5 | ||
90 | ||
20 | ||
40 | ||
2 | ||
25 | ||
10 | ||
8 | ||
0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Data.List | ||
|
||
data Section = Section { getA :: Int, getB :: Int, getC :: Int } deriving (Show) | ||
type RoadSystem = [Section] | ||
|
||
heathrowToLondon :: RoadSystem | ||
heathrowToLondon = [Section 50 10 30, Section 5 90 20, Section 40 2 25, Section 10 8 0] | ||
|
||
data Label = A | B | C deriving (Show) | ||
type Path = [(Label, Int)] | ||
|
||
roadStep :: (Path, Path) -> Section -> (Path, Path) | ||
roadStep (pathA, pathB) (Section a b c) = | ||
let priceA = sum $ map snd pathA | ||
priceB = sum $ map snd pathB | ||
forwardPriceToA = priceA + a | ||
crossPriceToA = priceB + b + c | ||
forwardPriceToB = priceB + b | ||
crossPriceToB = priceA + a + c | ||
newPathToA = if forwardPriceToA <= crossPriceToA | ||
then (A,a):pathA | ||
else (C,c):(B,b):pathB | ||
newPathToB = if forwardPriceToB <= crossPriceToB | ||
then (B,b):pathB | ||
else (C,c):(A,a):pathA | ||
in (newPathToA, newPathToB) | ||
|
||
optimalPath :: RoadSystem -> Path | ||
optimalPath roadSystem = | ||
let (bestAPath, bestBPath) = foldl roadStep ([],[]) roadSystem | ||
in if sum (map snd bestAPath) <= sum (map snd bestBPath) | ||
then reverse bestAPath | ||
else reverse bestBPath | ||
|
||
groupsOf :: Int -> [a] -> [[a]] | ||
groupsOf 0 _ = undefined | ||
groupsOf _ [] = [] | ||
groupsOf n xs = take n xs : groupsOf n (drop n xs) | ||
|
||
main = do | ||
contents <- getContents | ||
let threes = groupsOf 3 (map read $ lines contents) | ||
roadSystem = map (\[a,b,c] -> Section a b c) threes | ||
path = optimalPath roadSystem | ||
pathString = concat $ map (show . fst) path | ||
pathPrice = sum $ map snd path | ||
putStrLn $ "The best path to take is: " ++ pathString | ||
putStrLn $ "The price is: " ++ show pathPrice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Data.List | ||
|
||
unsafeSolveRPN :: String -> Float | ||
unsafeSolveRPN = head . foldl foldingFunction [] . words | ||
where foldingFunction (x:y:ys) "*" = (x * y):ys | ||
foldingFunction (x:y:ys) "+" = (x + y):ys | ||
foldingFunction (x:y:ys) "-" = (y - x):ys | ||
foldingFunction (x:y:ys) "/" = (y / x):ys | ||
foldingFunction (x:y:ys) "^" = (y ** x):ys | ||
foldingFunction (x:xs) "ln" = log x:xs | ||
foldingFunction xs "sum" = [sum xs] | ||
foldingFunction xs numberString = read numberString:xs | ||
|