Skip to content

Instantly share code, notes, and snippets.

View abhin4v's full-sized avatar

Abhinav Sarkar abhin4v

View GitHub Profile
nextGrids :: Grid -> (Grid, Grid)
nextGrids grid =
let (i, first@(Fixed _), rest) =
fixCell
. Data.List.minimumBy (compare `Data.Function.on` (possibilityCount . snd))
. filter (isPossible . snd)
. zip [0..]
. concat
$ grid
in (replace2D i first grid, replace2D i rest grid)
readGrid :: String -> Maybe Grid
readGrid s
| length s == 81 = traverse (traverse readCell) . Data.List.Split.chunksOf 9 $ s
| otherwise = Nothing
where
readCell '.' = Just $ Possible [1..9]
readCell c
| Data.Char.isDigit c && c > '0' = Just . Fixed . Data.Char.digitToInt $ c
| otherwise = Nothing
+-------------------------------------+-------------------------------------+-------------------------------------+
| [123456789] [123456789] [123456789] | [123456789] [123456789] [123456789] | [123456789] 1 [123456789] |
| 4 [123456789] [123456789] | [123456789] [123456789] [123456789] | [123456789] [123456789] [123456789] |
| [123456789] 2 [123456789] | [123456789] [123456789] [123456789] | [123456789] [123456789] [123456789] |
+-------------------------------------+-------------------------------------+-------------------------------------+
| [123456789] [123456789] [123456789] | [123456789] 5 [123456789] | 4 [123456789] 7 |
| [123456789] [123456789] 8 | [123456789] [123456789] [123456789] | 3 [123456789] [123456789] |
| [123456789] [123456789] 1 | [123456789] 9 [123456789] | [123456789] [123456789] [123456789] |
+-------------------------------------+-------------------------------------+-------------------
@abhin4v
abhin4v / cache_server.exs
Created March 18, 2018 06:36
Simple cache using GenServer in Elixir
defmodule CacheServer do
use GenServer
@name CS
## Client API
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts ++ [name: CS])
end
@abhin4v
abhin4v / pingpong.exs
Created March 17, 2018 15:55
PingPong in Elixir
defmodule PingPong do
def ping(parent_pid) do
receive do
{_, _, 0} -> send parent_pid, :done
{pid, :pong, n} ->
IO.puts("ping: " <> Integer.to_string(n))
send pid, {self(), :ping, n-1}
ping parent_pid
end
end
@abhin4v
abhin4v / FileSort.hs
Last active March 14, 2018 05:36
Simple on-file number sort in haskell
module Main where
import Data.List (sort)
import Data.List.Split (chunksOf)
import System.IO.Temp (writeSystemTempFile)
readI :: String -> Int
readI = read
merge :: (Ord a) => [a] -> [a] -> [a]
@abhin4v
abhin4v / sudoku.hs
Last active February 6, 2018 10:42
Sudoku solver in Haskell
import Control.Arrow ((&&&))
import Control.Applicative ((<|>))
import Control.DeepSeq (NFData(..))
import Control.Monad (foldM)
import Control.Parallel.Strategies (withStrategy, rdeepseq, parBuffer)
import Data.Bits
import Data.Char (isDigit, digitToInt)
import Data.Function (on)
import Data.List (nub, foldl', group, sort)
import Data.Maybe (isJust)

Keybase proof

I hereby claim:

  • I am abhin4v on github.
  • I am abhin4v (https://keybase.io/abhin4v) on keybase.
  • I have a public key whose fingerprint is D2AA 0E12 9D4B 6D06 0FC6 4B22 7C91 66A6 F546 5AD5

To claim this, I am signing this object:

@abhin4v
abhin4v / transformers.hs
Last active October 22, 2017 18:56
An exploration of Monad Transformers in Haskell
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module Transformers where
import Control.Applicative
import Control.Monad
import qualified Data.Char as Char
@abhin4v
abhin4v / guava_cache.clj
Created May 11, 2016 06:56
A clojure wrapper over Google Guava Cache
(ns guava-cache
(:refer-clojure :exclude [get])
(:import [com.google.common.cache Cache CacheBuilder]
(java.util Map)))
(defn create [cache-spec]
(.build (CacheBuilder/from ^String cache-spec)))
(def ^:private nil-sentinal "ABC_NIL_SENTINAL_XYZ")