Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
Created July 1, 2014 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlohamalainen/c89941a4056f3c807783 to your computer and use it in GitHub Desktop.
Save carlohamalainen/c89941a4056f3c807783 to your computer and use it in GitHub Desktop.
Airports for Blinco.
{-# LANGUAGE FlexibleInstances #-}
-- Airports for Blinco!
module Airports where
import Control.Monad (forM_)
airportsRaw :: [String]
airportsRaw = [ "BNE", "SYD", "ADL", "PER", "CNB", "NTL", "UDG", "CTL"
, "ULP", "TWB", "WNR", "BVI", "LRE", "EMD", "BLT", "THG"
, "ROK", "MKY", "PPP", "TSV", "CNS", "HID", "SIN", "NRT"
, "PVG", "BKK", "CNX", "KBV", "BOM", "DEL", "UDR", "AMM"
, "BWN", "AKL", "CHC", "ZQN", "LAX", "SFO", "SLC", "LAS"
, "ORD", "BMI", "JFK", "IAD", "ATL", "BHN", "DEN", "STL"
, "SJU", "MEX", "SJO", "LIM", "LPB", "SCL", "PMO", "PUQ"
, "ZCO", "EZE", "MDZ", "AEP", "COR", "MVD", "LHR", "LGW"
, "STN", "LCY", "CDG", "TXL", "FRA", "MUC", "BCN", "BIO"
, "FCO", "VCE", "MXP", "GVA", "CPH", "SVO", "LED"
]
toTriple :: Show a => [a] -> (Maybe a, Maybe a, Maybe a)
toTriple [x, y, z] = (Just x, Just y, Just z)
toTriple x = error $ "derp" ++ show x
emptyGrid :: Grid (Maybe a)
emptyGrid = Grid ( (Nothing, Nothing, Nothing)
, (Nothing, Nothing, Nothing)
, (Nothing, Nothing, Nothing)
)
data Grid a = Grid ((a, a, a), (a, a, a), (a, a, a)) deriving (Eq)
showMaybeChar :: Maybe Char -> Char
showMaybeChar (Just c) = c
showMaybeChar Nothing = '_'
instance Show (Grid (Maybe Char)) where
show (Grid ( (x0, x1, x2)
, (x3, x4, x5)
, (x6, x7, x8))) = (showMaybeChar x0):(showMaybeChar x1):(showMaybeChar x2):'\n'
:(showMaybeChar x3):(showMaybeChar x4):(showMaybeChar x5):'\n'
:(showMaybeChar x6):(showMaybeChar x7):(showMaybeChar x8):'\n':[]
exts0 :: [(Maybe a, Maybe a, Maybe a)] -> Grid (Maybe a) -> [Grid (Maybe a)]
exts0 codes (Grid ((Nothing, Nothing, Nothing), row1, row2)) = [Grid (code, row1, row2) | code <- codes]
exts0 _ _ = error "derp exts0"
exts1 :: [(Maybe a, Maybe a, Maybe a)] -> Grid (Maybe a) -> [Grid (Maybe a)]
exts1 codes (Grid (row0, (Nothing, Nothing, Nothing), row2)) = [Grid (row0, code, row2) | code <- codes]
exts1 _ _ = error "derp exts1"
exts2 :: [(Maybe a, Maybe a, Maybe a)] -> Grid (Maybe a) -> [Grid (Maybe a)]
exts2 codes (Grid (row0, row1, (Nothing, Nothing, Nothing))) = [Grid (row0, row1, code) | code <- codes]
exts2 _ _ = error "derp exts2"
isValid :: Eq a => [(Maybe a, Maybe a, Maybe a)] -> Grid (Maybe a) -> Bool
isValid codes (Grid ( (x0, x1, x2)
, (x3, x4, x5)
, (x6, x7, x8))) = (x0, x1, x2) `elem` codes
&& (x3, x4, x5) `elem` codes
&& (x6, x7, x8) `elem` codes
&& (x0, x3, x6) `elem` codes
&& (x1, x4, x7) `elem` codes
&& (x2, x5, x8) `elem` codes
main :: IO ()
main = do
let codes = map toTriple $ airportsRaw
e0 = exts0 codes emptyGrid
e1 = concatMap (exts1 codes) e0
e2 = concatMap (exts2 codes) e1
good = filter (isValid codes) e2
forM_ good print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment