diff options
author | Andrew Cady <d@jerkface.net> | 2020-12-29 09:41:47 -0500 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2020-12-29 09:41:47 -0500 |
commit | ab981deb57eedc35ec7bbb4f20e9deecd307123c (patch) | |
tree | 80e069b8faf435820c190f29bc300128655f2f9d /shelf.hs | |
parent | 9357f8327450bb8fa64230e434e060f48338aaa7 (diff) |
better
Diffstat (limited to 'shelf.hs')
-rwxr-xr-x | shelf.hs | 34 |
1 files changed, 32 insertions, 2 deletions
@@ -1,11 +1,32 @@ | |||
1 | #!/usr/bin/env stack | 1 | #!/usr/bin/env stack |
2 | {- stack script --resolver lts-16.14 --install-ghc | 2 | {- stack script --resolver lts-16.27 --install-ghc |
3 | --ghc-options -Wall --ghc-options -Wno-unused-imports -} | 3 | --ghc-options -Wall --ghc-options -Wno-unused-imports -} |
4 | {-# language NoImplicitPrelude #-} | 4 | {-# language NoImplicitPrelude #-} |
5 | {-# language DuplicateRecordFields #-} | 5 | {-# language DuplicateRecordFields #-} |
6 | import Rebase.Prelude | 6 | import Rebase.Prelude |
7 | import Control.Lens | 7 | import Control.Lens |
8 | 8 | ||
9 | import Data.Ratio | ||
10 | import Text.Printf | ||
11 | |||
12 | toCarpenter :: RealFrac a => a -> (Int, Int, Ratio Int) | ||
13 | toCarpenter l = (feet, div r 32, mod r 32 % 32) where | ||
14 | (feet, r) = divMod (round $ l * 32) (32 * 12) | ||
15 | |||
16 | feetAndInches :: RealFrac a => a -> String | ||
17 | feetAndInches l = case toCarpenter l of | ||
18 | (0,0,0) -> "0 feet 0 inches" | ||
19 | (f,i,t) -> showUnit "foot" "feet" (f % 1) ++ | ||
20 | (if f > 0 && (i%1 + t) > 0 then " " else "") ++ | ||
21 | showUnit "inch" "inches" (i % 1 + t) | ||
22 | where | ||
23 | showUnit _ _ 0 = "" | ||
24 | showUnit s m n = printf "%s %s" (showVal n) $ if n <= 1 then s else m | ||
25 | showVal v | d == 1 = show n | ||
26 | | v < 1 = printf "%d/%d" n d | ||
27 | | otherwise = printf "%d and %d/%d" (div n d) (mod n d) d | ||
28 | where (n,d) = (numerator v, denominator v) | ||
29 | |||
9 | type Inches = Rational | 30 | type Inches = Rational |
10 | 31 | ||
11 | boardLength, boardThickness, kickerHeight, numShelves, numHorizontals, availableSpace :: Inches | 32 | boardLength, boardThickness, kickerHeight, numShelves, numHorizontals, availableSpace :: Inches |
@@ -18,11 +39,20 @@ numHorizontals = numShelves + 1 | |||
18 | availableSpace = boardLength - kickerHeight - numHorizontals * boardThickness | 39 | availableSpace = boardLength - kickerHeight - numHorizontals * boardThickness |
19 | 40 | ||
20 | shelves :: [Rational] | 41 | shelves :: [Rational] |
21 | shelves = [2%9, 2%9, 2%9, 1%6, 1%6] | 42 | shelves = kickerHeight : map (* availableSpace) [2%9, 2%9, 2%9, 1%6, 1%6] |
43 | |||
44 | positions :: [Inches] | ||
45 | positions = map (+ boardThickness) $ map sum $ tails shelves | ||
22 | 46 | ||
47 | positions' = map (boardLength -) positions | ||
23 | 48 | ||
24 | main :: IO () | 49 | main :: IO () |
25 | main = do | 50 | main = do |
26 | _ <- return $ assert $ sum shelves == 1 | 51 | _ <- return $ assert $ sum shelves == 1 |
27 | print shelves | 52 | print shelves |
53 | print positions | ||
54 | print $ map (fromRational :: Rational -> Float) positions | ||
55 | print positions' | ||
56 | print $ map (fromRational :: Rational -> Float) positions' | ||
57 | mapM_ putStrLn $ feetAndInches <$> positions' | ||
28 | return () | 58 | return () |