Skip to content

Instantly share code, notes, and snippets.

@szastupov
Created December 24, 2009 21:58
Show Gist options
  • Save szastupov/263388 to your computer and use it in GitHub Desktop.
Save szastupov/263388 to your computer and use it in GitHub Desktop.
import qualified Data.Set as Set
(x1, y1) |+ (x2, y2) = (x1+x2, y1+y2)
size = 8
out_of_greed (x, y) =
let check v = v < 0 || v == size
in check x || check y
add_dangerous pos set dir =
let iter set pos
| out_of_greed pos = set
| otherwise = iter (Set.insert pos set) (pos |+ dir)
in
iter set pos
make_dangerous pos set =
foldl (add_dangerous pos) set [(0, 1), (-1, 1), (1, 1)]
place pos@(x, y) set | y >= size = Just []
| otherwise =
fmap (pos :) (try 0 (y+1))
where
dang = make_dangerous pos set
unsafe pos = Set.member pos dang
try x y | x == size = Nothing
| unsafe (x, y) = try (x+1) y
| otherwise =
case place (x, y) dang of
Nothing -> try (x+1) y
sol -> sol
draw_list xs =
let get_cell pos
| pos `elem` xs = '#'
| otherwise = '0'
draw_line line = do
mapM_ (putChar . get_cell) line
putChar '\n'
in
mapM_ draw_line [[(x, y) | x <- [0..size]] | y <- [0..size]]
main = do
case place (0, 0) Set.empty of
Just sol -> draw_list sol
Nothing -> putStrLn "failed :("
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment